From f12daa295e1a6941083b635555594dd6c8769953 Mon Sep 17 00:00:00 2001 From: Tykayn Date: Tue, 2 Jan 2024 23:37:40 +0100 Subject: [PATCH] add original file name with exiftool after rename --- conf/configs.ts | 3 ++- utils/finder.ts | 65 +++++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 59 insertions(+), 9 deletions(-) diff --git a/conf/configs.ts b/conf/configs.ts index 58e8c71..0a7b584 100644 --- a/conf/configs.ts +++ b/conf/configs.ts @@ -15,7 +15,8 @@ interface ConfigOverride { * configuration générale à importer dans les utilitaires */ class config_rangement { - log_level: LogLevelDesc = 'debug' // 'debug' | 'warn' |'info' + // log_level: LogLevelDesc = 'debug' // 'debug' | 'warn' |'info' + log_level: LogLevelDesc = 'info' // 'debug' | 'warn' |'info' version = '1.0.0' iso_date_format = 'yyyy-MM-DDTHH.mm.ss' // target format for dates in file names diff --git a/utils/finder.ts b/utils/finder.ts index 2665b7c..3c69774 100644 --- a/utils/finder.ts +++ b/utils/finder.ts @@ -10,11 +10,14 @@ import path from "node:path"; import minimist from 'minimist'; import {fileDestructuration, taggingCommand} from './interfaces'; +import child_process, {exec} from "child_process"; + log.setLevel(rangement_instance.log_level) let cwd = path.dirname(process.cwd()) + '/' + path.basename(process.cwd()) +const seconds_in_a_year: number = 1000 * 24 * 3600 * 30 * 12 /** * finds patterns for file name @@ -36,7 +39,8 @@ export default class finder { fs.readdir(baseDir, (err, filesList) => { if (err) throw err - log.debug('readSubdirectories - files', filesList) + log.debug(`readSubdirectories baseDir - ${baseDir} + * files: `, filesList) filesList.forEach((subDirOrFile) => { const newFullPath = cwd + '/' + subDirOrFile @@ -50,15 +54,20 @@ export default class finder { } }) + if (!filesList.length) { + filesList = [] + } + this.fileList = filesList; return filesList; }) } - static isFolderOrFile(filePathName: string) { + static async isFolderOrFile(filePathName: string): Promise { const stat = fs.statSync(filePathName) if (stat.isDirectory()) { - this.fileList = this.readSubdirectories(filePathName) + this.readSubdirectories(filePathName) + log.debug('fileList in directory ', filePathName, '\n', this.fileList) if (this.fileList.length) { this.expandedFileList.push(...this.fileList) @@ -161,7 +170,7 @@ export default class finder { if (rangement_instance.keepOriginalNameInRename) { log.debug(' +++++++++ on ajoute le nom original dans le free text +++++++', originalFileName) fileMixedNewName = this.addOriginalFileNameIfMissing(originalFileName, fileMixedNewName) - log.debug(' +++++++++ nouveau nom', fileMixedNewName) + log.info(' +++++++++ nouveau nom', fileMixedNewName) } let self = this; @@ -173,10 +182,35 @@ export default class finder { self.otherRenames.push(fileMixedNewName) finder.statistics['filesModified']++ + + // set the original file name + self.setOriginalFileNameInExifData(originalFileName, fileMixedNewName).then(res=>{ + log.debug('promise resolved setOriginalFileNameInExifData', res) + }) } }) } + /** + * définit la valeur exif du nom original du fichier + * @param filePath + * @param newFileName + */ + static setOriginalFileNameInExifData(filePath, newFileName) { + const command = `exiftool -OriginalFileName="${newFileName}" "${filePath}"`; + console.log('setOriginalFileNameInExifData command', command) + return new Promise((resolve, reject) => { + exec(command, (error, stdout, stderr) => { + if (error) { + reject(error); + } else { + resolve(stdout); + } + }); + }); +} + + static guessFileNameOnAllFilesFromArguments(): void { // parcourir les fichiers @@ -514,12 +548,26 @@ export default class finder { return result } - static dateIsGood(date: Date): boolean { + + static dateIsGood(date: string): boolean { + console.log('dateIsGood ?', date) if (!date) { return false } - let year: number = date.getFullYear(); - console.log('date', typeof date, year) + let year: number = 1970; + + let date_as_timestamp = date * 1 + console.log('date*1', date_as_timestamp) + if (date_as_timestamp > seconds_in_a_year) { + let dateObj: Date = new Date(date) + if (!dateObj) { + console.log("date is not valid") + } + console.log('date', date, dateObj) + year = dateObj.getUTCFullYear(); + } + + console.log('date : ', typeof date, year) console.log('(year > 1970)', (year > 1970)) @@ -539,6 +587,7 @@ export default class finder { } private static pushToMomentDatesIfGood(someDate: Date): void { + console.log('pushToMomentDatesIfGood someDate', someDate) if (this.dateIsGood(someDate)) { this.moments.push(someDate) } @@ -607,7 +656,7 @@ export default class finder { } let exifPromise = await exifr.parse(filepath); - // log.debug(' -------- exifdata', exifPromise) + console.log(' -------- exifdata', exifPromise) return exifPromise