( params: T, fallbackFn?: (params: T) => string | Promise<string> )
| 349 | }; |
| 350 | |
| 351 | export const makeBlobURL = <T extends { blob: Blob; persistence: boolean }>( |
| 352 | params: T, |
| 353 | fallbackFn?: (params: T) => string | Promise<string> |
| 354 | ): Promise<string> | string => { |
| 355 | if (typeof URL?.createObjectURL !== "function") { |
| 356 | // 在service worker中,透过 offscreen 取得 blob URL |
| 357 | if (!fallbackFn) throw new Error("URL.createObjectURL is not supported"); |
| 358 | return fallbackFn(params); |
| 359 | } else { |
| 360 | const url = URL.createObjectURL(params.blob); |
| 361 | if (!params.persistence) { |
| 362 | // 如果不是持久化的,则在1分钟后释放 |
| 363 | setTimeout(() => { |
| 364 | URL.revokeObjectURL(url); |
| 365 | }, 60_000); |
| 366 | } |
| 367 | return url; |
| 368 | } |
| 369 | }; |
| 370 | |
| 371 | export function blobToBase64(blob: Blob): Promise<string> { |
| 372 | return new Promise((resolve) => { |
no outgoing calls
no test coverage detected