diff --git a/apps/meteor/app/file-upload/server/config/AmazonS3.ts b/apps/meteor/app/file-upload/server/config/AmazonS3.ts index 1c4d8a838800..97604e2bc7c3 100644 --- a/apps/meteor/app/file-upload/server/config/AmazonS3.ts +++ b/apps/meteor/app/file-upload/server/config/AmazonS3.ts @@ -14,7 +14,14 @@ const get: FileUploadClass['get'] = async function (this: FileUploadClass, file, const forceDownload = typeof query.download !== 'undefined'; const fileUrl = await this.store.getRedirectURL(file, forceDownload); - if (!fileUrl || !file.store) { + if (!fileUrl || !file.store || fileUrl === 'https://s3.us-west-2.amazonaws.com/') { + // If the credentials fail, getRedirectURL can return the string above, + // which just 307s (incidentally to https://aws.amazon.com/s3/). + // Proxying that 307 (in proxyFile(), below) just sends an empty body and closes the connection, + // causing us to falsey return an empty file. + // HACK: just fail 500 and hopefully it will succeed the next time around. + res.setHeader('x-rc-debug-s3url', fileUrl || ''); + res.writeHead(500); res.end(); return; } diff --git a/apps/meteor/app/file-upload/server/lib/FileUpload.ts b/apps/meteor/app/file-upload/server/lib/FileUpload.ts index 9864bd4a4bd6..704d0111b904 100644 --- a/apps/meteor/app/file-upload/server/lib/FileUpload.ts +++ b/apps/meteor/app/file-upload/server/lib/FileUpload.ts @@ -562,7 +562,21 @@ export const FileUpload = { ) { res.setHeader('Content-Disposition', `${forceDownload ? 'attachment' : 'inline'}; filename="${encodeURI(fileName)}"`); - request.get(fileUrl, (fileRes) => fileRes.pipe(res)); + request.get(fileUrl, (fileRes) => { + if (fileRes.statusCode !== 200) { + res.setHeader('x-rc-proxyfile-status', fileRes.statusCode); + res.setHeader('content-length', 0); + res.writeHead(500); + res.end(); + return; + } + + ['content-length', 'content-type'].forEach((header) => { + fileRes.headers[header] && res.setHeader(header, fileRes.headers[header]); + }); + + fileRes.pipe(res); + }); }, generateJWTToFileUrls({ rid, userId, fileId }: { rid: string; userId: string; fileId: string }) {