Skip to content

Commit

Permalink
feat: add option to rename empty filenames
Browse files Browse the repository at this point in the history
  • Loading branch information
garzj committed Jun 23, 2024
1 parent 22fb904 commit 21930e8
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 55 deletions.
68 changes: 38 additions & 30 deletions src/commands/common.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,41 @@
import { flag, number, option, positional, string } from 'cmd-ts';
import { ArgParser } from 'cmd-ts/dist/cjs/argparser';
import { ProvidesHelp } from 'cmd-ts/dist/cjs/helpdoc';

export const errorDirArg = positional({
type: string,
displayName: 'error_dir',
description: 'Failed media will be saved here.',
});
type ArgTypes = Record<string, ArgParser<any> & Partial<ProvidesHelp>>;

export const timeoutArg = option({
type: number,
defaultValue: () => 30000,
short: 't',
long: 'timeout',
description:
'Sets the task timeout in milliseconds that will be passed to ExifTool.',
});

export const forceArg = flag({
short: 'f',
long: 'force',
description: "Forces the operation if the given directories aren't empty.",
});

export const skipCorrectionsArg = flag({
long: 'skip-corrections',
description: 'Skips renaming wrong extensions identified by ExifTool.',
});

export const verboseArg = flag({
short: 'v',
long: 'verbose',
description: 'Enables verbose logging.',
});
export const commonArgs = {
errorDir: positional({
type: string,
displayName: 'error_dir',
description: 'Failed media will be saved here.',
}),
force: flag({
short: 'f',
long: 'force',
description: "Forces the operation if the given directories aren't empty.",
}),
timeout: option({
type: number,
defaultValue: () => 30000,
short: 't',
long: 'timeout',
description:
'Sets the task timeout in milliseconds that will be passed to ExifTool.',
}),
skipCorrections: flag({
long: 'skip-corrections',
description: 'Skips renaming wrong extensions identified by ExifTool.',
}),
verbose: flag({
short: 'v',
long: 'verbose',
description: 'Enables verbose logging.',
}),
renameEmpty: option({
type: string,
defaultValue: () => '_',
long: 'rename-empty',
description: 'Use this name for empty filenames.',
}),
} satisfies ArgTypes;
18 changes: 6 additions & 12 deletions src/commands/migrate-flat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,7 @@ import { pathExists } from 'fs-extra';
import { migrateDirFlatGen } from '../dir/migrate-flat';
import { isEmptyDir } from '../fs/is-empty-dir';
import { MediaMigrationError } from '../media/MediaMigrationError';
import {
errorDirArg,
forceArg,
skipCorrectionsArg,
timeoutArg,
verboseArg,
} from './common';
import { commonArgs } from './common';

export const migrateFlat = command({
name: 'google-photos-migrate-flat',
Expand All @@ -25,18 +19,16 @@ export const migrateFlat = command({
displayName: 'output_dir',
description: 'The path to your flat output directory.',
}),
errorDir: errorDirArg,
force: forceArg,
timeout: timeoutArg,
skipCorrections: skipCorrectionsArg,
verbose: verboseArg,
...commonArgs,
},
handler: async ({
inputDir,
outputDir,
errorDir,
force,
timeout,
skipCorrections,
renameEmpty,
verbose,
}) => {
const errs: string[] = [];
Expand Down Expand Up @@ -83,6 +75,8 @@ export const migrateFlat = command({
verboseLog: verbose ? console.log : undefined,
exiftool: new ExifTool({ taskTimeoutMillis: timeout }),
endExifTool: true,
skipCorrections,
renameEmpty,
});
const counts = { err: 0, suc: 0 };
for await (const result of migGen) {
Expand Down
16 changes: 4 additions & 12 deletions src/commands/migrate-full.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,7 @@ import { migrateDirFullGen } from '..';
import { NoPhotosDirError } from '../dir/NoPhotosDirError';
import { isEmptyDir } from '../fs/is-empty-dir';
import { MediaMigrationError } from '../media/MediaMigrationError';
import {
errorDirArg,
forceArg,
skipCorrectionsArg,
timeoutArg,
verboseArg,
} from './common';
import { commonArgs } from './common';

export const migrateFull = command({
name: 'google-photos-migrate-full',
Expand All @@ -26,11 +20,7 @@ export const migrateFull = command({
displayName: 'output_dir',
description: 'The path where you want the processed directories to go.',
}),
errorDir: errorDirArg,
force: forceArg,
timeout: timeoutArg,
skipCorrections: skipCorrectionsArg,
verbose: verboseArg,
...commonArgs,
},
handler: async ({
inputDir,
Expand All @@ -39,6 +29,7 @@ export const migrateFull = command({
force,
timeout,
skipCorrections,
renameEmpty,
verbose,
}) => {
const errs: string[] = [];
Expand Down Expand Up @@ -78,6 +69,7 @@ export const migrateFull = command({
exiftool: new ExifTool({ taskTimeoutMillis: timeout }),
endExifTool: true,
skipCorrections,
renameEmpty,
});
const counts = { err: 0, suc: 0 };
for await (const result of migGen) {
Expand Down
2 changes: 2 additions & 0 deletions src/dir/migration-args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export type MigrationArgs = {
exiftool?: ExifTool;
endExifTool?: boolean;
skipCorrections?: boolean;
renameEmpty?: string;
migrationLocks?: Map<string, Promise<string>>;
};

Expand All @@ -25,5 +26,6 @@ export async function migrationArgsDefaults(
warnLog: args.warnLog ?? (() => {}),
verboseLog: args.verboseLog ?? (() => {}),
skipCorrections: args.skipCorrections ?? false,
renameEmpty: args.renameEmpty ? args.renameEmpty : '_',
};
}
2 changes: 1 addition & 1 deletion src/media/save-to-dir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export async function saveToDir(
saveBase = saveBase ?? basename(file);
let sanitized = sanitize(saveBase, { replacement: '_' });
if (sanitized === '') {
sanitized = '_';
sanitized = migCtx.renameEmpty;
}
if (saveBase != sanitized) {
migCtx.warnLog(`Sanitized file: ${file}` + ` (New filename: ${sanitized})`);
Expand Down

0 comments on commit 21930e8

Please sign in to comment.