feat(core): pdf preview (#8569)

Co-authored-by: forehalo <forehalo@gmail.com>
This commit is contained in:
Fangdun Tsai
2024-11-12 19:12:31 +08:00
committed by GitHub
parent 73283df3e1
commit f4abe39689
46 changed files with 1968 additions and 95 deletions

View File

@@ -0,0 +1,42 @@
import { fileTypeFromBuffer } from 'file-type';
export async function resourceUrlToBlob(
url: string
): Promise<Blob | undefined> {
const buffer = await fetch(url).then(response => response.arrayBuffer());
if (!buffer) {
console.warn('Could not get blob');
return;
}
try {
const type = await fileTypeFromBuffer(buffer);
if (!type) {
return;
}
const blob = new Blob([buffer], { type: type.mime });
return blob;
} catch (error) {
console.error('Error converting resource to blob', error);
}
return;
}
export async function downloadBlob(blob: Blob, filename: string) {
const blobUrl = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = blobUrl;
a.download = filename;
document.body.append(a);
a.click();
a.remove();
URL.revokeObjectURL(blobUrl);
}
export async function downloadResourceWithUrl(url: string, filename: string) {
// given input url may not have correct mime type
const blob = await resourceUrlToBlob(url);
if (!blob) return;
await downloadBlob(blob, filename);
}