diff --git a/packages/backend/src/misc/download-url.ts b/packages/backend/src/misc/download-url.ts index 079e84e8a..a1db5817b 100644 --- a/packages/backend/src/misc/download-url.ts +++ b/packages/backend/src/misc/download-url.ts @@ -1,3 +1,4 @@ +import { URL } from 'node:url'; import * as fs from 'node:fs'; import * as stream from 'node:stream'; import * as util from 'node:util'; @@ -20,6 +21,7 @@ export async function downloadUrl(url: string, path: string): Promise { const timeout = 30 * SECOND; const operationTimeout = MINUTE; const maxSize = config.maxFileSize || 262144000; + let filename = new URL(url).pathname.split('/').pop() || null; const req = got.stream(url, { headers: { @@ -51,6 +53,7 @@ export async function downloadUrl(url: string, path: string): Promise { } const contentLength = res.headers['content-length']; + filename = res.headers['content-disposition'].split('=').pop().replace(/"/g, ''); if (contentLength != null) { const size = Number(contentLength); if (size > maxSize) { @@ -76,6 +79,7 @@ export async function downloadUrl(url: string, path: string): Promise { } logger.succ(`Download finished: ${chalk.cyan(url)}`); + return filename; } function isPrivateIp(ip: string): boolean { diff --git a/packages/backend/src/services/drive/upload-from-url.ts b/packages/backend/src/services/drive/upload-from-url.ts index a815d8374..04d948b0e 100644 --- a/packages/backend/src/services/drive/upload-from-url.ts +++ b/packages/backend/src/services/drive/upload-from-url.ts @@ -41,7 +41,7 @@ export async function uploadFromUrl({ try { // write content at URL to temp file - await downloadUrl(url, path); + name = await downloadUrl(url, path); const driveFile = await addFile({ user,