( value: ToFileInput | PromiseLike<ToFileInput>, name?: string | null | undefined, options?: FilePropertyBag | undefined )
| 368 | * @returns a {@link File} with the given properties |
| 369 | */ |
| 370 | export async function toFile( |
| 371 | value: ToFileInput | PromiseLike<ToFileInput>, |
| 372 | name?: string | null | undefined, |
| 373 | options?: FilePropertyBag | undefined |
| 374 | ): Promise<FileLike> { |
| 375 | // If it's a promise, resolve it. |
| 376 | value = await value; |
| 377 | |
| 378 | // Use the file's options if there isn't one provided |
| 379 | options ??= isFileLike(value) ? { lastModified: value.lastModified, type: value.type } : {}; |
| 380 | |
| 381 | if (isResponseLike(value)) { |
| 382 | const blob = await value.blob(); |
| 383 | name ||= new URL(value.url).pathname.split(/[\\/]/).pop() ?? "unknown_file"; |
| 384 | |
| 385 | return new File([blob as any], name, options); |
| 386 | } |
| 387 | |
| 388 | const bits = await getBytes(value); |
| 389 | |
| 390 | name ||= getName(value) ?? "unknown_file"; |
| 391 | |
| 392 | if (!options.type) { |
| 393 | const type = (bits[0] as any)?.type; |
| 394 | if (typeof type === "string") { |
| 395 | options = { ...options, type }; |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | return new File(bits, name, options); |
| 400 | } |
| 401 | |
| 402 | function getName(value: any): string | undefined { |
| 403 | return ( |
no test coverage detected
searching dependent graphs…