{"version":3,"sources":["../../../src/uploads/endpoints/getFileFromURL.ts"],"sourcesContent":["import type { PayloadHandler } from '../../config/types.js'\n\nimport { executeAccess } from '../../auth/executeAccess.js'\nimport { APIError } from '../../errors/APIError.js'\nimport { Forbidden } from '../../errors/Forbidden.js'\nimport { getRequestCollectionWithID } from '../../utilities/getRequestEntity.js'\nimport { isURLAllowed } from '../../utilities/isURLAllowed.js'\n\n// If doc id is provided, it means we are updating the doc\n// /:collectionSlug/paste-url/:doc-id?src=:fileUrl\n\n// If doc id is not provided, it means we are creating a new doc\n// /:collectionSlug/paste-url?src=:fileUrl\n\nexport const getFileFromURLHandler: PayloadHandler = async (req) => {\n const { id, collection } = getRequestCollectionWithID(req, { optionalID: true })\n\n if (!req.user) {\n throw new Forbidden(req.t)\n }\n\n const config = collection?.config\n\n if (id) {\n // updating doc\n const accessResult = await executeAccess({ req }, config.access.update)\n if (!accessResult) {\n throw new Forbidden(req.t)\n }\n } else {\n // creating doc\n const accessResult = await executeAccess({ req }, config.access?.create)\n if (!accessResult) {\n throw new Forbidden(req.t)\n }\n }\n try {\n if (!req.url) {\n throw new APIError('Request URL is missing.', 400)\n }\n\n const { searchParams } = new URL(req.url)\n const src = searchParams.get('src')\n\n if (!src || typeof src !== 'string') {\n throw new APIError('A valid URL string is required.', 400)\n }\n\n const validatedUrl = new URL(src)\n\n if (\n typeof config.upload?.pasteURL === 'object' &&\n !isURLAllowed(validatedUrl.href, config.upload.pasteURL.allowList)\n ) {\n throw new APIError(`The provided URL (${validatedUrl.href}) is not allowed.`, 400)\n }\n\n // Fetch the file with no compression\n const response = await fetch(validatedUrl.href, {\n headers: {\n 'Accept-Encoding': 'identity',\n },\n })\n\n if (!response.ok) {\n throw new APIError(`Failed to fetch file from ${validatedUrl.href}`, response.status)\n }\n\n const decodedFileName = decodeURIComponent(validatedUrl.pathname.split('/').pop() || '')\n\n return new Response(response.body, {\n headers: {\n 'Content-Disposition': `attachment; filename=\"${decodedFileName}\"`,\n 'Content-Length': response.headers.get('content-length') || '',\n 'Content-Type': response.headers.get('content-type') || 'application/octet-stream',\n },\n })\n } catch (err) {\n throw new APIError(\n `Error fetching file: ${err instanceof Error ? err.message : 'Unknown error'}`,\n 500,\n )\n }\n}\n"],"names":["executeAccess","APIError","Forbidden","getRequestCollectionWithID","isURLAllowed","getFileFromURLHandler","req","id","collection","optionalID","user","t","config","accessResult","access","update","create","url","searchParams","URL","src","get","validatedUrl","upload","pasteURL","href","allowList","response","fetch","headers","ok","status","decodedFileName","decodeURIComponent","pathname","split","pop","Response","body","err","Error","message"],"mappings":"AAEA,SAASA,aAAa,QAAQ,8BAA6B;AAC3D,SAASC,QAAQ,QAAQ,2BAA0B;AACnD,SAASC,SAAS,QAAQ,4BAA2B;AACrD,SAASC,0BAA0B,QAAQ,sCAAqC;AAChF,SAASC,YAAY,QAAQ,kCAAiC;AAE9D,0DAA0D;AAC1D,kDAAkD;AAElD,gEAAgE;AAChE,0CAA0C;AAE1C,OAAO,MAAMC,wBAAwC,OAAOC;IAC1D,MAAM,EAAEC,EAAE,EAAEC,UAAU,EAAE,GAAGL,2BAA2BG,KAAK;QAAEG,YAAY;IAAK;IAE9E,IAAI,CAACH,IAAII,IAAI,EAAE;QACb,MAAM,IAAIR,UAAUI,IAAIK,CAAC;IAC3B;IAEA,MAAMC,SAASJ,YAAYI;IAE3B,IAAIL,IAAI;QACN,eAAe;QACf,MAAMM,eAAe,MAAMb,cAAc;YAAEM;QAAI,GAAGM,OAAOE,MAAM,CAACC,MAAM;QACtE,IAAI,CAACF,cAAc;YACjB,MAAM,IAAIX,UAAUI,IAAIK,CAAC;QAC3B;IACF,OAAO;QACL,eAAe;QACf,MAAME,eAAe,MAAMb,cAAc;YAAEM;QAAI,GAAGM,OAAOE,MAAM,EAAEE;QACjE,IAAI,CAACH,cAAc;YACjB,MAAM,IAAIX,UAAUI,IAAIK,CAAC;QAC3B;IACF;IACA,IAAI;QACF,IAAI,CAACL,IAAIW,GAAG,EAAE;YACZ,MAA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