fix(core): svg blob syncing issue (#4886)

This commit is contained in:
Peng Xiao
2023-11-10 13:32:51 +08:00
committed by GitHub
parent 2117d6b232
commit 5c2d958e2b
6 changed files with 120 additions and 19 deletions

View File

@@ -9,6 +9,7 @@ import { fetcher } from '@affine/workspace/affine/gql';
import type { BlobStorage } from '@blocksuite/store';
import { predefinedStaticFiles } from './local-static-storage';
import { bufferToBlob } from './util';
export const createCloudBlobStorage = (workspaceId: string): BlobStorage => {
return {
@@ -17,15 +18,15 @@ export const createCloudBlobStorage = (workspaceId: string): BlobStorage => {
const suffix = predefinedStaticFiles.includes(key)
? `/static/${key}`
: `/api/workspaces/${workspaceId}/blobs/${key}`;
return fetchWithTraceReport(
runtimeConfig.serverUrlPrefix + suffix
).then(res => {
).then(async res => {
if (!res.ok) {
// status not in the range 200-299
return null;
}
// todo: shall we add svg type here if it is missing?
return res.blob();
return bufferToBlob(await res.arrayBuffer());
});
},
set: async (key, value) => {

View File

@@ -1,5 +1,7 @@
import type { BlobStorage } from '@blocksuite/store';
import { bufferToBlob } from './util';
export const predefinedStaticFiles = [
'029uztLz2CzJezK7UUhrbGiWUdZ0J7NVs_qR6RDsvb8=',
'047ebf2c9a5c7c9d8521c2ea5e6140ff7732ef9e28a9f944e9bf3ca4',
@@ -40,17 +42,21 @@ export const createStaticStorage = (): BlobStorage => {
return {
crud: {
get: async (key: string) => {
if (key.startsWith('/static/')) {
const response = await fetch(key);
if (response.ok) {
return response.blob();
}
} else if (predefinedStaticFiles.includes(key)) {
const response = await fetch(`/static/${key}`);
if (response.ok) {
return response.blob();
}
const isStaticResource =
predefinedStaticFiles.includes(key) || key.startsWith('/static/');
if (!isStaticResource) {
return null;
}
const path = key.startsWith('/static/') ? key : `/static/${key}`;
const response = await fetch(path);
if (response.ok) {
const buffer = await response.arrayBuffer();
return bufferToBlob(buffer);
}
return null;
},
set: async (key: string) => {

View File

@@ -1,7 +1,7 @@
import { assertExists } from '@blocksuite/global/utils';
import type { BlobStorage } from '@blocksuite/store';
import { isSvgBuffer } from './util';
import { bufferToBlob } from './util';
export const createSQLiteStorage = (workspaceId: string): BlobStorage => {
const apis = window.apis;
@@ -11,11 +11,7 @@ export const createSQLiteStorage = (workspaceId: string): BlobStorage => {
get: async (key: string) => {
const buffer = await apis.db.getBlob(workspaceId, key);
if (buffer) {
const isSVG = isSvgBuffer(buffer);
// for svg blob, we need to explicitly set the type to image/svg+xml
return isSVG
? new Blob([buffer], { type: 'image/svg+xml' })
: new Blob([buffer]);
return bufferToBlob(buffer);
}
return null;
},

View File

@@ -7,3 +7,13 @@ export function isSvgBuffer(buffer: Uint8Array) {
const str = decoder.decode(buffer);
return isSvg(str);
}
export function bufferToBlob(buffer: Uint8Array | ArrayBuffer) {
const isSVG = isSvgBuffer(
buffer instanceof ArrayBuffer ? new Uint8Array(buffer) : buffer
);
// for svg blob, we need to explicitly set the type to image/svg+xml
return isSVG
? new Blob([buffer], { type: 'image/svg+xml' })
: new Blob([buffer]);
}