rangement/utils/finder.ts

551 lines
19 KiB
TypeScript
Raw Normal View History

2023-07-19 12:32:13 +02:00
/**
* la classe qui repère des patterns
*/
import exifr from 'exifr'
import moment from 'moment'
import log from 'loglevel'
import rangement_instance from '../conf/configs'
import * as fs from "fs";
import path from "node:path";
import minimist from 'minimist';
2023-07-19 17:41:51 +02:00
import {fileDestructuration, taggingCommand} from './interfaces';
2023-07-19 12:32:13 +02:00
log.setLevel(rangement_instance.log_level)
let cwd = path.dirname(process.cwd()) + '/' + path.basename(process.cwd())
2023-07-19 17:41:51 +02:00
2023-07-19 12:32:13 +02:00
/**
* finds patterns for file name
*/
export default class finder {
static statistics = {
filesModified: 0,
filesNotModified: 0,
}
private static otherRenames: any = [];
private static fileList: any = [];
private static expandedFileList: any = [];
private static mini_arguments: any;
static readSubdirectories(baseDir: string) {
fs.readdir(baseDir, (err, filesList) => {
if (err) throw err
log.debug('readSubdirectories - files', filesList)
filesList.forEach((subDirOrFile) => {
const newFullPath = cwd + '/' + subDirOrFile
if (fs.existsSync(newFullPath)) {
const s = fs.statSync(newFullPath)
if (s.isFile()) {
this.expandedFileList.push(cwd + '/' + subDirOrFile)
}
}
})
return filesList;
})
}
static isFolderOrFile(filePathName: string) {
const stat = fs.statSync(filePathName)
if (stat.isDirectory()) {
this.fileList = this.readSubdirectories(filePathName)
log.debug('fileList in directory ', filePathName, '\n', this.fileList)
if (this.fileList.length) {
this.expandedFileList.push(...this.fileList)
}
} else if (stat.isFile()) {
this.expandedFileList.push(filePathName)
}
}
/**
* guess file name on one file which is not a directory
* @param fullPath
*/
2023-07-19 17:41:51 +02:00
static guessFileNameOnOnefile(fullPath: string): void {
log.info('go guess file name on file: ', fullPath)
2023-07-19 17:59:20 +02:00
fs.stat(fullPath, (err, stats) => {
if (err) {
log.error('échec fichier', err)
log.error('ce fichier n existe pas: ', fullPath)
return
} else {
2023-07-19 17:59:20 +02:00
log.debug(' -------- le fichier existe bien, déstructuration')
let structureForFile = this.destructurateFileName(fullPath)
2023-07-19 17:59:20 +02:00
log.debug(' -------- ', fullPath)
log.debug(' -------- ', structureForFile)
// examiner les infos exif de chaque fichier pour proposer un nouveau nom
if (!structureForFile.dateStampInFileNameOriginal) {
log.info(' le nom de fichier "' + structureForFile.freeText + '" ne contient pas de date formatée au début')
2023-07-19 17:59:20 +02:00
} else {
log.debug(' -------- dateStampInFileNameOriginal: ', structureForFile.dateStampInFileNameOriginal)
}
2023-07-19 18:28:24 +02:00
log.debug(' -------- trouver les infos exif', structureForFile.fullPath)
this.findExifCreationDate(structureForFile.fullPath)
2023-07-19 17:59:20 +02:00
.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 ''
})
}
})
}
/**
* if there is no original file name free text into the new name, append it to the free text part
* @param originalFileName
* @param fileMixedNewName
*/
static addOriginalFileNameIfMissing(originalFileName: string, fileMixedNewName: string) {
if (!fileMixedNewName.includes(originalFileName)) {
let properties = finder.destructurateFileName(fileMixedNewName)
return properties.freeText + ' ' + originalFileName
} else {
return fileMixedNewName
}
}
2023-07-19 12:32:13 +02:00
static reportStatistics() {
log.info('\n --------- statistics',
this.statistics)
}
static renameFile(originalFileName: string, fileMixedNewName: string) {
if (rangement_instance.keepOriginalNameInRename) {
2023-07-19 18:28:24 +02:00
log.debug(' +++++++++ on ajoute le nom original dans le free text +++++++',originalFileName )
fileMixedNewName = this.addOriginalFileNameIfMissing(originalFileName, fileMixedNewName)
2023-07-19 18:28:24 +02:00
log.debug(' +++++++++ nouveau nom', fileMixedNewName)
}
let self = this;
fs.rename(originalFileName, fileMixedNewName, function (err) {
2023-07-19 18:28:24 +02:00
log.info(' ✅ name changed', fileMixedNewName)
if (err) {
log.info('rename ERROR: ' + err)
} else {
self.otherRenames.push(fileMixedNewName)
finder.statistics['filesModified']++
}
})
}
2023-07-19 17:41:51 +02:00
static guessFileNameOnAllFilesFromArguments(): void {
// parcourir les fichiers
2023-07-19 17:59:20 +02:00
log.debug('_____________ liste des fichiers', this.mini_arguments._)
let fileList = this.mini_arguments._
// test file exists
fileList.forEach((fullPath: string) => {
log.debug('file list element: ', fullPath)
// parcourir les dossiers
this.isFolderOrFile(`${fullPath}`)
}
)
log.info('expanded file list :', this.expandedFileList)
2023-07-19 17:59:20 +02:00
this.expandedFileList.forEach((filePath: string) => {
this.guessFileNameOnOnefile(filePath)
})
if (rangement_instance.reportStatistics || this.mini_arguments.stats) {
finder.reportStatistics()
}
}
static makeFileNameFromProperties(fileProperties: fileDestructuration) {
let tagPlace = ''
log.info('fileProperties.tags', fileProperties.tags)
if (fileProperties.tags.length && rangement_instance.keepTags) {
tagPlace = ' ' + rangement_instance.tagSectionSeparator + ' ' + fileProperties.tags.join(rangement_instance.tagSeparator)
}
log.debug('fileProperties.dateStampExif', fileProperties.dateStampExif)
let newName = ''
+ fileProperties.dateStampExif
2023-07-19 18:28:24 +02:00
// + ' '
+ (rangement_instance.keepFreeText ? fileProperties.freeText : '')
2023-07-19 18:28:24 +02:00
+ tagPlace.trim()
+ fileProperties.extension.trim()
if (rangement_instance.replaceUnderscoreWithSpaces) {
newName = newName.replace('_', ' ')
}
newName = finder.cleanSpaces(newName)
return newName
}
static parseArguments() {
this.mini_arguments = minimist(process.argv.slice(2))
log.debug('arguments', this.mini_arguments)
2023-07-19 17:41:51 +02:00
if (!this.mini_arguments._.length) {
log.info('pas de fichier ou de dossier demandé, veuillez spécifier un chemin en argument')
}
}
static shouldWeChangeName(structureForFile: fileDestructuration) {
log.info(' ______ shouldWeChangeName ', structureForFile.fileNameOriginal)
let newName = this.makeFileNameFromProperties(structureForFile)
log.debug('newName', newName)
2023-07-19 18:28:24 +02:00
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(' nouveau nom:', newName)
if (!this.mini_arguments['dry-run']) {
2023-07-19 18:28:24 +02:00
log.debug('___________ structureForFile.folderPath', structureForFile.folderPath)
log.debug('___________ newName', newName)
this.renameFile(structureForFile.fullPath,
structureForFile.folderPath + newName)
} else {
log.info('no renaming for real, this is a dry run')
finder.statistics['filesNotModified']++
}
} else {
log.info(' rien à changer')
}
}
2023-07-19 12:32:13 +02:00
static findScreenshot(inputString: string) {
return inputString.match(/screenshot/i) || inputString.match(/capture d'écran/i)
}
static findFormattedDate(filepath: string) {
let match = filepath.match(/\d{4}-\d{2}-\d{2}T\d{2}\.\d{2}\.\d{2}/ig)
log.debug(' finder - match findFormattedDate', match)
let result = ''
if (match && match[0]) {
result = match[0]
}
return result
}
static findFileExtension(inputString: string): string {
let result = inputString.match(/\.\w{3,4}$/i)
if (result && result.length) {
return result[0]
}
return ''
}
/**
* find the section of file name which contains the free text to describe the picture
* @param fileName
* @returns {*|string}
*/
static findFileNameFreeTextPart(fileName: string) {
fileName = fileName.replace(this.findFileExtension(fileName), '')
let boom = fileName.split(rangement_instance.tagSectionSeparator)
if (boom.length) {
let freeTextPart = boom[0].trim()
// remove date
let foundDate = this.findFormattedDate(freeTextPart)
if (foundDate) {
freeTextPart = freeTextPart.replace(foundDate, '').trim()
}
log.debug(' finder - freeTextPart:', freeTextPart)
return freeTextPart
}
return fileName.trim()
}
/**
* find an array of tags
* @param inputString
* @returns {[]}
*/
static findTagSectionInString(inputString: string) {
let listOfTags: Array<string> = []
// remove extension
let extensionFile = finder.findFileExtension(inputString)
if (extensionFile) {
inputString = inputString.replace(extensionFile, '')
} else {
log.debug(' finder - no extensionFile', extensionFile, inputString)
extensionFile = ''
}
inputString = inputString.replace(extensionFile, '')
log.debug(' finder - extensionFile', extensionFile)
if (inputString.includes(rangement_instance.tagSectionSeparator)) {
log.debug(' finder - inputString', inputString)
if (inputString.length) {
let boom = inputString.split(rangement_instance.tagSectionSeparator)
log.debug(' finder - boom', boom)
if (boom.length) {
listOfTags = [...boom[1].trim().split(rangement_instance.tagSeparator)]
log.debug(' finder - listOfTags', listOfTags)
} else {
log.debug(' finder - no boom', boom)
}
}
}
return listOfTags
}
static removeTagInProperties(properties: fileDestructuration, tagName: string) {
let foundTagNameIndex = properties.tags.indexOf(tagName)
if (foundTagNameIndex) {
delete properties.tags[foundTagNameIndex]
}
return properties
}
static cleanSpaces(inputString: string) {
2023-07-19 18:28:24 +02:00
return inputString.replace(/ *g/, ' ').replace(/ /, ' ').trim()
2023-07-19 12:32:13 +02:00
}
static searchAndReplaceInFileName(searchString: string, replaceString: string, fileName: string): string {
return this.cleanSpaces(fileName.replace(searchString, replaceString))
}
/**
* search screenshot clues and rename
*/
static searchAndRenameScreenshots(fileName: string) {
if (finder.findScreenshot(fileName)) {
let tags = this.findTagSectionInString(fileName)
log.debug(' finder - tags', tags)
if (!tags.includes('screenshot')) {
fileName = this.addTagInFileName('screenshot', fileName)
fileName = this.searchAndReplaceInFileName('Screenshot', '', fileName)
log.debug(' finder - screenShotMockFileName:', fileName)
return this.cleanSpaces(fileName)
}
log.debug(' finder - is a screenshot, remove screenshot in name, and add tag screenshot')
} else {
return null
}
}
/**
* determines if we must add or remove new tags
* @param tagCommand
* @returns {{tagsToAdd: [], tagCommand: *, tagsToRemove: []}}
*/
2023-07-19 17:41:51 +02:00
static addOrRemoveTagsParsing(tagCommand: string): taggingCommand {
2023-07-19 12:32:13 +02:00
let tagsToAdd: Array<any> = []
let tagsToRemove: Array<any> = []
// split all tags
let listOfTags = tagCommand.split(' ')
listOfTags.forEach(elem => {
// remove when a minus is present*
if (elem.indexOf('-')) {
tagsToRemove.push(elem)
} else {
// add tag otherwise
tagsToAdd.push(elem)
}
})
return {tagCommand, tagsToAdd, tagsToRemove}
}
static applyTagChangesOnProperties(tagChange: any, properties: fileDestructuration) {
2023-07-19 17:41:51 +02:00
// add new tags
2023-07-19 12:32:13 +02:00
properties.tags = [...properties.tags, tagChange.tagsToAdd]
2023-07-19 17:41:51 +02:00
properties.tags.forEach((elem, index) => {
if (tagChange.tagsToRemove.includes(elem)) {
delete properties.tags[index]
}
2023-07-19 12:32:13 +02:00
})
2023-07-19 17:41:51 +02:00
properties.tags = [...new Set(properties.tags)];
2023-07-19 12:32:13 +02:00
return properties
}
/**
* add a tag and gives new filename with extension
* @param tagName
* @param fileName
* @returns {*}
*/
static addTagInFileName(tagName: string, fileName: string) {
2023-07-19 12:32:13 +02:00
let tags: any = this.findTagSectionInString(fileName)
2023-07-19 12:32:13 +02:00
let firstPart = this.findFileNameFreeTextPart(fileName)
tags.push(tagName)
// @ts-ignore
let uniqueArray = [...new Set(tags)]
let newFileName = firstPart + ' ' + rangement_instance.tagSectionSeparator + ' ' + uniqueArray.join(rangement_instance.tagSeparator)
newFileName = newFileName.replace(/ {*}/, '') + this.findFileExtension(fileName)
return this.cleanSpaces(newFileName)
}
/**
* convertit un nom de fichier en une structure décrivant plusieurs parties correspondant au pattern d'archivage
* @param fullPath
* @returns {{extension: *, dateStamp: string, freeText: (*|string), tags: *[]}}
*/
static destructurateFileName(fullPath: string): fileDestructuration {
2023-07-19 12:32:13 +02:00
let [folderPath, fileNameOriginal] = this.findFolderPath(fullPath)
2023-07-19 18:28:24 +02:00
// if path is relative, add the current working directory as full path part
if (/^\\(?!\\)/.test(fullPath)) {
fullPath = cwd + '/' + fullPath
}
2023-07-19 12:32:13 +02:00
return {
fullPath,
folderPath,
fileNameOriginal,
2023-07-19 18:28:24 +02:00
dateStampInFileNameOriginal: this.findFormattedDate(fileNameOriginal),
2023-07-19 12:32:13 +02:00
dateStampExif: '',
freeText: this.findFileNameFreeTextPart(fileNameOriginal),
tags: this.findTagSectionInString(fileNameOriginal),
extension: this.findFileExtension(fileNameOriginal),
} as fileDestructuration
2023-07-19 12:32:13 +02:00
}
/**
* finds the earliest part in several exif date info
* @param exifData
* @returns {string}
*/
static findEarliestDateInExifData(exifData: any) {
2023-07-19 12:32:13 +02:00
log.debug(' finder - findEarliestDateInExifData')
if (exifData) {
let moments = []
log.debug(' finder - exif data : ', exifData) // Do something with your data!
if (exifData.DateTimeOriginal) {
log.debug(' finder - image créée le : DateTimeOriginal : ', exifData.DateTimeOriginal) // Do something with your data!
moments.push(exifData.DateTimeOriginal)
}
if (exifData.ModificationDateTime) {
log.debug(' finder - image créée le : ModificationDateTime : ', exifData.ModificationDateTime) // Do something with your data!
moments.push(exifData.ModificationDateTime)
}
if (exifData.ModifyDate) {
log.debug(' finder - image créée le : ModifyDate : ', exifData.ModifyDate) // Do something with your data!
moments.push(exifData.ModifyDate)
}
if (exifData.FileAccessDateTime) {
moments.push(exifData.FileAccessDateTime)
}
if (exifData.FileInodeChangeDateTime) {
moments.push(exifData.FileInodeChangeDateTime)
}
if (exifData.FileModificationDateTime) {
log.debug(' finder - image créée le : FileModificationDateTime : ', exifData.FileModificationDateTime) // Do something with your data!
moments.push(exifData.FileModificationDateTime)
}
if (exifData.CreateDate) {
log.debug(' finder - image créée le : CreateDate : ', exifData.CreateDate) // Do something with your data!
moments.push(exifData.CreateDate)
}
moments = moments.map(d => {
let newdate = moment(d)
return newdate
})
let minDate = moment.min(moments)
log.debug(' finder - minDate :::::::::', minDate)
log.info(' finder - minDate :::::::::', minDate.format(rangement_instance.iso_date_format))
return minDate.format(rangement_instance.iso_date_format)
} else {
2023-07-19 17:59:20 +02:00
log.debug(' finder - 😥 /!\\ pas de exif data')
2023-07-19 12:32:13 +02:00
return ''
}
}
static appendFileName(fileProperties: fileDestructuration, newText: string) {
2023-07-19 12:32:13 +02:00
fileProperties.freeText = finder.cleanSpaces(fileProperties.freeText + ' ' + newText)
return fileProperties
}
static prependFileName(fileProperties: fileDestructuration, newText: string) {
2023-07-19 12:32:13 +02:00
fileProperties.freeText = finder.cleanSpaces(newText + ' ' + fileProperties.freeText)
return fileProperties
}
/**
* examine plusieurs propriétés exif de date et retourne la plus ancienne
* @param filepath
*/
static async findExifCreationDate(filepath: string) {
2023-07-19 12:32:13 +02:00
2023-07-19 17:59:20 +02:00
log.debug(' -------- findExifCreationDate')
2023-07-19 12:32:13 +02:00
let dateAlreadyInFileName = finder.findFormattedDate(filepath)
if (dateAlreadyInFileName) {
log.debug(' finder - ------ dateAlreadyInFileName', dateAlreadyInFileName)
2023-07-19 17:59:20 +02:00
} else {
log.debug(' -------- pas de date présente')
2023-07-19 12:32:13 +02:00
}
2023-07-19 17:59:20 +02:00
let exifPromise = await exifr.parse(filepath);
log.debug(' -------- exifdata', exifPromise)
return exifPromise
2023-07-19 12:32:13 +02:00
}
static findFolderPath(filePath: string) {
let folders: any = filePath.split('/')
let fileName: any = folders.pop()
2023-07-19 12:32:13 +02:00
folders = filePath.replace(fileName, '')
log.debug(' finder - \n - folders', folders)
log.debug(' finder - - fileName', fileName, '\n')
return [folders, fileName]
}
}