Merge toBlobURL and toBlobURLWithProgress
This commit is contained in:
parent
ec183935fe
commit
99f984a150
@ -95,24 +95,6 @@ export const importScript = async (url: string): Promise<void> =>
|
|||||||
document.getElementsByTagName("head")[0].appendChild(script);
|
document.getElementsByTagName("head")[0].appendChild(script);
|
||||||
});
|
});
|
||||||
|
|
||||||
/**
|
|
||||||
* toBlobURL fetches data from an URL and return a blob URL.
|
|
||||||
*
|
|
||||||
* Example:
|
|
||||||
*
|
|
||||||
* ```ts
|
|
||||||
* await toBlobURL("http://localhost:3000/ffmpeg.js", "text/javascript");
|
|
||||||
* ```
|
|
||||||
*/
|
|
||||||
export const toBlobURL = async (
|
|
||||||
url: string,
|
|
||||||
mimeType: string
|
|
||||||
): Promise<string> => {
|
|
||||||
const buf = await (await fetch(url)).arrayBuffer();
|
|
||||||
const blob = new Blob([buf], { type: mimeType });
|
|
||||||
return URL.createObjectURL(blob);
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Download content of a URL with progress.
|
* Download content of a URL with progress.
|
||||||
*
|
*
|
||||||
@ -121,7 +103,7 @@ export const toBlobURL = async (
|
|||||||
*/
|
*/
|
||||||
export const downloadWithProgress = async (
|
export const downloadWithProgress = async (
|
||||||
url: string | URL,
|
url: string | URL,
|
||||||
cb: ProgressCallback
|
cb?: ProgressCallback
|
||||||
): Promise<ArrayBuffer> => {
|
): Promise<ArrayBuffer> => {
|
||||||
const resp = await fetch(url);
|
const resp = await fetch(url);
|
||||||
let buf;
|
let buf;
|
||||||
@ -141,13 +123,13 @@ export const downloadWithProgress = async (
|
|||||||
|
|
||||||
if (done) {
|
if (done) {
|
||||||
if (total != -1 && total !== received) throw ERROR_INCOMPLETED_DOWNLOAD;
|
if (total != -1 && total !== received) throw ERROR_INCOMPLETED_DOWNLOAD;
|
||||||
cb({ url, total, received, delta, done });
|
cb && cb({ url, total, received, delta, done });
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
chunks.push(value);
|
chunks.push(value);
|
||||||
received += delta;
|
received += delta;
|
||||||
cb({ url, total, received, delta, done });
|
cb && cb({ url, total, received, delta, done });
|
||||||
}
|
}
|
||||||
|
|
||||||
const data = new Uint8Array(received);
|
const data = new Uint8Array(received);
|
||||||
@ -162,29 +144,37 @@ export const downloadWithProgress = async (
|
|||||||
console.log(`failed to send download progress event: `, e);
|
console.log(`failed to send download progress event: `, e);
|
||||||
// Fetch arrayBuffer directly when it is not possible to get progress.
|
// Fetch arrayBuffer directly when it is not possible to get progress.
|
||||||
buf = await resp.arrayBuffer();
|
buf = await resp.arrayBuffer();
|
||||||
cb({
|
cb &&
|
||||||
url,
|
cb({
|
||||||
total: buf.byteLength,
|
url,
|
||||||
received: buf.byteLength,
|
total: buf.byteLength,
|
||||||
delta: 0,
|
received: buf.byteLength,
|
||||||
done: true,
|
delta: 0,
|
||||||
});
|
done: true,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
return buf;
|
return buf;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Convert an URL to an Blob URL to avoid issues like CORS.
|
* toBlobURL fetches data from an URL and return a blob URL.
|
||||||
|
*
|
||||||
|
* Example:
|
||||||
|
*
|
||||||
|
* ```ts
|
||||||
|
* await toBlobURL("http://localhost:3000/ffmpeg.js", "text/javascript");
|
||||||
|
* ```
|
||||||
*/
|
*/
|
||||||
export const toBlobURLWithProgress = async (
|
export const toBlobURL = async (
|
||||||
url: string,
|
url: string,
|
||||||
/** mime type like `text/javascript` and `application/wasm` */
|
|
||||||
mimeType: string,
|
mimeType: string,
|
||||||
cb: ProgressCallback
|
progress = false,
|
||||||
): Promise<string> =>
|
cb?: ProgressCallback
|
||||||
URL.createObjectURL(
|
): Promise<string> => {
|
||||||
new Blob([await downloadWithProgress(url, cb)], {
|
const buf = progress
|
||||||
type: mimeType,
|
? await downloadWithProgress(url, cb)
|
||||||
})
|
: await (await fetch(url)).arrayBuffer();
|
||||||
);
|
const blob = new Blob([buf], { type: mimeType });
|
||||||
|
return URL.createObjectURL(blob);
|
||||||
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user