From eb0d6ee08ed05cb9b8bf2cfa598ab5dbb9c765c3 Mon Sep 17 00:00:00 2001 From: Himself65 Date: Fri, 10 Mar 2023 02:21:12 -0600 Subject: [PATCH] fix: syncing images (#1501) --- apps/web/src/plugins/affine/index.tsx | 23 ++++++++++++++++++++++- apps/web/src/shared/apis.ts | 2 +- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/apps/web/src/plugins/affine/index.tsx b/apps/web/src/plugins/affine/index.tsx index ba599a6023..8cab219324 100644 --- a/apps/web/src/plugins/affine/index.tsx +++ b/apps/web/src/plugins/affine/index.tsx @@ -14,7 +14,7 @@ import { LoadPriority, RemWorkspaceFlavour, } from '../../shared'; -import { apis } from '../../shared/apis'; +import { apis, clientAuth } from '../../shared/apis'; import { createEmptyBlockSuiteWorkspace } from '../../utils'; import { WorkspacePlugin } from '..'; import { fetcher, QueryKey } from './fetcher'; @@ -68,6 +68,27 @@ export const AffinePlugin: WorkspacePlugin = { blockSuiteWorkspace.doc ); const { id } = await apis.createWorkspace(new Blob([binary.buffer])); + // fixme: syncing images + const newWorkspaceId = id; + + await new Promise(resolve => setTimeout(resolve, 1000)); + const blobs = await blockSuiteWorkspace.blobs; + if (blobs) { + const ids = await blobs.blobs; + for (const id of ids) { + const url = await blobs.get(id); + if (url) { + const blob = await fetch(url).then(res => res.blob()); + await clientAuth.put(`api/workspace/${newWorkspaceId}/blob`, { + body: blob, + headers: { + 'Content-Type': blob.type, + }, + }); + } + } + } + await mutate(matcher => matcher === QueryKey.getWorkspaces); // refresh the local storage await AffinePlugin.CRUD.list(); diff --git a/apps/web/src/shared/apis.ts b/apps/web/src/shared/apis.ts index 00b9a9283c..313740c7c7 100644 --- a/apps/web/src/shared/apis.ts +++ b/apps/web/src/shared/apis.ts @@ -22,5 +22,5 @@ if (typeof window === 'undefined') { const bareAuth = createBareClient(prefixUrl); const googleAuth = new GoogleAuth(bareAuth); -const clientAuth = createAuthClient(bareAuth, googleAuth); +export const clientAuth = createAuthClient(bareAuth, googleAuth); export const apis = getApis(bareAuth, clientAuth, googleAuth);