mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-07-18 02:26:21 +08:00
refactor(infra): migrate to new infra (#5565)
This commit is contained in:
@@ -7,69 +7,70 @@ import {
|
||||
setBlobMutation,
|
||||
} from '@affine/graphql';
|
||||
import { fetcher } from '@affine/graphql';
|
||||
import type { BlobStorage } from '@affine/workspace';
|
||||
import { BlobStorageOverCapacity } from '@affine/workspace';
|
||||
import { type BlobStorage, BlobStorageOverCapacity } from '@toeverything/infra';
|
||||
import { isArray } from 'lodash-es';
|
||||
|
||||
import { bufferToBlob } from '../utils/buffer-to-blob';
|
||||
|
||||
export const createAffineCloudBlobStorage = (
|
||||
workspaceId: string
|
||||
): BlobStorage => {
|
||||
return {
|
||||
name: 'affine-cloud',
|
||||
readonly: false,
|
||||
get: async key => {
|
||||
const suffix = key.startsWith('/')
|
||||
? key
|
||||
: `/api/workspaces/${workspaceId}/blobs/${key}`;
|
||||
export class AffineCloudBlobStorage implements BlobStorage {
|
||||
constructor(private readonly workspaceId: string) {}
|
||||
|
||||
return fetchWithTraceReport(getBaseUrl() + suffix).then(async res => {
|
||||
if (!res.ok) {
|
||||
// status not in the range 200-299
|
||||
return null;
|
||||
name = 'affine-cloud';
|
||||
readonly = false;
|
||||
|
||||
async get(key: string) {
|
||||
const suffix = key.startsWith('/')
|
||||
? key
|
||||
: `/api/workspaces/${this.workspaceId}/blobs/${key}`;
|
||||
|
||||
return fetchWithTraceReport(getBaseUrl() + suffix).then(async res => {
|
||||
if (!res.ok) {
|
||||
// status not in the range 200-299
|
||||
return null;
|
||||
}
|
||||
return bufferToBlob(await res.arrayBuffer());
|
||||
});
|
||||
}
|
||||
|
||||
async set(key: string, value: Blob) {
|
||||
// set blob will check blob size & quota
|
||||
return await fetcher({
|
||||
query: setBlobMutation,
|
||||
variables: {
|
||||
workspaceId: this.workspaceId,
|
||||
blob: new File([value], key),
|
||||
},
|
||||
})
|
||||
.then(res => res.setBlob)
|
||||
.catch(err => {
|
||||
if (isArray(err)) {
|
||||
err.map(e => {
|
||||
if (e instanceof GraphQLError && e.extensions.code === 413) {
|
||||
throw new BlobStorageOverCapacity(e);
|
||||
} else throw e;
|
||||
});
|
||||
}
|
||||
return bufferToBlob(await res.arrayBuffer());
|
||||
throw err;
|
||||
});
|
||||
},
|
||||
set: async (key, value) => {
|
||||
// set blob will check blob size & quota
|
||||
return await fetcher({
|
||||
query: setBlobMutation,
|
||||
variables: {
|
||||
workspaceId,
|
||||
blob: new File([value], key),
|
||||
},
|
||||
})
|
||||
.then(res => res.setBlob)
|
||||
.catch(err => {
|
||||
if (isArray(err)) {
|
||||
err.map(e => {
|
||||
if (e instanceof GraphQLError && e.extensions.code === 413) {
|
||||
throw new BlobStorageOverCapacity(e);
|
||||
} else throw e;
|
||||
});
|
||||
}
|
||||
throw err;
|
||||
});
|
||||
},
|
||||
list: async () => {
|
||||
const result = await fetcher({
|
||||
query: listBlobsQuery,
|
||||
variables: {
|
||||
workspaceId,
|
||||
},
|
||||
});
|
||||
return result.listBlobs;
|
||||
},
|
||||
delete: async (key: string) => {
|
||||
await fetcher({
|
||||
query: deleteBlobMutation,
|
||||
variables: {
|
||||
workspaceId,
|
||||
hash: key,
|
||||
},
|
||||
});
|
||||
},
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
async delete(key: string) {
|
||||
await fetcher({
|
||||
query: deleteBlobMutation,
|
||||
variables: {
|
||||
workspaceId: key,
|
||||
hash: key,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async list() {
|
||||
const result = await fetcher({
|
||||
query: listBlobsQuery,
|
||||
variables: {
|
||||
workspaceId: this.workspaceId,
|
||||
},
|
||||
});
|
||||
return result.listBlobs;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user