|
|
|
|
@ -1,14 +1,15 @@
|
|
|
|
|
import { BadRequestException, Controller, Get, Param, Post, Req, Res, UploadedFile, UseGuards, UseInterceptors } from '@nestjs/common';
|
|
|
|
|
import { BadRequestException, Controller, Delete, Get, NotFoundException, Param, Post, Req, Res, UploadedFile, UseGuards, UseInterceptors } from '@nestjs/common';
|
|
|
|
|
import { AuthGuard } from '@nestjs/passport';
|
|
|
|
|
import { FileInterceptor } from '@nestjs/platform-express';
|
|
|
|
|
import { existsSync, mkdirSync } from 'fs';
|
|
|
|
|
import { diskStorage } from 'multer';
|
|
|
|
|
import { extname } from 'path';
|
|
|
|
|
|
|
|
|
|
import { GetUser } from 'src/auth/dto/get-user.decorator';
|
|
|
|
|
import { FileUploadService } from './file-upload.service';
|
|
|
|
|
import * as fs from 'fs';
|
|
|
|
|
import * as archiver from 'archiver';
|
|
|
|
|
import * as path from 'path';
|
|
|
|
|
import { join, extname } from 'path';
|
|
|
|
|
|
|
|
|
|
export function createFolders(filePath: string) {
|
|
|
|
|
|
|
|
|
|
@ -33,20 +34,23 @@ export function createFolders(filePath: string) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Controller('file-upload')
|
|
|
|
|
|
|
|
|
|
export class FileUploadController {
|
|
|
|
|
constructor(
|
|
|
|
|
private iservice: FileUploadService
|
|
|
|
|
) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Post('user-documents/:id')
|
|
|
|
|
@UseGuards(AuthGuard())
|
|
|
|
|
@Post('user-documents/:id/:userId')
|
|
|
|
|
@UseInterceptors(FileInterceptor('file', {
|
|
|
|
|
storage: diskStorage({
|
|
|
|
|
destination: (req, res, cb) => {
|
|
|
|
|
const filePath: string = './uploads/userDocuments/'+req.params.id;
|
|
|
|
|
createFolders(filePath);
|
|
|
|
|
return cb(null, filePath);
|
|
|
|
|
const parentFolder: string = './uploads/userDocuments/' + req.params.id;
|
|
|
|
|
createFolders(parentFolder);
|
|
|
|
|
const childFolder: string = parentFolder + '/' + req.params.userId;
|
|
|
|
|
createFolders(childFolder);
|
|
|
|
|
|
|
|
|
|
return cb(null, childFolder);
|
|
|
|
|
},
|
|
|
|
|
filename: (req, file, cb) => {
|
|
|
|
|
console.log('===here===file upload==');
|
|
|
|
|
@ -58,16 +62,18 @@ export class FileUploadController {
|
|
|
|
|
userDoc(@UploadedFile() file) {
|
|
|
|
|
return { fileName: file.filename, filePath: file.path };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@UseGuards(AuthGuard())
|
|
|
|
|
@Post('Bank-documents/:id')
|
|
|
|
|
@UseInterceptors(FileInterceptor('file', {
|
|
|
|
|
storage: diskStorage({
|
|
|
|
|
// destination: './uploads/cars',
|
|
|
|
|
destination: (req, res, cb) => {
|
|
|
|
|
// console.log(req);
|
|
|
|
|
|
|
|
|
|
const filePath: string = './uploads/BankDocuments/'+req.params.id;
|
|
|
|
|
console.log(req);
|
|
|
|
|
|
|
|
|
|
const filePath: string = './uploads/BankDocuments/' + req.params.id;
|
|
|
|
|
createFolders(filePath);
|
|
|
|
|
return cb(null, filePath);
|
|
|
|
|
},
|
|
|
|
|
@ -87,15 +93,15 @@ export class FileUploadController {
|
|
|
|
|
return { fileName: file.filename, filePath: file.path, fileSizeKB };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@UseGuards(AuthGuard())
|
|
|
|
|
@Post('drawings/:id')
|
|
|
|
|
@UseInterceptors(FileInterceptor('file', {
|
|
|
|
|
storage: diskStorage({
|
|
|
|
|
// destination: './uploads/cars',
|
|
|
|
|
destination: (req, res, cb) => {
|
|
|
|
|
// console.log(req);
|
|
|
|
|
|
|
|
|
|
const filePath: string = './uploads/Drawings/'+req.params.id;
|
|
|
|
|
|
|
|
|
|
const filePath: string = './uploads/Drawings/' + req.params.id;
|
|
|
|
|
createFolders(filePath);
|
|
|
|
|
return cb(null, filePath);
|
|
|
|
|
},
|
|
|
|
|
@ -115,10 +121,106 @@ export class FileUploadController {
|
|
|
|
|
return { fileName: file.filename, filePath: file.path, fileSizeKB };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@UseGuards(AuthGuard())
|
|
|
|
|
@Delete('R-drawing/:id/:filename')
|
|
|
|
|
async deleteDrawing(
|
|
|
|
|
@Param('filename') filename : string,
|
|
|
|
|
@Param('id') id:string): Promise<any> {
|
|
|
|
|
const filePath = './uploads/Drawings/'+id+'/'+ filename;
|
|
|
|
|
console.log("filepath===",filePath);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
await fs.promises.unlink(filePath);
|
|
|
|
|
return { message: 'File deleted successfully.' };
|
|
|
|
|
} catch (err) {
|
|
|
|
|
throw new NotFoundException('File not found.');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@UseGuards(AuthGuard())
|
|
|
|
|
@Delete('R-userDoc/:id/:userId/:filename')
|
|
|
|
|
async deleteUserDoc(
|
|
|
|
|
@Param('userId') userId:string,
|
|
|
|
|
@Param('filename') filename : string,
|
|
|
|
|
@Param('id') id:string): Promise<any> {
|
|
|
|
|
const filePath = './uploads/userDocuments/'+id+'/'+userId +'/'+ filename;
|
|
|
|
|
console.log("filepath===",filePath);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
await fs.promises.unlink(filePath);
|
|
|
|
|
return { message: 'File deleted successfully.' };
|
|
|
|
|
} catch (err) {
|
|
|
|
|
throw new NotFoundException('File not found.');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@UseGuards(AuthGuard())
|
|
|
|
|
@Delete('R-notice/:id/:filename')
|
|
|
|
|
async deleteNotice(
|
|
|
|
|
@Param('filename') filename : string,
|
|
|
|
|
@Param('id') id:string): Promise<any> {
|
|
|
|
|
const filePath = './uploads/Notice/'+id +'/'+ filename;
|
|
|
|
|
console.log("filepath===",filePath);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
await fs.promises.unlink(filePath);
|
|
|
|
|
return { message: 'File deleted successfully.' };
|
|
|
|
|
} catch (err) {
|
|
|
|
|
throw new NotFoundException('File not found.');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@UseGuards(AuthGuard())
|
|
|
|
|
@Post('notice/:id')
|
|
|
|
|
@UseInterceptors(FileInterceptor('file', {
|
|
|
|
|
storage: diskStorage({
|
|
|
|
|
// destination: './uploads/cars',
|
|
|
|
|
destination: (req, res, cb) => {
|
|
|
|
|
// console.log(req);
|
|
|
|
|
|
|
|
|
|
const filePath: string = './uploads/Notice/' + req.params.id;
|
|
|
|
|
createFolders(filePath);
|
|
|
|
|
return cb(null, filePath);
|
|
|
|
|
},
|
|
|
|
|
filename: (req, file, cb) => {
|
|
|
|
|
console.log('===here===file upload==');
|
|
|
|
|
const randomName = Array(32).fill(null).map(() => (Math.round(Math.random() * 16)).toString(16)).join('');
|
|
|
|
|
return cb(null, `${randomName}${extname(file.originalname)}`);
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}))
|
|
|
|
|
|
|
|
|
|
notice(@UploadedFile() file) {
|
|
|
|
|
if (!file) {
|
|
|
|
|
return { error: 'No file uploaded.' };
|
|
|
|
|
}
|
|
|
|
|
//To return the size of the document in kb
|
|
|
|
|
const fileSizeKB = Math.ceil(file.size / 1024);
|
|
|
|
|
return { fileName: file.filename, filePath: file.path, fileSizeKB };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Get('notice/:id/:imgpath')
|
|
|
|
|
getNotice(
|
|
|
|
|
@Param('imgpath') image: string,
|
|
|
|
|
@Param('id') id: string,
|
|
|
|
|
@Res() res,
|
|
|
|
|
@Req() req,
|
|
|
|
|
) {
|
|
|
|
|
return res.sendFile(image, { root: './uploads/Notice/' + id }, function (err) {
|
|
|
|
|
if (err) {
|
|
|
|
|
console.log(err.status);
|
|
|
|
|
res.status(err.status).end();
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@UseGuards(AuthGuard())
|
|
|
|
|
@Get('Bank-Documents/:id/:imgpath')
|
|
|
|
|
getBankDocuments(
|
|
|
|
|
@Param('imgpath') image: string,
|
|
|
|
|
@ -127,7 +229,7 @@ export class FileUploadController {
|
|
|
|
|
@Req() req,
|
|
|
|
|
|
|
|
|
|
) {
|
|
|
|
|
return res.sendFile(image, { root: './uploads/BankDocuments/'+id}, function (err) {
|
|
|
|
|
return res.sendFile(image, { root: './uploads/BankDocuments/' + id }, function (err) {
|
|
|
|
|
if (err) {
|
|
|
|
|
console.log(err.status);
|
|
|
|
|
res.status(err.status).end();
|
|
|
|
|
@ -135,15 +237,17 @@ export class FileUploadController {
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Get('user-Documents/:id/:imgpath')
|
|
|
|
|
@UseGuards(AuthGuard())
|
|
|
|
|
@Get('user-Documents/:id/:userId/:imgpath')
|
|
|
|
|
getUserDocuments(
|
|
|
|
|
@Param('imgpath') image: string,
|
|
|
|
|
@Param('userId') userId: string,
|
|
|
|
|
@Param('id') id: string,
|
|
|
|
|
@Res() res,
|
|
|
|
|
@Req() req,
|
|
|
|
|
|
|
|
|
|
) {
|
|
|
|
|
return res.sendFile(image, { root: './uploads/userDocuments/'+id}, function (err) {
|
|
|
|
|
return res.sendFile(image, { root: './uploads/userDocuments/' + id + '/' + userId }, function (err) {
|
|
|
|
|
if (err) {
|
|
|
|
|
console.log(err.status);
|
|
|
|
|
res.status(err.status).end();
|
|
|
|
|
@ -151,119 +255,178 @@ export class FileUploadController {
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@UseGuards(AuthGuard())
|
|
|
|
|
@Get('Bank-Documents-Arc/:id')
|
|
|
|
|
async getBankDocumentsAsZip(
|
|
|
|
|
@Param('id') id: string,
|
|
|
|
|
@Res() res,
|
|
|
|
|
@Req() req,
|
|
|
|
|
) {
|
|
|
|
|
) {
|
|
|
|
|
const folderPath = path.join('./uploads/BankDocuments', id);
|
|
|
|
|
if (!fs.existsSync(folderPath)) {
|
|
|
|
|
res.status(404).send({ error: 'File not found' });
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
res.status(404).send({ error: 'File not found' });
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Uncomment when we want tender id as the zip file name
|
|
|
|
|
//const zipFileName = id + '.zip';
|
|
|
|
|
const zipFileName = 'tenderDocuments.zip';
|
|
|
|
|
const zipPath = path.join('./uploads/BankDocuments', zipFileName);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Create a new archive
|
|
|
|
|
const archive = archiver('zip', {
|
|
|
|
|
zlib: { level: 9 } // Set compression level (optional)
|
|
|
|
|
zlib: { level: 9 } // Set compression level (optional)
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Create a write stream to the zip file
|
|
|
|
|
const output = fs.createWriteStream(zipPath);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Pipe the archive to the write stream
|
|
|
|
|
archive.pipe(output);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Add the entire folder to the archive
|
|
|
|
|
archive.directory(folderPath, id);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Finalize the archive
|
|
|
|
|
await archive.finalize();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Set the appropriate headers for downloading the zip file
|
|
|
|
|
res.attachment(zipFileName);
|
|
|
|
|
res.setHeader('Content-Type', 'application/zip');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Stream the zip file to the response
|
|
|
|
|
const readStream = fs.createReadStream(zipPath);
|
|
|
|
|
readStream.pipe(res);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Remove the temporary zip file once it's streamed
|
|
|
|
|
readStream.on('end', () => {
|
|
|
|
|
fs.unlinkSync(zipPath);
|
|
|
|
|
fs.unlinkSync(zipPath);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Handle any errors that occur during the archiving process
|
|
|
|
|
archive.on('error', function (err) {
|
|
|
|
|
console.log(err);
|
|
|
|
|
res.status(500).send({ error: 'Failed to create the zip file' });
|
|
|
|
|
console.log(err);
|
|
|
|
|
res.status(500).send({ error: 'Failed to create the zip file' });
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@UseGuards(AuthGuard())
|
|
|
|
|
@Get('Drawings-Arc/:id')
|
|
|
|
|
async getDrawingsAsZip(
|
|
|
|
|
@Param('id') id: string,
|
|
|
|
|
@Res() res,
|
|
|
|
|
@Req() req,
|
|
|
|
|
) {
|
|
|
|
|
) {
|
|
|
|
|
const folderPath = path.join('./uploads/Drawings', id);
|
|
|
|
|
if (!fs.existsSync(folderPath)) {
|
|
|
|
|
res.status(404).send({ error: 'File not found' });
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
res.status(404).send({ error: 'File not found' });
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Uncomment when we want tender id as the zip file name
|
|
|
|
|
//const zipFileName = id + '.zip';
|
|
|
|
|
const zipFileName = 'Drawings.zip';
|
|
|
|
|
const zipPath = path.join('./uploads/Drawings', zipFileName);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Create a new archive
|
|
|
|
|
const archive = archiver('zip', {
|
|
|
|
|
zlib: { level: 9 } // Set compression level (optional)
|
|
|
|
|
zlib: { level: 9 } // Set compression level (optional)
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Create a write stream to the zip file
|
|
|
|
|
const output = fs.createWriteStream(zipPath);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Pipe the archive to the write stream
|
|
|
|
|
archive.pipe(output);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Add the entire folder to the archive
|
|
|
|
|
archive.directory(folderPath, id);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Finalize the archive
|
|
|
|
|
await archive.finalize();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Set the appropriate headers for downloading the zip file
|
|
|
|
|
res.attachment(zipFileName);
|
|
|
|
|
res.setHeader('Content-Type', 'application/zip');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Stream the zip file to the response
|
|
|
|
|
const readStream = fs.createReadStream(zipPath);
|
|
|
|
|
readStream.pipe(res);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Remove the temporary zip file once it's streamed
|
|
|
|
|
readStream.on('end', () => {
|
|
|
|
|
fs.unlinkSync(zipPath);
|
|
|
|
|
fs.unlinkSync(zipPath);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Handle any errors that occur during the archiving process
|
|
|
|
|
archive.on('error', function (err) {
|
|
|
|
|
console.log(err);
|
|
|
|
|
res.status(500).send({ error: 'Failed to create the zip file' });
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@UseGuards(AuthGuard())
|
|
|
|
|
@Get('user-doc-arc/:id')
|
|
|
|
|
async getUserDocsAsZip(
|
|
|
|
|
@Param('id') id: string,
|
|
|
|
|
@Res() res,
|
|
|
|
|
@Req() req,
|
|
|
|
|
) {
|
|
|
|
|
const folderPath = path.join('./uploads/userDocuments', id);
|
|
|
|
|
if (!fs.existsSync(folderPath)) {
|
|
|
|
|
res.status(404).send({ error: 'File not found' });
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Uncomment when we want tender id as the zip file name
|
|
|
|
|
//const zipFileName = id + '.zip';
|
|
|
|
|
const zipFileName = 'userDocuments.zip';
|
|
|
|
|
const zipPath = path.join('./uploads/userDocuments', zipFileName);
|
|
|
|
|
|
|
|
|
|
// Create a new archive
|
|
|
|
|
const archive = archiver('zip', {
|
|
|
|
|
zlib: { level: 9 } // Set compression level (optional)
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Create a write stream to the zip file
|
|
|
|
|
const output = fs.createWriteStream(zipPath);
|
|
|
|
|
|
|
|
|
|
// Pipe the archive to the write stream
|
|
|
|
|
archive.pipe(output);
|
|
|
|
|
|
|
|
|
|
// Add the entire folder to the archive
|
|
|
|
|
archive.directory(folderPath, id);
|
|
|
|
|
|
|
|
|
|
// Finalize the archive
|
|
|
|
|
await archive.finalize();
|
|
|
|
|
|
|
|
|
|
// Set the appropriate headers for downloading the zip file
|
|
|
|
|
res.attachment(zipFileName);
|
|
|
|
|
res.setHeader('Content-Type', 'application/zip');
|
|
|
|
|
|
|
|
|
|
// Stream the zip file to the response
|
|
|
|
|
const readStream = fs.createReadStream(zipPath);
|
|
|
|
|
readStream.pipe(res);
|
|
|
|
|
|
|
|
|
|
// Remove the temporary zip file once it's streamed
|
|
|
|
|
readStream.on('end', () => {
|
|
|
|
|
fs.unlinkSync(zipPath);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Handle any errors that occur during the archiving process
|
|
|
|
|
archive.on('error', function (err) {
|
|
|
|
|
console.log(err);
|
|
|
|
|
res.status(500).send({ error: 'Failed to create the zip file' });
|
|
|
|
|
console.log(err);
|
|
|
|
|
res.status(500).send({ error: 'Failed to create the zip file' });
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|