|
|
|
@ -14,6 +14,8 @@ import {
|
|
|
|
|
UseInterceptors, |
|
|
|
|
} from '@nestjs/common'; |
|
|
|
|
import { AnyFilesInterceptor } from '@nestjs/platform-express'; |
|
|
|
|
import { Request as RequestType, Response as ResponseType } from 'express'; |
|
|
|
|
import type { AttachmentReqType, FileType } from 'nocodb-sdk'; |
|
|
|
|
import { UploadAllowedInterceptor } from '~/interceptors/is-upload-allowed/is-upload-allowed.interceptor'; |
|
|
|
|
import { GlobalGuard } from '~/guards/global/global.guard'; |
|
|
|
|
import { AttachmentsService } from '~/services/attachments.service'; |
|
|
|
@ -29,13 +31,12 @@ export class AttachmentsController {
|
|
|
|
|
@HttpCode(200) |
|
|
|
|
@UseInterceptors(UploadAllowedInterceptor, AnyFilesInterceptor()) |
|
|
|
|
async upload( |
|
|
|
|
@UploadedFiles() files: Array<any>, |
|
|
|
|
@Body() body: any, |
|
|
|
|
@Request() req: any, |
|
|
|
|
@UploadedFiles() files: Array<FileType>, |
|
|
|
|
@Request() req: RequestType, |
|
|
|
|
) { |
|
|
|
|
const attachments = await this.attachmentsService.upload({ |
|
|
|
|
files: files, |
|
|
|
|
path: req.query?.path as string, |
|
|
|
|
path: req.query?.path?.toString(), |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
return attachments; |
|
|
|
@ -45,7 +46,10 @@ export class AttachmentsController {
|
|
|
|
|
@HttpCode(200) |
|
|
|
|
@UseInterceptors(UploadAllowedInterceptor) |
|
|
|
|
@UseGuards(MetaApiLimiterGuard, GlobalGuard) |
|
|
|
|
async uploadViaURL(@Body() body: any, @Query('path') path: string) { |
|
|
|
|
async uploadViaURL( |
|
|
|
|
@Body() body: Array<AttachmentReqType>, |
|
|
|
|
@Query('path') path: string, |
|
|
|
|
) { |
|
|
|
|
const attachments = await this.attachmentsService.uploadViaURL({ |
|
|
|
|
urls: body, |
|
|
|
|
path, |
|
|
|
@ -58,16 +62,17 @@ export class AttachmentsController {
|
|
|
|
|
// , getCacheMiddleware(), catchError(fileRead));
|
|
|
|
|
@Get('/download/:filename(*)') |
|
|
|
|
// This route will match any URL that starts with
|
|
|
|
|
async fileRead(@Param('filename') filename: string, @Response() res) { |
|
|
|
|
async fileRead( |
|
|
|
|
@Param('filename') filename: string, |
|
|
|
|
@Response() res: ResponseType, |
|
|
|
|
) { |
|
|
|
|
try { |
|
|
|
|
const { img, type } = await this.attachmentsService.fileRead({ |
|
|
|
|
const file = await this.attachmentsService.getFile({ |
|
|
|
|
path: path.join('nc', 'uploads', filename), |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
res.writeHead(200, { 'Content-Type': type }); |
|
|
|
|
res.end(img, 'binary'); |
|
|
|
|
res.sendFile(file.path); |
|
|
|
|
} catch (e) { |
|
|
|
|
console.log(e); |
|
|
|
|
res.status(404).send('Not found'); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
@ -79,10 +84,10 @@ export class AttachmentsController {
|
|
|
|
|
@Param('param1') param1: string, |
|
|
|
|
@Param('param2') param2: string, |
|
|
|
|
@Param('filename') filename: string, |
|
|
|
|
@Response() res, |
|
|
|
|
@Response() res: ResponseType, |
|
|
|
|
) { |
|
|
|
|
try { |
|
|
|
|
const { img, type } = await this.attachmentsService.fileRead({ |
|
|
|
|
const file = await this.attachmentsService.getFile({ |
|
|
|
|
path: path.join( |
|
|
|
|
'nc', |
|
|
|
|
param1, |
|
|
|
@ -92,23 +97,25 @@ export class AttachmentsController {
|
|
|
|
|
), |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
res.writeHead(200, { 'Content-Type': type }); |
|
|
|
|
res.end(img, 'binary'); |
|
|
|
|
res.sendFile(file.path); |
|
|
|
|
} catch (e) { |
|
|
|
|
res.status(404).send('Not found'); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Get('/dltemp/:param(*)') |
|
|
|
|
async fileReadv3(@Param('param') param: string, @Response() res) { |
|
|
|
|
async fileReadv3( |
|
|
|
|
@Param('param') param: string, |
|
|
|
|
@Response() res: ResponseType, |
|
|
|
|
) { |
|
|
|
|
try { |
|
|
|
|
const fpath = await PresignedUrl.getPath(`dltemp/${param}`); |
|
|
|
|
|
|
|
|
|
const { img } = await this.attachmentsService.fileRead({ |
|
|
|
|
const file = await this.attachmentsService.getFile({ |
|
|
|
|
path: path.join('nc', 'uploads', fpath), |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
res.sendFile(img); |
|
|
|
|
res.sendFile(file.path); |
|
|
|
|
} catch (e) { |
|
|
|
|
res.status(404).send('Not found'); |
|
|
|
|
} |
|
|
|
|