Skip to content
This repository has been archived by the owner on Nov 20, 2023. It is now read-only.

Add an option for a filter command #113

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
35 changes: 33 additions & 2 deletions bin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import * as mime from 'mime-types';
import chalk from 'chalk';
import pjson from '../package.json';
import pLimit from 'p-limit';
import { execSync } from 'child_process';

const log = console.log;
const rl = readline.createInterface({
Expand Down Expand Up @@ -48,6 +49,18 @@ program
)
.addOption(new Option('-i, --import', 'Import instead of upload').env('IMMICH_IMPORT').default(false))
.addOption(new Option('-id, --device-uuid <value>', 'Set a device UUID').env('IMMICH_DEVICE_UUID'))
.addOption(
new Option(
'-f, --filter <cmd>',
[
'Run a script to filter local files.',
'Each file will be provided to this script as the first argument.',
'If the provided command exits with a non-zero exit code, the file will not be processed.',
].join(' '),
)
.env('IMMICH_IMPORT_FILTER')
.default(''),
)
.addOption(
new Option(
'-d, --directory <value>',
Expand Down Expand Up @@ -101,6 +114,7 @@ async function upload(
album: createAlbums,
deviceUuid: deviceUuid,
import: doImport,
filter: filterCmd,
}: any,
) {
const endpoint = server;
Expand Down Expand Up @@ -129,7 +143,7 @@ async function upload(
crawler = crawler.withMaxDepth(0);
}

const files: any[] = [];
const files: string[] = [];

for (const newPath of paths) {
try {
Expand All @@ -154,6 +168,23 @@ async function upload(
}
}

if (filterCmd) {
log(`Performing file filtering ...`);
const filterResults: typeof files = files.filter((file) => {
try {
execSync(`${filterCmd} "${file}"`, { stdio: 'inherit' });
return true;
} catch (_) {
// TODO: Add log level support to the CLI script.
// console.info(`Filtered out file: %s`, file);
}
return false;
});
log(`Filtered out ${files.length - filterResults.length} files from ${files.length}`);
files.splice(0, files.length);
files.concat(filterResults);
}

// Ensure that list of files only has unique entries
const uniqueFiles = new Set(files);

Expand Down Expand Up @@ -463,7 +494,7 @@ async function getAssetInfoFromServer(endpoint: string, key: string, deviceId: s
return res.data;
} catch (e) {
log(chalk.red("Error getting device's uploaded assets"));
console.log(e)
console.log(e);
process.exit(1);
}
}
Expand Down