mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-14 21:27:20 +00:00
fix(server): avoid get object content when syncing (#9402)
This commit is contained in:
@@ -32,7 +32,7 @@ export class WorkspaceBlobStorage {
|
||||
const meta: PutObjectMetadata = autoMetadata(blob);
|
||||
|
||||
await this.provider.put(`${workspaceId}/${key}`, blob, meta);
|
||||
this.trySyncBlobMeta(workspaceId, key, {
|
||||
await this.upsert(workspaceId, key, {
|
||||
contentType: meta.contentType ?? 'application/octet-stream',
|
||||
contentLength: blob.length,
|
||||
lastModified: new Date(),
|
||||
@@ -119,53 +119,48 @@ export class WorkspaceBlobStorage {
|
||||
|
||||
private trySyncBlobsMeta(workspaceId: string, blobs: ListObjectsMetadata[]) {
|
||||
for (const blob of blobs) {
|
||||
this.trySyncBlobMeta(workspaceId, blob.key);
|
||||
this.event.emit('workspace.blob.sync', {
|
||||
workspaceId,
|
||||
key: blob.key,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private trySyncBlobMeta(
|
||||
private async upsert(
|
||||
workspaceId: string,
|
||||
key: string,
|
||||
meta?: GetObjectMetadata
|
||||
meta: GetObjectMetadata
|
||||
) {
|
||||
setImmediate(() => {
|
||||
this.syncBlobMeta(workspaceId, key, meta).catch(() => {
|
||||
/* never throw */
|
||||
});
|
||||
await this.db.blob.upsert({
|
||||
where: {
|
||||
workspaceId_key: {
|
||||
workspaceId,
|
||||
key,
|
||||
},
|
||||
},
|
||||
update: {
|
||||
mime: meta.contentType,
|
||||
size: meta.contentLength,
|
||||
},
|
||||
create: {
|
||||
workspaceId,
|
||||
key,
|
||||
mime: meta.contentType,
|
||||
size: meta.contentLength,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
private async syncBlobMeta(
|
||||
workspaceId: string,
|
||||
key: string,
|
||||
meta?: GetObjectMetadata
|
||||
) {
|
||||
@OnEvent('workspace.blob.sync')
|
||||
async syncBlobMeta({
|
||||
workspaceId,
|
||||
key,
|
||||
}: EventPayload<'workspace.blob.sync'>) {
|
||||
try {
|
||||
if (!meta) {
|
||||
const blob = await this.get(workspaceId, key);
|
||||
meta = blob.metadata;
|
||||
blob.body?.destroy();
|
||||
}
|
||||
const meta = await this.provider.head(`${workspaceId}/${key}`);
|
||||
|
||||
if (meta) {
|
||||
await this.db.blob.upsert({
|
||||
where: {
|
||||
workspaceId_key: {
|
||||
workspaceId,
|
||||
key,
|
||||
},
|
||||
},
|
||||
update: {
|
||||
mime: meta.contentType,
|
||||
size: meta.contentLength,
|
||||
},
|
||||
create: {
|
||||
workspaceId,
|
||||
key,
|
||||
mime: meta.contentType,
|
||||
size: meta.contentLength,
|
||||
},
|
||||
});
|
||||
await this.upsert(workspaceId, key, meta);
|
||||
} else {
|
||||
await this.db.blob.deleteMany({
|
||||
where: {
|
||||
|
||||
Reference in New Issue
Block a user