{"version":3,"file":"presignedPut.cjs","sources":["../../../../../src/components/forms/uploads/presignedPut.ts"],"sourcesContent":["import type { PresignedUploadResult } from \"@/types\";\n\n/**\n * Upload a file to a presigned PUT URL, reporting progress via XHR.\n *\n * The uploaders never hold bucket credentials: the consumer's\n * `getPresignedUrl(file)` asks its own server for a short-lived URL, and this\n * sends the bytes. `contentType` and any `headers` must match what the server\n * signed, so they are sent verbatim.\n */\nexport function uploadToPresignedUrl(\n  url: string,\n  file: File,\n  contentType: string,\n  onProgress: (pct: number) => void,\n  headers: Record<string, string> = {}\n): Promise<void> {\n  return new Promise((resolve, reject) => {\n    const xhr = new XMLHttpRequest();\n\n    xhr.upload.onprogress = (e) => {\n      if (e.lengthComputable) {\n        onProgress(Math.round((e.loaded / e.total) * 100));\n      }\n    };\n\n    xhr.onload = () => {\n      if (xhr.status >= 200 && xhr.status < 300) {\n        resolve();\n      } else {\n        reject(new Error(`Upload failed: HTTP ${xhr.status}`));\n      }\n    };\n\n    xhr.onerror = () => reject(new Error(\"Network error during upload\"));\n    xhr.ontimeout = () => reject(new Error(\"Upload timed out\"));\n\n    xhr.open(\"PUT\", url);\n    // Use the server-validated content type, never trust file.type alone.\n    xhr.setRequestHeader(\"Content-Type\", contentType);\n    // Any other headers the signature covers (e.g. x-amz-acl) must match exactly.\n    Object.entries(headers)\n      .filter(([name]) => name.toLowerCase() !== \"content-type\")\n      .forEach(([name, value]) => xhr.setRequestHeader(name, value));\n    xhr.send(file);\n  });\n}\n\n/** Presign with the consumer's function, then PUT. Resolves with the presign result. */\nexport async function presignAndUpload(\n  file: File,\n  getPresignedUrl: (file: File) => Promise<PresignedUploadResult>,\n  onProgress: (pct: number) => void\n): Promise<PresignedUploadResult> {\n  const presigned = await getPresignedUrl(file);\n  await uploadToPresignedUrl(\n    presigned.url,\n    file,\n    presigned.contentType,\n    onProgress,\n    presigned.headers\n  );\n  return presigned;\n}\n\nexport const UPLOADS_NOT_CONFIGURED =\n  \"Uploads are not configured: pass getPresignedUrl (a function that asks your server for a presigned PUT URL).\";\n"],"names":["uploadToPresignedUrl","url","file","contentType","onProgress","headers","arguments","length","undefined","Promise","resolve","reject","xhr","XMLHttpRequest","upload","onprogress","e","lengthComputable","Math","round","loaded","total","onload","status","Error","concat","onerror","ontimeout","open","setRequestHeader","Object","entries","filter","_ref","_slicedToArray","toLowerCase","forEach","_ref3","_ref4","name","value","send","_presignAndUpload","_asyncToGenerator","_regenerator","m","_callee","getPresignedUrl","presigned","w","_context","n","v","a","apply","this","_x","_x2","_x3"],"mappings":"wIAUgB,SAAAA,EACdC,EACAC,EACAC,EACAC,GACoC,IAApCC,EAAAC,UAAAC,OAAA,QAAAC,IAAAF,UAAA,GAAAA,UAAA,GAAkC,CAAA,EAElC,OAAO,IAAIG,QAAQ,SAACC,EAASC,GAC3B,IAAMC,EAAM,IAAIC,eAEhBD,EAAIE,OAAOC,WAAa,SAACC,GACnBA,EAAEC,kBACJb,EAAWc,KAAKC,MAAOH,EAAEI,OAASJ,EAAEK,MAAS,OAIjDT,EAAIU,OAAS,WACPV,EAAIW,QAAU,KAAOX,EAAIW,OAAS,IACpCb,IAEAC,EAAO,IAAIa,MAAKC,uBAAAA,OAAwBb,EAAIW,WAIhDX,EAAIc,QAAU,WAAA,OAAMf,EAAO,IAAIa,MAAM,+BAA+B,EACpEZ,EAAIe,UAAY,WAAA,OAAMhB,EAAO,IAAIa,MAAM,oBAAoB,EAE3DZ,EAAIgB,KAAK,MAAO3B,GAEhBW,EAAIiB,iBAAiB,eAAgB1B,GAErC2B,OAAOC,QAAQ1B,GACZ2B,OAAO,SAAAC,GAAM,MAA6B,iBAAnCC,EAAAA,cAAAD,EAAA,GAAM,GAAWE,aAAgC,GACxDC,QAAQ,SAAAC,GAAA,IAAAC,EAAAJ,EAAAA,cAAAG,EAAA,GAAEE,EAAID,EAAA,GAAEE,EAAKF,EAAA,GAAA,OAAM1B,EAAIiB,iBAAiBU,EAAMC,KACzD5B,EAAI6B,KAAKvC,EACX,EACF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