Compare commits
2 Commits
5ac004d194
...
1c1f9ed5fd
Author | SHA1 | Date |
---|---|---|
Tykayn | 1c1f9ed5fd | |
Tykayn | 91a61d2c07 |
|
@ -32,7 +32,7 @@ class config_rangement {
|
||||||
base_archive_folder = constants.base_archive_folder
|
base_archive_folder = constants.base_archive_folder
|
||||||
templates = mainTemplates;
|
templates = mainTemplates;
|
||||||
statistics = {};
|
statistics = {};
|
||||||
keepOriginalNameInRename = true;
|
keepOriginalNameInRename = false;
|
||||||
log_actions = true;
|
log_actions = true;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
1
index.ts
1
index.ts
|
@ -25,6 +25,7 @@ log.info(' ')
|
||||||
|
|
||||||
|
|
||||||
finder.parseArguments()
|
finder.parseArguments()
|
||||||
|
finder.guessFileNameOnAllFilesFromArguments()
|
||||||
|
|
||||||
|
|
||||||
console.log('hello ts', finder)
|
console.log('hello ts', finder)
|
Binary file not shown.
After Width: | Height: | Size: 2.0 MiB |
|
@ -76,6 +76,7 @@ export default class finder {
|
||||||
static guessFileNameOnOnefile(fullPath: string): void {
|
static guessFileNameOnOnefile(fullPath: string): void {
|
||||||
|
|
||||||
log.info('go guess file name on file: ', fullPath)
|
log.info('go guess file name on file: ', fullPath)
|
||||||
|
|
||||||
fs.stat(fullPath, (err, stats) => {
|
fs.stat(fullPath, (err, stats) => {
|
||||||
|
|
||||||
if (err) {
|
if (err) {
|
||||||
|
@ -84,32 +85,40 @@ export default class finder {
|
||||||
return
|
return
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
|
log.debug(' -------- le fichier existe bien, déstructuration')
|
||||||
let structureForFile = this.destructurateFileName(fullPath)
|
let structureForFile = this.destructurateFileName(fullPath)
|
||||||
|
log.debug(' -------- ', fullPath)
|
||||||
|
log.debug(' -------- ', structureForFile)
|
||||||
|
|
||||||
// examiner les infos exif de chaque fichier pour proposer un nouveau nom
|
// examiner les infos exif de chaque fichier pour proposer un nouveau nom
|
||||||
if (!structureForFile.dateStampInFileNameOriginal) {
|
if (!structureForFile.dateStampInFileNameOriginal) {
|
||||||
log.info(' le nom de fichier "' + structureForFile.freeText + '" ne contient pas de date formatée au début')
|
log.info(' le nom de fichier "' + structureForFile.freeText + '" ne contient pas de date formatée au début')
|
||||||
|
} else {
|
||||||
this.findExifCreationDate(structureForFile.fullPath)
|
log.debug(' -------- dateStampInFileNameOriginal: ', structureForFile.dateStampInFileNameOriginal)
|
||||||
.then(data => {
|
|
||||||
log.info(' ... chercher la date de création : "' + structureForFile.freeText + '"')
|
|
||||||
let foundDate = this.findEarliestDateInExifData(data)
|
|
||||||
|
|
||||||
log.info(' =>>>>>>> foundDate : ', foundDate)
|
|
||||||
if (foundDate) {
|
|
||||||
structureForFile.dateStampExif = foundDate
|
|
||||||
} else {
|
|
||||||
log.info('pas de date trouvée dans le nom')
|
|
||||||
}
|
|
||||||
this.shouldWeChangeName(structureForFile)
|
|
||||||
}
|
|
||||||
,
|
|
||||||
(error) => {
|
|
||||||
log.warn('/////////// Error in reading exif of file: ' + error.message)
|
|
||||||
return ''
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
log.debug(' -------- trouver les infos exif', structureForFile.fullPath)
|
||||||
|
this.findExifCreationDate(structureForFile.fullPath)
|
||||||
|
.then(data => {
|
||||||
|
log.info(' ... chercher la date de création : "' + structureForFile.freeText + '"')
|
||||||
|
log.debug('data', data)
|
||||||
|
let foundDate = this.findEarliestDateInExifData(data)
|
||||||
|
|
||||||
|
log.info(' =>>>>>>> foundDate : ', foundDate)
|
||||||
|
if (foundDate) {
|
||||||
|
structureForFile.dateStampExif = foundDate
|
||||||
|
} else {
|
||||||
|
log.info('pas de date trouvée dans le nom')
|
||||||
|
}
|
||||||
|
this.shouldWeChangeName(structureForFile)
|
||||||
|
}
|
||||||
|
,
|
||||||
|
(error) => {
|
||||||
|
log.warn('/////////// Error in reading exif of file: ' + error.message)
|
||||||
|
return ''
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -137,12 +146,14 @@ export default class finder {
|
||||||
|
|
||||||
static renameFile(originalFileName: string, fileMixedNewName: string) {
|
static renameFile(originalFileName: string, fileMixedNewName: string) {
|
||||||
if (rangement_instance.keepOriginalNameInRename) {
|
if (rangement_instance.keepOriginalNameInRename) {
|
||||||
|
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)
|
||||||
}
|
}
|
||||||
|
|
||||||
let self = this;
|
let self = this;
|
||||||
fs.rename(originalFileName, fileMixedNewName, function (err) {
|
fs.rename(originalFileName, fileMixedNewName, function (err) {
|
||||||
log.info('name changed', fileMixedNewName)
|
log.info(' ✅ name changed', fileMixedNewName)
|
||||||
if (err) {
|
if (err) {
|
||||||
log.info('rename ERROR: ' + err)
|
log.info('rename ERROR: ' + err)
|
||||||
} else {
|
} else {
|
||||||
|
@ -156,7 +167,7 @@ export default class finder {
|
||||||
static guessFileNameOnAllFilesFromArguments(): void {
|
static guessFileNameOnAllFilesFromArguments(): void {
|
||||||
|
|
||||||
// parcourir les fichiers
|
// parcourir les fichiers
|
||||||
log.debug('liste des fichiers', this.mini_arguments._)
|
log.debug('_____________ liste des fichiers', this.mini_arguments._)
|
||||||
let fileList = this.mini_arguments._
|
let fileList = this.mini_arguments._
|
||||||
|
|
||||||
// test file exists
|
// test file exists
|
||||||
|
@ -168,7 +179,9 @@ export default class finder {
|
||||||
)
|
)
|
||||||
|
|
||||||
log.info('expanded file list :', this.expandedFileList)
|
log.info('expanded file list :', this.expandedFileList)
|
||||||
this.expandedFileList.forEach((filePath: string) => this.guessFileNameOnOnefile(filePath))
|
this.expandedFileList.forEach((filePath: string) => {
|
||||||
|
this.guessFileNameOnOnefile(filePath)
|
||||||
|
})
|
||||||
|
|
||||||
if (rangement_instance.reportStatistics || this.mini_arguments.stats) {
|
if (rangement_instance.reportStatistics || this.mini_arguments.stats) {
|
||||||
finder.reportStatistics()
|
finder.reportStatistics()
|
||||||
|
@ -187,10 +200,10 @@ export default class finder {
|
||||||
log.debug('fileProperties.dateStampExif', fileProperties.dateStampExif)
|
log.debug('fileProperties.dateStampExif', fileProperties.dateStampExif)
|
||||||
let newName = ''
|
let newName = ''
|
||||||
+ fileProperties.dateStampExif
|
+ fileProperties.dateStampExif
|
||||||
+ ' '
|
// + ' '
|
||||||
+ (rangement_instance.keepFreeText ? fileProperties.freeText : '')
|
+ (rangement_instance.keepFreeText ? fileProperties.freeText : '')
|
||||||
+ tagPlace
|
+ tagPlace.trim()
|
||||||
+ fileProperties.extension
|
+ fileProperties.extension.trim()
|
||||||
|
|
||||||
if (rangement_instance.replaceUnderscoreWithSpaces) {
|
if (rangement_instance.replaceUnderscoreWithSpaces) {
|
||||||
newName = newName.replace('_', ' ')
|
newName = newName.replace('_', ' ')
|
||||||
|
@ -213,14 +226,22 @@ export default class finder {
|
||||||
log.info(' ______ shouldWeChangeName ', structureForFile.fileNameOriginal)
|
log.info(' ______ shouldWeChangeName ', structureForFile.fileNameOriginal)
|
||||||
let newName = this.makeFileNameFromProperties(structureForFile)
|
let newName = this.makeFileNameFromProperties(structureForFile)
|
||||||
log.debug('newName', newName)
|
log.debug('newName', newName)
|
||||||
if (structureForFile.fileNameOriginal !== newName && !this.otherRenames.includes(newName)) {
|
if(this.otherRenames.includes(newName)){
|
||||||
|
log.debug('nouveau nom déjà présent dans les fichiers déjà renommés, on garde en état actuel')
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (structureForFile.fileNameOriginal !== newName) {
|
||||||
|
|
||||||
log.info('\n ancien nom :', structureForFile.fileNameOriginal)
|
log.info('\n ancien nom :', structureForFile.fileNameOriginal)
|
||||||
|
|
||||||
log.info(' nouveau nom:', newName)
|
log.info(' nouveau nom:', newName)
|
||||||
if (!this.mini_arguments['dry-run']) {
|
if (!this.mini_arguments['dry-run']) {
|
||||||
|
|
||||||
this.renameFile(structureForFile.fullPath, structureForFile.folderPath + newName)
|
log.debug('___________ structureForFile.folderPath', structureForFile.folderPath)
|
||||||
|
log.debug('___________ newName', newName)
|
||||||
|
|
||||||
|
this.renameFile(structureForFile.fullPath,
|
||||||
|
structureForFile.folderPath + newName)
|
||||||
} else {
|
} else {
|
||||||
log.info('no renaming for real, this is a dry run')
|
log.info('no renaming for real, this is a dry run')
|
||||||
finder.statistics['filesNotModified']++
|
finder.statistics['filesNotModified']++
|
||||||
|
@ -320,7 +341,7 @@ export default class finder {
|
||||||
}
|
}
|
||||||
|
|
||||||
static cleanSpaces(inputString: string) {
|
static cleanSpaces(inputString: string) {
|
||||||
return inputString.trim().replace(/ *g/, ' ').replace(/ /, ' ')
|
return inputString.replace(/ *g/, ' ').replace(/ /, ' ').trim()
|
||||||
}
|
}
|
||||||
|
|
||||||
static searchAndReplaceInFileName(searchString: string, replaceString: string, fileName: string): string {
|
static searchAndReplaceInFileName(searchString: string, replaceString: string, fileName: string): string {
|
||||||
|
@ -411,12 +432,17 @@ export default class finder {
|
||||||
*/
|
*/
|
||||||
static destructurateFileName(fullPath: string): fileDestructuration {
|
static destructurateFileName(fullPath: string): fileDestructuration {
|
||||||
let [folderPath, fileNameOriginal] = this.findFolderPath(fullPath)
|
let [folderPath, fileNameOriginal] = this.findFolderPath(fullPath)
|
||||||
let dateStampInFileNameOriginal = this.findFormattedDate(fileNameOriginal)
|
|
||||||
|
// if path is relative, add the current working directory as full path part
|
||||||
|
if (/^\\(?!\\)/.test(fullPath)) {
|
||||||
|
fullPath = cwd + '/' + fullPath
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
fullPath,
|
fullPath,
|
||||||
folderPath,
|
folderPath,
|
||||||
fileNameOriginal,
|
fileNameOriginal,
|
||||||
dateStampInFileNameOriginal,
|
dateStampInFileNameOriginal: this.findFormattedDate(fileNameOriginal),
|
||||||
dateStampExif: '',
|
dateStampExif: '',
|
||||||
freeText: this.findFileNameFreeTextPart(fileNameOriginal),
|
freeText: this.findFileNameFreeTextPart(fileNameOriginal),
|
||||||
tags: this.findTagSectionInString(fileNameOriginal),
|
tags: this.findTagSectionInString(fileNameOriginal),
|
||||||
|
@ -475,7 +501,7 @@ export default class finder {
|
||||||
|
|
||||||
return minDate.format(rangement_instance.iso_date_format)
|
return minDate.format(rangement_instance.iso_date_format)
|
||||||
} else {
|
} else {
|
||||||
log.debug(' finder - /!\\ pas de exif data')
|
log.debug(' finder - 😥 /!\\ pas de exif data')
|
||||||
return ''
|
return ''
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -497,14 +523,18 @@ export default class finder {
|
||||||
*/
|
*/
|
||||||
static async findExifCreationDate(filepath: string) {
|
static async findExifCreationDate(filepath: string) {
|
||||||
|
|
||||||
log.debug(' finder - filepath', filepath)
|
log.debug(' -------- findExifCreationDate')
|
||||||
let dateAlreadyInFileName = finder.findFormattedDate(filepath)
|
let dateAlreadyInFileName = finder.findFormattedDate(filepath)
|
||||||
if (dateAlreadyInFileName) {
|
if (dateAlreadyInFileName) {
|
||||||
|
|
||||||
log.debug(' finder - ------ dateAlreadyInFileName', dateAlreadyInFileName)
|
log.debug(' finder - ------ dateAlreadyInFileName', dateAlreadyInFileName)
|
||||||
|
} else {
|
||||||
|
log.debug(' -------- pas de date présente')
|
||||||
}
|
}
|
||||||
|
|
||||||
return await exifr.parse(filepath)
|
let exifPromise = await exifr.parse(filepath);
|
||||||
|
log.debug(' -------- exifdata', exifPromise)
|
||||||
|
|
||||||
|
return exifPromise
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue