-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
418 additions
and
310 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 55 additions & 32 deletions
87
packages/frontend/component/src/components/attachment-viewer/utils.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,63 @@ | ||
import { fileTypeFromBuffer } from 'file-type'; | ||
import type { AttachmentBlockModel } from '@blocksuite/affine/blocks'; | ||
|
||
export async function saveBufferToFile(url: string, filename: string) { | ||
// given input url may not have correct mime type | ||
const blob = await attachmentUrlToBlob(url); | ||
if (!blob) { | ||
return; | ||
export async function getAttachmentBlob(model: AttachmentBlockModel) { | ||
const sourceId = model.sourceId; | ||
if (!sourceId) { | ||
return null; | ||
} | ||
|
||
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 attachmentUrlToBlob( | ||
url: string | ||
): Promise<Blob | undefined> { | ||
const buffer = await fetch(url).then(response => response.arrayBuffer()); | ||
const doc = model.doc; | ||
let blob = await doc.blobSync.get(sourceId); | ||
|
||
if (!buffer) { | ||
console.warn('Could not get blob'); | ||
return; | ||
if (blob) { | ||
blob = new Blob([blob], { type: model.type }); | ||
} | ||
try { | ||
const type = await fileTypeFromBuffer(buffer); | ||
if (!type) { | ||
|
||
return blob; | ||
} | ||
|
||
export function download(model: AttachmentBlockModel) { | ||
(async () => { | ||
const blob = await getAttachmentBlob(model); | ||
if (!blob) { | ||
return; | ||
} | ||
const blob = new Blob([buffer], { type: type.mime }); | ||
return blob; | ||
} catch (error) { | ||
console.error('Error converting attachment to blob', error); | ||
} | ||
return; | ||
|
||
const blobUrl = URL.createObjectURL(blob); | ||
const a = document.createElement('a'); | ||
a.href = blobUrl; | ||
a.download = model.name; | ||
document.body.append(a); | ||
a.click(); | ||
a.remove(); | ||
URL.revokeObjectURL(blobUrl); | ||
})().catch(console.error); | ||
} | ||
|
||
export function renderItem( | ||
scroller: HTMLElement | null, | ||
id: number, | ||
imageBitmap: ImageBitmap | ||
) { | ||
if (!scroller) return; | ||
|
||
const wrapper = scroller.querySelector(`[data-index="${id}"]`); | ||
if (!wrapper) return; | ||
|
||
const item = wrapper.firstElementChild; | ||
if (!item) return; | ||
if (item.firstElementChild) return; | ||
|
||
const canvas = document.createElement('canvas'); | ||
const ctx = canvas.getContext('bitmaprenderer'); | ||
if (!ctx) return; | ||
|
||
canvas.width = imageBitmap.width; | ||
canvas.height = imageBitmap.height; | ||
canvas.style.width = '100%'; | ||
canvas.style.height = '100%'; | ||
|
||
ctx.transferFromImageBitmap(imageBitmap); | ||
|
||
item.append(canvas); | ||
} |
Oops, something went wrong.