Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/cli delete duplicates #14769

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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
32 changes: 29 additions & 3 deletions cli/src/commands/asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export interface UploadOptionsDto {
dryRun?: boolean;
skipHash?: boolean;
delete?: boolean;
deleteDuplicates?: boolean;
album?: boolean;
albumName?: string;
includeHidden?: boolean;
Expand Down Expand Up @@ -67,7 +68,7 @@ export const upload = async (paths: string[], baseOptions: BaseOptions, options:
const { newFiles, duplicates } = await checkForDuplicates(scanFiles, options);
const newAssets = await uploadFiles(newFiles, options);
await updateAlbums([...newAssets, ...duplicates], options);
await deleteFiles(newFiles, options);
await deleteFiles({ newFiles, duplicates }, options);
};

const scan = async (pathsToCrawl: string[], options: UploadOptionsDto) => {
Expand Down Expand Up @@ -300,13 +301,17 @@ const uploadFile = async (input: string, stats: Stats): Promise<AssetMediaRespon
return response.json();
};

const deleteFiles = async (files: string[], options: UploadOptionsDto): Promise<void> => {
const deleteFiles = async ({ files: string[], duplicates: string[] } , options: UploadOptionsDto): Promise<void> => {
if (!options.delete) {
return;
}

if (options.dryRun) {
console.log(`Would have deleted ${files.length} local asset${s(files.length)}`);
console.log(`Would have deleted ${files.length} new local asset${s(files.length)}`);

if (options.deleteDuplicates) {
console.log(`Would have deleted ${duplicates.length} duplicate local asset${s(duplicates.length)}`);
}
return;
}

Expand All @@ -326,6 +331,27 @@ const deleteFiles = async (files: string[], options: UploadOptionsDto): Promise<
} finally {
deletionProgress.stop();
}


if (!options.deleteDuplicates) {
return;
}
console.log('Deleting assets which are duplicates...');

const deletionProgressDupe = new SingleBar(
{ format: 'Deleting local assets | {bar} | {percentage}% | ETA: {eta}s | {value}/{total} assets' },
Presets.shades_classic,
);
deletionProgressDupe.start(duplicates.length, 0);

try {
for (const assetBatch of chunk(duplicates, options.concurrency)) {
await Promise.all(assetBatch.map((input: string) => unlink(input)));
deletionProgressDupe.update(assetBatch.length);
}
} finally {
deletionProgressDupe.stop();
}
};

const updateAlbums = async (assets: Asset[], options: UploadOptionsDto) => {
Expand Down
1 change: 1 addition & 0 deletions cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ program
.default(4),
)
.addOption(new Option('--delete', 'Delete local assets after upload').env('IMMICH_DELETE_ASSETS'))
.addOption(new Option('--deleteDuplicates', 'Delete duplicate assets after upload. Requires also --delete.').env('IMMICH_DELETE_DUPLICATES'))
.argument('[paths...]', 'One or more paths to assets to be uploaded')
.action((paths, options) => upload(paths, program.opts(), options));

Expand Down
Loading