(blob: Blob, sourceUrl?: string)
| 1470 | } |
| 1471 | |
| 1472 | async function normalizeUploadBlob(blob: Blob, sourceUrl?: string): Promise<{ blob: Blob; filename: string; originalFilename: string; }> { |
| 1473 | let sourceFileName = ""; |
| 1474 | if (blob instanceof File && blob.name) { |
| 1475 | sourceFileName = blob.name; |
| 1476 | } else if (sourceUrl && URL.canParse(sourceUrl)) { |
| 1477 | const segment = new URL(sourceUrl).pathname.split("/").pop(); |
| 1478 | if (segment) sourceFileName = decodeURIComponent(segment); |
| 1479 | } |
| 1480 | |
| 1481 | const extGuessFromSource = sourceUrl ? getUrlExtension(sourceUrl) : undefined; |
| 1482 | let ext = await getExtensionFromBytes(blob) |
| 1483 | || getExtensionFromMime(blob.type) |
| 1484 | || getFilenameExtension(sourceFileName) |
| 1485 | || extGuessFromSource |
| 1486 | || "bin"; |
| 1487 | |
| 1488 | if (ext === "apng" && settings.store.apngToGif) { |
| 1489 | const gifBlob = await convertApngToGif(blob); |
| 1490 | if (gifBlob) { |
| 1491 | blob = gifBlob; |
| 1492 | ext = "gif"; |
| 1493 | } else { |
| 1494 | showToast("APNG to GIF conversion failed, uploading as APNG", Toasts.Type.FAILURE); |
| 1495 | } |
| 1496 | } |
| 1497 | |
| 1498 | const mimeType = getMimeFromExtension(ext); |
| 1499 | const { preserveOriginalFilename } = settings.store; |
| 1500 | let filename = `upload.${ext}`; |
| 1501 | if (preserveOriginalFilename && sourceFileName) { |
| 1502 | const dotIndex = sourceFileName.lastIndexOf("."); |
| 1503 | filename = `${sourceFileName.slice(0, dotIndex < 0 ? undefined : dotIndex)}.${ext}`; |
| 1504 | } |
| 1505 | |
| 1506 | return { |
| 1507 | blob: new Blob([blob], { type: mimeType }), |
| 1508 | filename, |
| 1509 | originalFilename: sourceFileName |
| 1510 | }; |
| 1511 | } |
| 1512 | |
| 1513 | async function uploadPreparedBlob(blob: Blob, sourceUrl?: string, forceSend?: boolean): Promise<string> { |
| 1514 | const primary = settings.store.serviceType as ServiceType; |
no test coverage detected