Skip to content

Commit

Permalink
fix(blocks): switch the file-type to @sgtpooki/file-type (#6803)
Browse files Browse the repository at this point in the history
Fix toeverything/AFFiNE#6603

This pull request switches the [file-type](https://github.com/sindresorhus/file-type) to [@sgtpooki/file-type](https://github.com/sgtpooki/file-type).

It also includes other minor changes and updates.

~~The `file-type` is no longer useful since the toeverything/AFFiNE#3245 resolved in toeverything/AFFiNE#5565

---

More context see toeverything/AFFiNE#3245 #3555 #4818 #5372 sindresorhus/file-type#578

---

## Caveat

The [@sgtpooki/file-type](https://github.com/sgtpooki/file-type) is an unofficial fork package from the community. It may need to be source code audited before updating.

Also, thanks to sgtpooki for their strong contribution to @sgtpooki/file-type
  • Loading branch information
lawvs committed Apr 22, 2024
1 parent 6abcda1 commit 7664f3e
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 53 deletions.
3 changes: 1 addition & 2 deletions packages/blocks/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,14 @@
"@dotlottie/player-component": "^2.7.12",
"@fal-ai/serverless-client": "^0.9.3",
"@floating-ui/dom": "^1.6.3",
"@sgtpooki/file-type": "1.0.1",
"@toeverything/theme": "^0.7.29",
"@types/hast": "^3.0.4",
"@types/mdast": "^4.0.3",
"@types/sortablejs": "^1.15.8",
"@types/webfontloader": "^1.6.38",
"buffer": "^6.0.3",
"date-fns": "^3.6.0",
"dompurify": "^3.1.0",
"file-type": "^16.5.4",
"fractional-indexing": "^3.2.0",
"html2canvas": "^1.4.1",
"jszip": "^3.10.1",
Expand Down
22 changes: 18 additions & 4 deletions packages/blocks/src/image-block/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,11 @@ async function getImageBlob(model: ImageBlockModel) {
if (!blob.type) {
const buffer = await blob.arrayBuffer();

const FileType = await import('file-type/browser.js');
const fileType = await FileType.fromBuffer(buffer);
// Switch to the original file-type package after https://github.com/sindresorhus/file-type/issues/578 is fixed
// The [@sgtpooki/file-type](https://github.com/sgtpooki/file-type) is a fork of the original file-type package
// Please check the commit before updating the package
const FileType = await import('@sgtpooki/file-type');
const fileType = await FileType.fileTypeFromBuffer(buffer);
if (!fileType?.mime.startsWith('image/')) {
return null;
}
Expand Down Expand Up @@ -216,6 +219,7 @@ export async function copyImageBlob(blockElement: ImageBlockComponent) {
const { host, model } = blockElement;
let blob = await getImageBlob(model);
if (!blob) {
console.error('Failed to get image blob');
return;
}

Expand All @@ -229,10 +233,20 @@ export async function copyImageBlob(blockElement: ImageBlockComponent) {
} else {
// DOMException: Type image/jpeg not supported on write.
if (blob.type !== 'image/png') {
blob = await convertToPng(blob);
const pngBlob = await convertToPng(blob);
if (!pngBlob) {
console.error('Failed to convert blob to PNG');
return;
}
blob = pngBlob;
}

if (!blob) {
if (!globalThis.isSecureContext) {
console.error(
'Clipboard API is not available in insecure context',
blob.type,
blob
);
return;
}

Expand Down
1 change: 0 additions & 1 deletion packages/presets/vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ export default defineConfig(_configEnv =>
esbuild: { target: 'es2022' },
optimizeDeps: {
force: true,
include: ['@blocksuite/blocks > buffer'],
esbuildOptions: {
// Vitest hardcodes the esbuild target to es2020,
// override it to es2022 for top level await.
Expand Down
76 changes: 30 additions & 46 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7664f3e

Please sign in to comment.