Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 6 additions & 14 deletions server/src/services/asset.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -575,16 +575,9 @@ describe(AssetService.name, () => {

await sut.handleAssetDeletion({ id: asset.id, deleteOnDisk: true });

expect(mocks.job.queue.mock.calls).toEqual([
[
{
name: JobName.FileDelete,
data: {
files: [...asset.files.map(({ path }) => path), asset.originalPath],
},
},
],
]);
expect(mocks.storage.unlink.mock.calls).toEqual(
[...asset.files.map(({ path }) => path), asset.originalPath].map((file) => [file]),
);
expect(mocks.asset.remove).toHaveBeenCalledWith(getForAssetDeletion(asset));
});

Expand Down Expand Up @@ -613,8 +606,8 @@ describe(AssetService.name, () => {

expect(mocks.job.queue.mock.calls).toEqual([
[{ name: JobName.AssetDelete, data: { id: motionAsset.id, deleteOnDisk: true } }],
[{ name: JobName.FileDelete, data: { files: [asset.originalPath] } }],
]);
expect(mocks.storage.unlink).toHaveBeenCalledWith(asset.originalPath);
});

it('should not delete a live motion part if it is being used by another asset', async () => {
Expand All @@ -624,9 +617,8 @@ describe(AssetService.name, () => {

await sut.handleAssetDeletion({ id: asset.id, deleteOnDisk: true });

expect(mocks.job.queue.mock.calls).toEqual([
[{ name: JobName.FileDelete, data: { files: [`/data/library/IMG_${asset.id}.jpg`] } }],
]);
expect(mocks.job.queue.mock.calls).toEqual([]);
expect(mocks.storage.unlink).toHaveBeenCalledWith(`/data/library/IMG_${asset.id}.jpg`);
});

it('should update usage', async () => {
Expand Down
25 changes: 24 additions & 1 deletion server/src/services/asset.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,11 @@ export class AssetService extends BaseService {
return JobStatus.Failed;
}

const fileDeletionStatus = await this.deleteAssetFiles(id, this.getFilesForDeletion(asset, deleteOnDisk));
if (fileDeletionStatus === JobStatus.Failed) {
return JobStatus.Failed;
}

// replace the parent of the stack children with a new asset
if (asset.stack?.primaryAssetId === id) {
// this only includes timeline visible assets and excludes the primary asset
Expand Down Expand Up @@ -362,6 +367,13 @@ export class AssetService extends BaseService {
}
}

return JobStatus.Success;
}

private getFilesForDeletion(
asset: { files: AssetFile[]; originalPath: string; isOffline: boolean },
deleteOnDisk: boolean,
): string[] {
const assetFiles = getAssetFiles(asset.files ?? []);
const files = [
assetFiles.thumbnailFile?.path,
Expand All @@ -377,7 +389,18 @@ export class AssetService extends BaseService {
files.push(assetFiles.sidecarFile?.path, asset.originalPath);
}

await this.jobRepository.queue({ name: JobName.FileDelete, data: { files: files.filter(Boolean) } });
return files.filter((file): file is string => file !== undefined);
}

private async deleteAssetFiles(assetId: string, files: string[]): Promise<JobStatus> {
for (const file of files) {
try {
await this.storageRepository.unlink(file);
} catch (error) {
this.logger.warn(`Unable to remove asset file for asset ${assetId}: ${file}`, error);
return JobStatus.Failed;
}
}

return JobStatus.Success;
}
Expand Down
Loading