feat(nbstore): add cloud implementation (#8810)

This commit is contained in:
forehalo
2024-12-10 10:48:27 +00:00
parent 1721875ab6
commit 2f80b4f822
32 changed files with 1030 additions and 315 deletions
@@ -0,0 +1,72 @@
import {
deleteBlobMutation,
gqlFetcherFactory,
listBlobsQuery,
releaseDeletedBlobsMutation,
setBlobMutation,
} from '@affine/graphql';
import { DummyConnection } from '../../connection';
import { type BlobRecord, BlobStorage } from '../../storage';
export class CloudBlobStorage extends BlobStorage {
private readonly gql = gqlFetcherFactory(this.options.peer + '/graphql');
override connection = new DummyConnection();
override async get(key: string) {
const res = await fetch(
this.options.peer + '/api/workspaces/' + this.spaceId + '/blobs/' + key,
{ cache: 'default' }
);
if (!res.ok) {
return null;
}
const data = await res.arrayBuffer();
return {
key,
data: new Uint8Array(data),
mime: res.headers.get('content-type') || '',
size: data.byteLength,
createdAt: new Date(res.headers.get('last-modified') || Date.now()),
};
}
override async set(blob: BlobRecord) {
await this.gql({
query: setBlobMutation,
variables: {
workspaceId: this.spaceId,
blob: new File([blob.data], blob.key, { type: blob.mime }),
},
});
}
override async delete(key: string, permanently: boolean) {
await this.gql({
query: deleteBlobMutation,
variables: { workspaceId: this.spaceId, key, permanently },
});
}
override async release() {
await this.gql({
query: releaseDeletedBlobsMutation,
variables: { workspaceId: this.spaceId },
});
}
override async list() {
const res = await this.gql({
query: listBlobsQuery,
variables: { workspaceId: this.spaceId },
});
return res.workspace.blobs.map(blob => ({
...blob,
createdAt: new Date(blob.createdAt),
}));
}
}