add original file name with exiftool after rename

This commit is contained in:
Tykayn 2024-01-02 23:37:40 +01:00 committed by tykayn
parent 051d029c30
commit f12daa295e
2 changed files with 59 additions and 9 deletions

View File

@ -15,7 +15,8 @@ interface ConfigOverride {
* configuration générale à importer dans les utilitaires * configuration générale à importer dans les utilitaires
*/ */
class config_rangement { 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' version = '1.0.0'
iso_date_format = 'yyyy-MM-DDTHH.mm.ss' // target format for dates in file names iso_date_format = 'yyyy-MM-DDTHH.mm.ss' // target format for dates in file names

View File

@ -10,11 +10,14 @@ import path from "node:path";
import minimist from 'minimist'; import minimist from 'minimist';
import {fileDestructuration, taggingCommand} from './interfaces'; import {fileDestructuration, taggingCommand} from './interfaces';
import child_process, {exec} from "child_process";
log.setLevel(rangement_instance.log_level) log.setLevel(rangement_instance.log_level)
let cwd = path.dirname(process.cwd()) + '/' + path.basename(process.cwd()) 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 * finds patterns for file name
@ -36,7 +39,8 @@ export default class finder {
fs.readdir(baseDir, (err, filesList) => { fs.readdir(baseDir, (err, filesList) => {
if (err) throw err if (err) throw err
log.debug('readSubdirectories - files', filesList) log.debug(`readSubdirectories baseDir - ${baseDir}
* files: `, filesList)
filesList.forEach((subDirOrFile) => { filesList.forEach((subDirOrFile) => {
const newFullPath = cwd + '/' + subDirOrFile const newFullPath = cwd + '/' + subDirOrFile
@ -50,15 +54,20 @@ export default class finder {
} }
}) })
if (!filesList.length) {
filesList = []
}
this.fileList = filesList;
return filesList; return filesList;
}) })
} }
static isFolderOrFile(filePathName: string) { static async isFolderOrFile(filePathName: string): Promise<void> {
const stat = fs.statSync(filePathName) const stat = fs.statSync(filePathName)
if (stat.isDirectory()) { if (stat.isDirectory()) {
this.fileList = this.readSubdirectories(filePathName) this.readSubdirectories(filePathName)
log.debug('fileList in directory ', filePathName, '\n', this.fileList) log.debug('fileList in directory ', filePathName, '\n', this.fileList)
if (this.fileList.length) { if (this.fileList.length) {
this.expandedFileList.push(...this.fileList) this.expandedFileList.push(...this.fileList)
@ -161,7 +170,7 @@ export default class finder {
if (rangement_instance.keepOriginalNameInRename) { if (rangement_instance.keepOriginalNameInRename) {
log.debug(' +++++++++ on ajoute le nom original dans le free text +++++++', originalFileName) log.debug(' +++++++++ on ajoute le nom original dans le free text +++++++', originalFileName)
fileMixedNewName = this.addOriginalFileNameIfMissing(originalFileName, fileMixedNewName) fileMixedNewName = this.addOriginalFileNameIfMissing(originalFileName, fileMixedNewName)
log.debug(' +++++++++ nouveau nom', fileMixedNewName) log.info(' +++++++++ nouveau nom', fileMixedNewName)
} }
let self = this; let self = this;
@ -173,10 +182,35 @@ export default class finder {
self.otherRenames.push(fileMixedNewName) self.otherRenames.push(fileMixedNewName)
finder.statistics['filesModified']++ 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 { static guessFileNameOnAllFilesFromArguments(): void {
// parcourir les fichiers // parcourir les fichiers
@ -514,12 +548,26 @@ export default class finder {
return result return result
} }
static dateIsGood(date: Date): boolean {
static dateIsGood(date: string): boolean {
console.log('dateIsGood ?', date)
if (!date) { if (!date) {
return false return false
} }
let year: number = date.getFullYear(); let year: number = 1970;
console.log('date', typeof date, year)
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)) console.log('(year > 1970)', (year > 1970))
@ -539,6 +587,7 @@ export default class finder {
} }
private static pushToMomentDatesIfGood(someDate: Date): void { private static pushToMomentDatesIfGood(someDate: Date): void {
console.log('pushToMomentDatesIfGood someDate', someDate)
if (this.dateIsGood(someDate)) { if (this.dateIsGood(someDate)) {
this.moments.push(someDate) this.moments.push(someDate)
} }
@ -607,7 +656,7 @@ export default class finder {
} }
let exifPromise = await exifr.parse(filepath); let exifPromise = await exifr.parse(filepath);
// log.debug(' -------- exifdata', exifPromise) console.log(' -------- exifdata', exifPromise)
return exifPromise return exifPromise