till download files, everything works
This commit is contained in:
1 parent
ce893a4185
commit
ed1a580b09
6050 files changed
+768543
-177
No files matched your search
@@ -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,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -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({
|
||||
|
||||
Reference in new issue
Block a user