mirror of
https://github.com/Drop-OSS/drop.git
synced 2026-06-22 04:11:32 +10:00
feat: cleanup extra metadata
This commit is contained in:
@@ -174,6 +174,30 @@ export class FsObjectBackend extends ObjectBackend {
|
|||||||
async listAll(): Promise<string[]> {
|
async listAll(): Promise<string[]> {
|
||||||
return fs.readdirSync(this.baseObjectPath);
|
return fs.readdirSync(this.baseObjectPath);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async cleanupMetadata() {
|
||||||
|
const metadataFiles = fs.readdirSync(this.baseMetadataPath);
|
||||||
|
const objects = await this.listAll();
|
||||||
|
|
||||||
|
const extraFiles = metadataFiles.filter(
|
||||||
|
(file) => !objects.includes(file.replace(/\.json$/, "")),
|
||||||
|
);
|
||||||
|
console.log(
|
||||||
|
`[FsObjectBackend#cleanupMetadata]: Found ${extraFiles.length} metadata files without corresponding objects.`,
|
||||||
|
);
|
||||||
|
for (const file of extraFiles) {
|
||||||
|
const filePath = path.join(this.baseMetadataPath, file);
|
||||||
|
try {
|
||||||
|
fs.rmSync(filePath);
|
||||||
|
console.log(`[FsObjectBackend#cleanupMetadata]: Removed ${file}`);
|
||||||
|
} catch (error) {
|
||||||
|
console.error(
|
||||||
|
`[FsObjectBackend#cleanupMetadata]: Failed to remove ${file}`,
|
||||||
|
error,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class FsHashStore {
|
class FsHashStore {
|
||||||
|
|||||||
@@ -71,6 +71,7 @@ export abstract class ObjectBackend {
|
|||||||
): Promise<boolean>;
|
): Promise<boolean>;
|
||||||
abstract fetchHash(id: ObjectReference): Promise<string | undefined>;
|
abstract fetchHash(id: ObjectReference): Promise<string | undefined>;
|
||||||
abstract listAll(): Promise<string[]>;
|
abstract listAll(): Promise<string[]>;
|
||||||
|
abstract cleanupMetadata(): Promise<void>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class ObjectHandler {
|
export class ObjectHandler {
|
||||||
@@ -257,4 +258,13 @@ export class ObjectHandler {
|
|||||||
async listAll() {
|
async listAll() {
|
||||||
return await this.backend.listAll();
|
return await this.backend.listAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Purges metadata for objects that no longer exist
|
||||||
|
* This is useful for cleaning up metadata files that are left behinds
|
||||||
|
* @returns
|
||||||
|
*/
|
||||||
|
async cleanupMetadata() {
|
||||||
|
return await this.backend.cleanupMetadata();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -44,6 +44,9 @@ export default defineTask<TaskReturn>({
|
|||||||
}
|
}
|
||||||
await Promise.all(deletePromises);
|
await Promise.all(deletePromises);
|
||||||
|
|
||||||
|
// Remove any possible leftover metadata
|
||||||
|
objectHandler.cleanupMetadata();
|
||||||
|
|
||||||
console.log("[Task cleanup:objects]: Done");
|
console.log("[Task cleanup:objects]: Done");
|
||||||
return {
|
return {
|
||||||
result: {
|
result: {
|
||||||
|
|||||||
Reference in New Issue
Block a user