refactor(infra): migrate to new infra (#5565)

This commit is contained in:
EYHN
2024-01-30 07:16:39 +00:00
parent 1e3499c323
commit 329fc19852
170 changed files with 2007 additions and 4354 deletions
@@ -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;
}
}