MCPcopy Create free account
hub / github.com/ScattrdBlade/bigFileUpload / normalizeUploadBlob

Function normalizeUploadBlob

utils/upload.ts:1472–1511  ·  view source on GitHub ↗
(blob: Blob, sourceUrl?: string)

Source from the content-addressed store, hash-verified

1470}
1471
1472async 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
1513async function uploadPreparedBlob(blob: Blob, sourceUrl?: string, forceSend?: boolean): Promise<string> {
1514 const primary = settings.store.serviceType as ServiceType;

Callers 1

uploadPreparedBlobFunction · 0.85

Calls 6

getUrlExtensionFunction · 0.90
getExtensionFromBytesFunction · 0.90
getExtensionFromMimeFunction · 0.90
convertApngToGifFunction · 0.90
getMimeFromExtensionFunction · 0.90
getFilenameExtensionFunction · 0.85

Tested by

no test coverage detected