( app: FilesRouteHost, ix: IInstantiationService, )
| 71 | } |
| 72 | |
| 73 | export function registerFilesRoutes( |
| 74 | app: FilesRouteHost, |
| 75 | ix: IInstantiationService, |
| 76 | ): void { |
| 77 | |
| 78 | app.register(multipart, { |
| 79 | limits: { |
| 80 | fileSize: DEFAULT_MAX_UPLOAD_BYTES, |
| 81 | files: 1, |
| 82 | }, |
| 83 | }); |
| 84 | |
| 85 | const uploadRoute = defineRoute( |
| 86 | { |
| 87 | method: 'POST', |
| 88 | path: '/files', |
| 89 | success: { data: uploadFileResponseSchema }, |
| 90 | consumes: ['multipart/form-data'], |
| 91 | description: 'Upload a file', |
| 92 | tags: ['files'], |
| 93 | }, |
| 94 | async (req, reply) => { |
| 95 | try { |
| 96 | const fastifyReq = req as unknown as FastifyRequestLike; |
| 97 | if (!fastifyReq.file) { |
| 98 | reply.send( |
| 99 | errEnvelope( |
| 100 | ErrorCode.VALIDATION_FAILED, |
| 101 | 'multipart not initialized', |
| 102 | req.id, |
| 103 | ), |
| 104 | ); |
| 105 | return; |
| 106 | } |
| 107 | const part = await fastifyReq.file(); |
| 108 | if (!part) { |
| 109 | reply.send( |
| 110 | errEnvelope( |
| 111 | ErrorCode.VALIDATION_FAILED, |
| 112 | 'missing `file` field', |
| 113 | req.id, |
| 114 | ), |
| 115 | ); |
| 116 | return; |
| 117 | } |
| 118 | |
| 119 | const nameOverride = readFieldString(part.fields['name']); |
| 120 | const expiresInSec = readFieldNumber(part.fields['expires_in_sec']); |
| 121 | |
| 122 | const store = ix.invokeFunction((a) => a.get(IFileStore)); |
| 123 | |
| 124 | const partFile = part.file as NodeJS.ReadableStream & { truncated?: boolean }; |
| 125 | let busboyTruncated = false; |
| 126 | partFile.on('limit', () => { |
| 127 | busboyTruncated = true; |
| 128 | }); |
| 129 | try { |
| 130 | const meta = await store.save( |
no test coverage detected