refactor(server): use Blob Model on storage (#12897)
#### PR Dependency Tree * **PR #12894** * **PR #12897** 👈 This tree was auto-generated by [Charcoal](https://github.com/danerwilliams/charcoal)
This commit is contained in:
@@ -1,5 +1,4 @@
|
|||||||
import { Injectable, Logger } from '@nestjs/common';
|
import { Injectable, Logger } from '@nestjs/common';
|
||||||
import { PrismaClient } from '@prisma/client';
|
|
||||||
|
|
||||||
import {
|
import {
|
||||||
autoMetadata,
|
autoMetadata,
|
||||||
@@ -13,6 +12,7 @@ import {
|
|||||||
StorageProviderFactory,
|
StorageProviderFactory,
|
||||||
URLHelper,
|
URLHelper,
|
||||||
} from '../../../base';
|
} from '../../../base';
|
||||||
|
import { Models } from '../../../models';
|
||||||
|
|
||||||
declare global {
|
declare global {
|
||||||
interface Events {
|
interface Events {
|
||||||
@@ -40,7 +40,7 @@ export class WorkspaceBlobStorage {
|
|||||||
private readonly AFFiNEConfig: Config,
|
private readonly AFFiNEConfig: Config,
|
||||||
private readonly event: EventBus,
|
private readonly event: EventBus,
|
||||||
private readonly storageFactory: StorageProviderFactory,
|
private readonly storageFactory: StorageProviderFactory,
|
||||||
private readonly db: PrismaClient,
|
private readonly models: Models,
|
||||||
private readonly url: URLHelper
|
private readonly url: URLHelper
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
@@ -72,12 +72,7 @@ export class WorkspaceBlobStorage {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async list(workspaceId: string, syncBlobMeta = true) {
|
async list(workspaceId: string, syncBlobMeta = true) {
|
||||||
const blobsInDb = await this.db.blob.findMany({
|
const blobsInDb = await this.models.blob.list(workspaceId);
|
||||||
where: {
|
|
||||||
workspaceId,
|
|
||||||
deletedAt: null,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
if (blobsInDb.length > 0) {
|
if (blobsInDb.length > 0) {
|
||||||
return blobsInDb;
|
return blobsInDb;
|
||||||
@@ -103,36 +98,12 @@ export class WorkspaceBlobStorage {
|
|||||||
async delete(workspaceId: string, key: string, permanently = false) {
|
async delete(workspaceId: string, key: string, permanently = false) {
|
||||||
if (permanently) {
|
if (permanently) {
|
||||||
await this.provider.delete(`${workspaceId}/${key}`);
|
await this.provider.delete(`${workspaceId}/${key}`);
|
||||||
await this.db.blob.deleteMany({
|
|
||||||
where: {
|
|
||||||
workspaceId,
|
|
||||||
key,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
await this.db.blob.update({
|
|
||||||
where: {
|
|
||||||
workspaceId_key: {
|
|
||||||
workspaceId,
|
|
||||||
key,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
data: {
|
|
||||||
deletedAt: new Date(),
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
await this.models.blob.delete(workspaceId, key, permanently);
|
||||||
}
|
}
|
||||||
|
|
||||||
async release(workspaceId: string) {
|
async release(workspaceId: string) {
|
||||||
const deletedBlobs = await this.db.blob.findMany({
|
const deletedBlobs = await this.models.blob.listDeleted(workspaceId);
|
||||||
where: {
|
|
||||||
workspaceId,
|
|
||||||
deletedAt: {
|
|
||||||
not: null,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
deletedBlobs.forEach(blob => {
|
deletedBlobs.forEach(blob => {
|
||||||
this.event.emit('workspace.blob.delete', {
|
this.event.emit('workspace.blob.delete', {
|
||||||
@@ -140,20 +111,14 @@ export class WorkspaceBlobStorage {
|
|||||||
key: blob.key,
|
key: blob.key,
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
this.logger.log(
|
||||||
|
`released ${deletedBlobs.length} blobs for workspace ${workspaceId}`
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
async totalSize(workspaceId: string) {
|
async totalSize(workspaceId: string) {
|
||||||
const sum = await this.db.blob.aggregate({
|
return await this.models.blob.totalSize(workspaceId);
|
||||||
where: {
|
|
||||||
workspaceId,
|
|
||||||
deletedAt: null,
|
|
||||||
},
|
|
||||||
_sum: {
|
|
||||||
size: true,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
return sum._sum.size ?? 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
getAvatarUrl(workspaceId: string, avatarKey: string | null) {
|
getAvatarUrl(workspaceId: string, avatarKey: string | null) {
|
||||||
@@ -177,23 +142,11 @@ export class WorkspaceBlobStorage {
|
|||||||
key: string,
|
key: string,
|
||||||
meta: GetObjectMetadata
|
meta: GetObjectMetadata
|
||||||
) {
|
) {
|
||||||
await this.db.blob.upsert({
|
await this.models.blob.upsert({
|
||||||
where: {
|
|
||||||
workspaceId_key: {
|
|
||||||
workspaceId,
|
|
||||||
key,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
update: {
|
|
||||||
mime: meta.contentType,
|
|
||||||
size: meta.contentLength,
|
|
||||||
},
|
|
||||||
create: {
|
|
||||||
workspaceId,
|
workspaceId,
|
||||||
key,
|
key,
|
||||||
mime: meta.contentType,
|
mime: meta.contentType,
|
||||||
size: meta.contentLength,
|
size: meta.contentLength,
|
||||||
},
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -205,12 +158,7 @@ export class WorkspaceBlobStorage {
|
|||||||
if (meta) {
|
if (meta) {
|
||||||
await this.upsert(workspaceId, key, meta);
|
await this.upsert(workspaceId, key, meta);
|
||||||
} else {
|
} else {
|
||||||
await this.db.blob.deleteMany({
|
await this.models.blob.delete(workspaceId, key, true);
|
||||||
where: {
|
|
||||||
workspaceId,
|
|
||||||
key,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
// never throw
|
// never throw
|
||||||
|
|||||||
Reference in New Issue
Block a user