| 1 | import { |
| 2 | Controller, |
| 3 | Get, |
| 4 | Param, |
| 5 | Post, |
| 6 | Query, |
| 7 | Req, |
| 8 | Res, |
| 9 | Session, |
| 10 | StreamableFile, |
| 11 | } from '@nestjs/common'; |
| 12 | import { |
| 13 | ApiBadRequestResponse, |
| 14 | ApiBody, |
| 15 | ApiConsumes, |
| 16 | ApiCreatedResponse, |
| 17 | ApiNotFoundResponse, |
| 18 | ApiOkResponse, |
| 19 | ApiOperation, |
| 20 | ApiPayloadTooLargeResponse, |
| 21 | ApiTags, |
| 22 | ApiUnsupportedMediaTypeResponse, |
| 23 | } from '@nestjs/swagger'; |
| 24 | import { OptionalAuth, type |
| 25 | import type { Request, |
| 26 | |
| 27 | import { ErrorResponseDTO } |
| 28 | import { isInlineAttachment } |
| 29 | import { AttachmentsService } |
| 30 | import { |
| 31 | AttachmentDTO, |
| 32 | UploadAttachmentQueryDTO, |
| 33 | } from './dto/attachment.dto.js'; |
| 34 | |
| 35 | @Controller() |
| 36 | @ApiTags('Attachments') |
| 37 | export class AttachmentsController |
| 38 | constructor(private readonly |
| 39 | |
| 40 | @Post('repositories/:username/:repo/attachments' |
| 41 | @ApiOperation({ |
| 42 | summary: 'Attach a file', |
| 43 | description: |
| 44 | "Needs read access. Takes the raw file bytes as `application/octet-stream` with a `Content-Length`. Embed the file by linking `/api/attachments/{id}` in an issue, pull request, comment or release; one no text in the repository mentions is removed after a day. The file counts against the uploader's asset quota, when the instance sets one.", |
| 45 | }) |
| 46 | @ApiConsumes('application/octet-stream' |
| 47 | @ApiBody({ schema |
| 48 | @ApiCreatedResponse({ type |
| 49 | @ApiBadRequestResponse({ |
| 50 | description: 'The file name is not a plain file name.', |
| 51 | type: ErrorResponseDTO, |
| 52 | }) |
| 53 | @ApiNotFoundResponse({ type |
| 54 | @ApiPayloadTooLargeResponse({ |
| 55 | description: |
| 56 | 'The file is over the per-file limit, or would take the uploader over their storage quota.', |
| 57 | type: ErrorResponseDTO, |
| 58 | }) |
| 59 | @ApiUnsupportedMediaTypeResponse({ |
| 60 | description: |
| 61 | 'The body was not sent as `application/octet-stream`, or files with this extension cannot be attached.', |
| 62 | type: ErrorResponseDTO, |
| 63 | }) |
| 64 | uploadAttachment( |
| 65 | @Param('username' |
| 66 | @Param('repo' |
| 67 | @Query() query |
| 68 | // read off the request rather than `@Headers()`, which would publish them as parameters a browser is not allowed to set |
| 69 | @Req() request |
| 70 | @Session() session |
| 71 | ): Promise<AttachmentDTO |
| 72 | return this.attachments |
| 73 | username, |
| 74 | repo, |
| 75 | requesterId: session.user |
| 76 | name: query.name |
| 77 | bodyType: request.headers |
| 78 | contentLength: request.headers |
| 79 | body: request, |
| 80 | }); |
| 81 | } |
| 82 | |
| 83 | @Get('attachments/:id' |
| 84 | @OptionalAuth() |
| 85 | @ApiOperation({ |
| 86 | summary: 'Download an attachment', |
| 87 | description: |
| 88 | 'Readable by anyone who can read the repository it was attached in. Images are served inline, anything else as a download.', |
| 89 | }) |
| 90 | @ApiOkResponse({ |
| 91 | content: { |
| 92 | 'application/octet-stream': { |
| 93 | schema: { type: ' |
| 94 | }, |
| 95 | }, |
| 96 | }) |
| 97 | @ApiNotFoundResponse({ type |
| 98 | async downloadAttachment( |
| 99 | @Param('id' |
| 100 | @Session() session |
| 101 | @Res({ passthrough |
| 102 | ): Promise<StreamableFile |
| 103 | const attachment = |
| 104 | attachmentId, |
| 105 | requesterId: session?.user |
| 106 | }); |
| 107 | |
| 108 | const disposition = |
| 109 | ? 'inline' |
| 110 | : 'attachment'; |
| 111 | response.set({ |
| 112 | 'Content-Type': attachment. |
| 113 | 'Content-Length': String( |
| 114 | 'Content-Disposition': `${disposition |
| 115 | // the bytes are user-controlled: never sniffed, never scripted |
| 116 | 'X-Content-Type-Options': 'nosniff', |
| 117 | 'Content-Security-Policy': "default-src 'none'; sandbox", |
| 118 | 'Cache-Control': 'private, max-age=3600', |
| 119 | }); |
| 120 | |
| 121 | return new StreamableFile |
| 122 | } |
| 123 | } |