-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Factorize and add public-assets/*path endpoint #18080
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,17 @@ | ||
| import { | ||
| Controller, | ||
| Get, | ||
| Param, | ||
| Req, | ||
| Res, | ||
| UseFilters, | ||
| UseGuards, | ||
| } from '@nestjs/common'; | ||
|
|
||
| import { join } from 'path'; | ||
|
|
||
| import { Request, Response } from 'express'; | ||
| import { FileFolder } from 'twenty-shared/types'; | ||
|
|
||
| import { | ||
| FileStorageException, | ||
|
|
@@ -23,13 +27,63 @@ import { FilePathGuard } from 'src/engine/core-modules/file/guards/file-path-gua | |
| import { FileService } from 'src/engine/core-modules/file/services/file.service'; | ||
| import { extractFileInfoFromRequest } from 'src/engine/core-modules/file/utils/extract-file-info-from-request.utils'; | ||
| import { NoPermissionGuard } from 'src/engine/guards/no-permission.guard'; | ||
| import { PublicEndpointGuard } from 'src/engine/guards/public-endpoint.guard'; | ||
| import { | ||
| FileByIdGuard, | ||
| SupportedFileFolder, | ||
| } from 'src/engine/core-modules/file/guards/file-by-id.guard'; | ||
|
|
||
| @Controller('files') | ||
| @Controller() | ||
| @UseFilters(FileApiExceptionFilter) | ||
| export class FileController { | ||
| constructor(private readonly fileService: FileService) {} | ||
|
|
||
| @Get('*path') | ||
| @Get('public-assets/:workspaceId/:applicationId/*path') | ||
| @UseGuards(PublicEndpointGuard, NoPermissionGuard) | ||
| async getPublicAssets( | ||
| @Res() res: Response, | ||
| @Req() req: Request, | ||
| @Param('workspaceId') workspaceId: string, | ||
| @Param('applicationId') | ||
| applicationId: string, | ||
| ) { | ||
| const filepath = join(...req.params.path); | ||
|
|
||
| try { | ||
| const fileStream = await this.fileService.getFileStreamByPath({ | ||
| workspaceId, | ||
| applicationId, | ||
| fileFolder: FileFolder.PublicAsset, | ||
| filepath, | ||
| }); | ||
|
|
||
| fileStream.on('error', () => { | ||
| throw new FileException( | ||
| 'Error streaming file from storage', | ||
| FileExceptionCode.INTERNAL_SERVER_ERROR, | ||
| ); | ||
| }); | ||
|
Comment on lines
+60
to
+65
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug: Throwing an exception within the Suggested FixReplace the Prompt for AI Agent |
||
|
|
||
| fileStream.pipe(res); | ||
| } catch (error) { | ||
| if ( | ||
| error instanceof FileStorageException && | ||
| error.code === FileStorageExceptionCode.FILE_NOT_FOUND | ||
| ) { | ||
| throw new FileException( | ||
| 'File not found', | ||
| FileExceptionCode.FILE_NOT_FOUND, | ||
| ); | ||
| } | ||
|
|
||
| throw new FileException( | ||
| `Error retrieving file: ${error.message}`, | ||
| FileExceptionCode.INTERNAL_SERVER_ERROR, | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| @Get('files/*path') | ||
| @UseGuards(FilePathGuard, NoPermissionGuard) | ||
| async getFile(@Res() res: Response, @Req() req: Request) { | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
|
|
@@ -69,4 +123,48 @@ export class FileController { | |
| ); | ||
| } | ||
| } | ||
|
|
||
| @Get('file/:fileFolder/:id') | ||
| @UseGuards(FileByIdGuard, NoPermissionGuard) | ||
| async getFileById( | ||
| @Res() res: Response, | ||
| @Req() req: Request, | ||
| @Param('fileFolder') fileFolder: SupportedFileFolder, | ||
| @Param('id') fileId: string, | ||
| ) { | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| const workspaceId = (req as any)?.workspaceId; | ||
|
|
||
| try { | ||
| const fileStream = await this.fileService.getFileStreamById({ | ||
| fileId, | ||
| workspaceId, | ||
| fileFolder, | ||
| }); | ||
|
|
||
| fileStream.on('error', () => { | ||
| throw new FileException( | ||
| 'Error streaming file from storage', | ||
| FileExceptionCode.INTERNAL_SERVER_ERROR, | ||
| ); | ||
| }); | ||
|
|
||
| fileStream.pipe(res); | ||
| } catch (error) { | ||
| if ( | ||
| error instanceof FileStorageException && | ||
| error.code === FileStorageExceptionCode.FILE_NOT_FOUND | ||
| ) { | ||
| throw new FileException( | ||
| 'File not found', | ||
| FileExceptionCode.FILE_NOT_FOUND, | ||
| ); | ||
| } | ||
|
|
||
| throw new FileException( | ||
| `Error retrieving file: ${error.message}`, | ||
| FileExceptionCode.INTERNAL_SERVER_ERROR, | ||
| ); | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.