till download files, everything works

This commit is contained in:
dimitar committed 2024-11-01 22:13:14 +01:00
1 parent ce893a4185
commit ed1a580b09
6050 files changed
+768543 -177

No files matched your search

+1
View File
@@ -0,0 +1 @@
use flake
Submodule backend/imk-backend/nix-prisma-utils added at 3bf92853b6.
+41 -37
View File
@@ -20,7 +20,7 @@
"@nestjs/passport": "^10.0.3",
"@nestjs/platform-express": "^10.4.6",
"@nestjs/typeorm": "^10.0.2",
"@prisma/client": "^5.21.1",
"@prisma/client": "^5.12.1",
"@types/multer": "^1.4.12",
"bcrypt": "^5.1.1",
"class-transformer": "^0.5.1",
@@ -51,7 +51,7 @@
"eslint-plugin-prettier": "^5.0.0",
"jest": "^29.5.0",
"prettier": "^3.0.0",
"prisma": "^5.21.1",
"prisma": "^5.12.1",
"source-map-support": "^0.5.21",
"supertest": "^6.3.3",
"ts-jest": "^29.1.0",
@@ -2898,10 +2898,11 @@
}
},
"node_modules/@prisma/client": {
"version": "5.21.1",
"resolved": "https://registry.npmjs.org/@prisma/client/-/client-5.21.1.tgz",
"integrity": "sha512-3n+GgbAZYjaS/k0M03yQsQfR1APbr411r74foknnsGpmhNKBG49VuUkxIU6jORgvJPChoD4WC4PqoHImN1FP0w==",
"version": "5.12.1",
"resolved": "https://registry.npmjs.org/@prisma/client/-/client-5.12.1.tgz",
"integrity": "sha512-6/JnizEdlSBxDIdiLbrBdMW5NqDxOmhXAJaNXiPpgzAPr/nLZResT6MMpbOHLo5yAbQ1Vv5UU8PTPRzb0WIxdA==",
"hasInstallScript": true,
"license": "Apache-2.0",
"engines": {
"node": ">=16.13"
},
@@ -2915,48 +2916,53 @@
}
},
"node_modules/@prisma/debug": {
"version": "5.21.1",
"resolved": "https://registry.npmjs.org/@prisma/debug/-/debug-5.21.1.tgz",
"integrity": "sha512-uY8SAhcnORhvgtOrNdvWS98Aq/nkQ9QDUxrWAgW8XrCZaI3j2X7zb7Xe6GQSh6xSesKffFbFlkw0c2luHQviZA==",
"devOptional": true
"version": "5.12.1",
"resolved": "https://registry.npmjs.org/@prisma/debug/-/debug-5.12.1.tgz",
"integrity": "sha512-kd/wNsR0klrv79o1ITsbWxYyh4QWuBidvxsXSParPsYSu0ircUmNk3q4ojsgNc3/81b0ozg76iastOG43tbf8A==",
"devOptional": true,
"license": "Apache-2.0"
},
"node_modules/@prisma/engines": {
"version": "5.21.1",
"resolved": "https://registry.npmjs.org/@prisma/engines/-/engines-5.21.1.tgz",
"integrity": "sha512-hGVTldUkIkTwoV8//hmnAAiAchi4oMEKD3aW5H2RrnI50tTdwza7VQbTTAyN3OIHWlK5DVg6xV7X8N/9dtOydA==",
"version": "5.12.1",
"resolved": "https://registry.npmjs.org/@prisma/engines/-/engines-5.12.1.tgz",
"integrity": "sha512-HQDdglLw2bZR/TXD2Y+YfDMvi5Q8H+acbswqOsWyq9pPjBLYJ6gzM+ptlTU/AV6tl0XSZLU1/7F4qaWa8bqpJA==",
"devOptional": true,
"hasInstallScript": true,
"license": "Apache-2.0",
"dependencies": {
"@prisma/debug": "5.21.1",
"@prisma/engines-version": "5.21.1-1.bf0e5e8a04cada8225617067eaa03d041e2bba36",
"@prisma/fetch-engine": "5.21.1",
"@prisma/get-platform": "5.21.1"
"@prisma/debug": "5.12.1",
"@prisma/engines-version": "5.12.0-21.473ed3124229e22d881cb7addf559799debae1ab",
"@prisma/fetch-engine": "5.12.1",
"@prisma/get-platform": "5.12.1"
}
},
"node_modules/@prisma/engines-version": {
"version": "5.21.1-1.bf0e5e8a04cada8225617067eaa03d041e2bba36",
"resolved": "https://registry.npmjs.org/@prisma/engines-version/-/engines-version-5.21.1-1.bf0e5e8a04cada8225617067eaa03d041e2bba36.tgz",
"integrity": "sha512-qvnEflL0//lh44S/T9NcvTMxfyowNeUxTunPcDfKPjyJNrCNf2F1zQLcUv5UHAruECpX+zz21CzsC7V2xAeM7Q==",
"devOptional": true
"version": "5.12.0-21.473ed3124229e22d881cb7addf559799debae1ab",
"resolved": "https://registry.npmjs.org/@prisma/engines-version/-/engines-version-5.12.0-21.473ed3124229e22d881cb7addf559799debae1ab.tgz",
"integrity": "sha512-6yvO8s80Tym61aB4QNtYZfWVmE3pwqe807jEtzm8C5VDe7nw8O1FGX3TXUaXmWV0fQTIAfRbeL2Gwrndabp/0g==",
"devOptional": true,
"license": "Apache-2.0"
},
"node_modules/@prisma/fetch-engine": {
"version": "5.21.1",
"resolved": "https://registry.npmjs.org/@prisma/fetch-engine/-/fetch-engine-5.21.1.tgz",
"integrity": "sha512-70S31vgpCGcp9J+mh/wHtLCkVezLUqe/fGWk3J3JWZIN7prdYSlr1C0niaWUyNK2VflLXYi8kMjAmSxUVq6WGQ==",
"version": "5.12.1",
"resolved": "https://registry.npmjs.org/@prisma/fetch-engine/-/fetch-engine-5.12.1.tgz",
"integrity": "sha512-qSs3KcX1HKcea1A+hlJVK/ljj0PNIUHDxAayGMvgJBqmaN32P9tCidlKz1EGv6WoRFICYnk3Dd/YFLBwnFIozA==",
"devOptional": true,
"license": "Apache-2.0",
"dependencies": {
"@prisma/debug": "5.21.1",
"@prisma/engines-version": "5.21.1-1.bf0e5e8a04cada8225617067eaa03d041e2bba36",
"@prisma/get-platform": "5.21.1"
"@prisma/debug": "5.12.1",
"@prisma/engines-version": "5.12.0-21.473ed3124229e22d881cb7addf559799debae1ab",
"@prisma/get-platform": "5.12.1"
}
},
"node_modules/@prisma/get-platform": {
"version": "5.21.1",
"resolved": "https://registry.npmjs.org/@prisma/get-platform/-/get-platform-5.21.1.tgz",
"integrity": "sha512-sRxjL3Igst3ct+e8ya/x//cDXmpLbZQ5vfps2N4tWl4VGKQAmym77C/IG/psSMsQKszc8uFC/q1dgmKFLUgXZQ==",
"version": "5.12.1",
"resolved": "https://registry.npmjs.org/@prisma/get-platform/-/get-platform-5.12.1.tgz",
"integrity": "sha512-pgIR+pSvhYHiUcqXVEZS31NrFOTENC9yFUdEAcx7cdQBoZPmHVjtjN4Ss6NzVDMYPrKJJ51U14EhEoeuBlMioQ==",
"devOptional": true,
"license": "Apache-2.0",
"dependencies": {
"@prisma/debug": "5.21.1"
"@prisma/debug": "5.12.1"
}
},
"node_modules/@sinclair/typebox": {
@@ -9316,22 +9322,20 @@
}
},
"node_modules/prisma": {
"version": "5.21.1",
"resolved": "https://registry.npmjs.org/prisma/-/prisma-5.21.1.tgz",
"integrity": "sha512-PB+Iqzld/uQBPaaw2UVIk84kb0ITsLajzsxzsadxxl54eaU5Gyl2/L02ysivHxK89t7YrfQJm+Ggk37uvM70oQ==",
"version": "5.12.1",
"resolved": "https://registry.npmjs.org/prisma/-/prisma-5.12.1.tgz",
"integrity": "sha512-SkMnb6wyIxTv9ACqiHBI2u9gD6y98qXRoCoLEnZsF6yee5Qg828G+ARrESN+lQHdw4maSZFFSBPPDpvSiVTo0Q==",
"devOptional": true,
"hasInstallScript": true,
"license": "Apache-2.0",
"dependencies": {
"@prisma/engines": "5.21.1"
"@prisma/engines": "5.12.1"
},
"bin": {
"prisma": "build/index.js"
},
"engines": {
"node": ">=16.13"
},
"optionalDependencies": {
"fsevents": "2.3.3"
}
},
"node_modules/process-nextick-args": {
+5 -6
View File
@@ -31,7 +31,7 @@
"@nestjs/passport": "^10.0.3",
"@nestjs/platform-express": "^10.4.6",
"@nestjs/typeorm": "^10.0.2",
"@prisma/client": "^5.21.1",
"@prisma/client": "^5.12.1",
"@types/multer": "^1.4.12",
"bcrypt": "^5.1.1",
"class-transformer": "^0.5.1",
@@ -62,7 +62,7 @@
"eslint-plugin-prettier": "^5.0.0",
"jest": "^29.5.0",
"prettier": "^3.0.0",
"prisma": "^5.21.1",
"prisma": "^5.12.1",
"source-map-support": "^0.5.21",
"supertest": "^6.3.3",
"ts-jest": "^29.1.0",
@@ -71,10 +71,9 @@
"tsconfig-paths": "^4.2.0",
"typescript": "^5.1.3"
},
"prisma": {
"seed": "ts-node prisma/seed.ts"
},
"prisma": {
"seed": "ts-node prisma/seed.ts"
},
"jest": {
"moduleFileExtensions": [
"js",
Submodule backend/imk-backend/prisma/nix-prisma-utils added at 3bf92853b6.
@@ -1,5 +1,6 @@
import { Controller, Get, Param } from '@nestjs/common';
import { Controller, Get, Param, Req, UseGuards } from '@nestjs/common';
import { DocumentsService } from './documents.service';
import { JwtAuthGuard } from 'src/auth/jwt-auth.guard';
@Controller('documents')
export class DocumentsController {
@@ -9,4 +10,10 @@ export class DocumentsController {
console.log('userId', userId);
return this.documentsService.getClientDocuments(parseInt(userId));
}
@Get('shared/download/:key')
@UseGuards(JwtAuthGuard)
async downloadDocument(@Param('key') key: string, @Req() req) {
return this.documentsService.downloadDocument(key, req.user.id);
}
}
@@ -1,33 +1,170 @@
import { Injectable } from '@nestjs/common';
// import { Injectable } from '@nestjs/common';
// import { PrismaService } from '../prisma/prisma.service';
// //import { Document } from '@prisma/client';
// @Injectable()
// export class DocumentsService {
// downloadDocument(key: string, id: any) {
// throw new Error('Method not implemented.');
// }
// constructor(private readonly prisma: PrismaService) {}
// async getClientDocuments(clientId: number) {
// // return this.prisma.document.findMany({
// // where: {
// // sharedWithId: clientId,
// // },
// // include: {
// // sharedWith: {
// // select: {
// // id: true,
// // name: true,
// // email: true,
// // },
// // },
// // },
// // });
// return this.prisma.document.findMany({
// where: {
// sharedWithId: clientId,
// },
// orderBy: {
// createdAt: 'desc',
// },
// });
// }
// }
import { Injectable, NotFoundException, UnauthorizedException } from '@nestjs/common';
import { PrismaService } from '../prisma/prisma.service';
//import { Document } from '@prisma/client';
import { S3Service } from '../s3/s3.service';
import { Document, User } from '@prisma/client';
@Injectable()
export class DocumentsService {
constructor(private readonly prisma: PrismaService) {}
constructor(
private prisma: PrismaService,
private s3Service: S3Service,
) {}
async getClientDocuments(clientId: number) {
// return this.prisma.document.findMany({
// where: {
// sharedWithId: clientId,
// },
// include: {
// sharedWith: {
// select: {
// id: true,
// name: true,
// email: true,
// },
// },
// },
// });
return this.prisma.document.findMany({
async findAllByClient(userId: number): Promise<Document[]> {
const documents = await this.prisma.document.findMany({
where: {
sharedWithId: clientId,
sharedWithId: userId,
},
include: {
sharedWith: {
select: {
id: true,
name: true,
email: true,
},
},
},
orderBy: {
createdAt: 'desc',
},
});
return documents;
}
async getClientDocuments(clientId: number) {
// return this.prisma.document.findMany({
// where: {
// sharedWithId: clientId,
// },
// include: {
// sharedWith: {
// select: {
// id: true,
// name: true,
// email: true,
// },
// },
// },
// });
return this.prisma.document.findMany({
where: {
sharedWithId: clientId,
},
orderBy: {
createdAt: 'desc',
},
});
}
async downloadDocument(s3Key: string, userId: number): Promise<any> {
// Verify document exists and user has access
const document = await this.prisma.document.findFirst({
where: {
s3Key: s3Key,
sharedWithId: userId,
},
});
if (!document) {
throw new NotFoundException('Document not found or access denied');
}
try {
// Get document stream from S3
const fileStream = await this.s3Service.getObject(s3Key).createReadStream();
return fileStream;
} catch (error) {
console.error('Error downloading document:', error);
throw new NotFoundException('Document file not found in storage');
}
}
async getAllDocuments(): Promise<Document[]> {
return this.prisma.document.findMany({
include: {
sharedWith: {
select: {
id: true,
name: true,
email: true,
},
},
},
orderBy: {
createdAt: 'desc',
},
});
}
}
async shareDocument(documentId: number, userId: number): Promise<Document> {
const document = await this.prisma.document.update({
where: { id: documentId },
data: {
sharedWithId: userId,
},
include: {
sharedWith: {
select: {
id: true,
name: true,
email: true,
},
},
},
});
return document;
}
async updateStatus(documentId: number, status: string): Promise<Document> {
return this.prisma.document.update({
where: { id: documentId },
data: { status },
include: {
sharedWith: {
select: {
id: true,
name: true,
email: true,
},
},
},
});
}
}
+8 -58
View File
@@ -1,48 +1,4 @@
// import { Injectable } from '@nestjs/common';
// import {
// S3Client,
// PutObjectCommand,
// DeleteObjectCommand,
// } from '@aws-sdk/client-s3';
// import { Upload } from '@aws-sdk/lib-storage';
// @Injectable()
// export class S3Service {
// private s3Client: S3Client;
// constructor() {
// this.s3Client = new S3Client({
// region: process.env.AWS_REGION,
// credentials: {
// accessKeyId: process.env.AWS_ACCESS_KEY_ID,
// secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
// },
// });
// }
// async uploadFile(file: Express.Multer.File, key: string): Promise<string> {
// const upload = new Upload({
// client: this.s3Client,
// params: {
// Bucket: process.env.AWS_S3_BUCKET_NAME,
// Key: key,
// Body: file.buffer,
// },
// });
// await upload.done();
// return key;
// }
// async deleteFile(key: string): Promise<void> {
// const command = new DeleteObjectCommand({
// Bucket: process.env.AWS_S3_BUCKET_NAME,
// Key: key,
// });
// await this.s3Client.send(command);
// }
// }
import { Injectable, Logger } from '@nestjs/common';
import {
S3Client,
@@ -58,6 +14,7 @@ import { ConfigService } from '@nestjs/config';
export class S3Service {
private s3Client: S3Client;
private readonly logger = new Logger(S3Service.name);
s3: any;
constructor(private configService: ConfigService) {
this.s3Client = new S3Client({
@@ -71,20 +28,7 @@ export class S3Service {
});
}
// async uploadFile(file: Express.Multer.File, key: string): Promise<string> {
// const upload = new Upload({
// client: this.s3Client,
// params: {
// Bucket: this.configService.get('AWS_S3_BUCKET_NAME'),
// Key: key,
// Body: file.buffer,
// ContentType: file.mimetype,
// },
// });
// await upload.done();
// return key;
// }
async uploadFile(file: Express.Multer.File, key: string): Promise<string> {
try {
const uniqueKey = `${Date.now()}-${key}`; // Ensure unique keys
@@ -115,6 +59,12 @@ export class S3Service {
await this.s3Client.send(command);
}
getObject(key: string) {
return this.s3.getObject({
Bucket: process.env.AWS_S3_BUCKET,
Key: key,
});
}
async getFileUrl(key: string): Promise<string> {
const command = new GetObjectCommand({
+64
View File
@@ -0,0 +1,64 @@
{ nixpkgs, prisma-factory }:
with nixpkgs;
let
hashesBySystem = {
x86_64-linux = {
prisma-fmt-hash = "sha256-4zsJv0PW8FkGfiiv/9g0y5xWNjmRWD8Q2l2blSSBY3s=";
query-engine-hash = "sha256-6ILWB6ZmK4ac6SgAtqCkZKHbQANmcqpWO92U8CfkFzw=";
libquery-engine-hash = "sha256-n9IimBruqpDJStlEbCJ8nsk8L9dDW95ug+gz9DHS1Lc=";
schema-engine-hash = "sha256-j38xSXOBwAjIdIpbSTkFJijby6OGWCoAx+xZyms/34Q=";
};
aarch64-linux = {
prisma-fmt-hash = "sha256-gqbgN9pZxzZEi6cBicUfH7qqlXWM+z28sGVuW/wKHb8=";
query-engine-hash = "sha256-q1HVbRtWhF3J5ScETrwvGisS8fXA27nryTvqFb+XIuo=";
libquery-engine-hash = "sha256-oalG9QKuxURtdgs5DgJZZtyWMz3ZpywHlov+d1ct2vA=";
schema-engine-hash = "sha256-5bp8iiq6kc9c37G8dNKVHKWJHvaxFaetR4DOR/0/eWs=";
};
};
test-npm =
let
prisma =
(prisma-factory ({ inherit nixpkgs; } // hashesBySystem.${nixpkgs.system})).fromNpmLock
./npm/package-lock.json;
in
writeShellApplication {
name = "test-npm";
text = ''
echo "testing npm"
${prisma.shellHook}
cd npm
npm ci
./node_modules/.bin/prisma generate
'';
};
test-pnpm =
let
prisma =
(prisma-factory ({ inherit nixpkgs; } // hashesBySystem.${nixpkgs.system})).fromPnpmLock
./pnpm/pnpm-lock.yaml;
in
writeShellApplication {
name = "test-pnpm";
text = ''
echo "testing pnpm"
${prisma.shellHook}
cd pnpm
pnpm install
./node_modules/.bin/prisma generate
'';
};
test-all = writeShellApplication {
name = "test";
runtimeInputs = [
test-pnpm
test-npm
];
text = ''
test-npm
test-pnpm
'';
};
in
{
inherit test-npm test-pnpm test-all;
}