add: delete actions for device + slightly better ui
This commit is contained in:
@@ -10,7 +10,7 @@ import { FlowExecCtx } from "@core/flow.execution.context";
|
||||
import { StorageRepository } from "./storage.repository";
|
||||
import { FileRepository } from "./repository";
|
||||
import { settings } from "@core/settings";
|
||||
import { ResultAsync } from "neverthrow";
|
||||
import { okAsync, ResultAsync } from "neverthrow";
|
||||
import { traceResultAsync } from "@core/observability";
|
||||
import { db } from "@pkg/db";
|
||||
|
||||
@@ -198,6 +198,65 @@ export class FileController {
|
||||
});
|
||||
}
|
||||
|
||||
deleteFile(fctx: FlowExecCtx, fileId: string, userId: string) {
|
||||
return traceResultAsync({
|
||||
name: "logic.files.controller.deleteFile",
|
||||
fctx,
|
||||
attributes: { "app.user.id": userId, "app.file.id": fileId },
|
||||
fn: () => this.deleteFiles(fctx, [fileId], userId),
|
||||
});
|
||||
}
|
||||
|
||||
cleanupDanglingStorageFiles(fctx: FlowExecCtx, userId: string) {
|
||||
return traceResultAsync({
|
||||
name: "logic.files.controller.cleanupDanglingStorageFiles",
|
||||
fctx,
|
||||
attributes: { "app.user.id": userId },
|
||||
fn: () =>
|
||||
this.fileRepo
|
||||
.listReferencedObjectKeysForUser(fctx, userId)
|
||||
.andThen((referencedKeys) => {
|
||||
const referencedSet = new Set(referencedKeys);
|
||||
return ResultAsync.combine([
|
||||
this.storageRepo.listObjectKeys(
|
||||
fctx,
|
||||
`uploads/${userId}/`,
|
||||
),
|
||||
this.storageRepo.listObjectKeys(
|
||||
fctx,
|
||||
`thumbnails/${userId}/`,
|
||||
),
|
||||
]).andThen(([uploadKeys, thumbnailKeys]) => {
|
||||
const existingStorageKeys = [
|
||||
...new Set([...uploadKeys, ...thumbnailKeys]),
|
||||
];
|
||||
|
||||
const danglingKeys = existingStorageKeys.filter(
|
||||
(key) => !referencedSet.has(key),
|
||||
);
|
||||
|
||||
if (danglingKeys.length === 0) {
|
||||
return okAsync({
|
||||
scanned: existingStorageKeys.length,
|
||||
referenced: referencedKeys.length,
|
||||
dangling: 0,
|
||||
deleted: 0,
|
||||
});
|
||||
}
|
||||
|
||||
return this.storageRepo
|
||||
.deleteFiles(fctx, danglingKeys)
|
||||
.map(() => ({
|
||||
scanned: existingStorageKeys.length,
|
||||
referenced: referencedKeys.length,
|
||||
dangling: danglingKeys.length,
|
||||
deleted: danglingKeys.length,
|
||||
}));
|
||||
});
|
||||
}),
|
||||
});
|
||||
}
|
||||
|
||||
shareFile(
|
||||
fctx: FlowExecCtx,
|
||||
fileId: string,
|
||||
|
||||
Reference in New Issue
Block a user