2023-07-01 12:35:33 +02:00
|
|
|
/**
|
|
|
|
* la classe qui repère des patterns
|
|
|
|
*/
|
2023-07-04 16:14:25 +02:00
|
|
|
import rangement_instance from './configs.mjs'
|
2023-07-01 12:35:33 +02:00
|
|
|
import exifr from 'exifr'
|
|
|
|
import moment from 'moment'
|
2023-07-05 22:36:34 +02:00
|
|
|
import log from 'loglevel'
|
2023-07-05 22:34:32 +02:00
|
|
|
|
2023-07-05 22:36:34 +02:00
|
|
|
log.setLevel(rangement_instance.log_level)
|
2023-07-01 12:35:33 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* finds patterns for file name
|
|
|
|
*/
|
|
|
|
export default class finder {
|
|
|
|
|
|
|
|
static statistics = {
|
|
|
|
filesModified: 0,
|
|
|
|
}
|
|
|
|
|
|
|
|
static reportStatistics () {
|
2023-07-04 15:26:27 +02:00
|
|
|
log.info('statistics',
|
2023-07-01 12:35:33 +02:00
|
|
|
this.statistics)
|
|
|
|
}
|
|
|
|
|
|
|
|
static findScreenshot (inputString) {
|
|
|
|
return inputString.match(/screenshot/i) || inputString.match(/capture d'écran/i)
|
|
|
|
}
|
|
|
|
|
|
|
|
static findFormattedDate (filepath) {
|
2023-07-05 22:34:32 +02:00
|
|
|
let match = filepath.match(/\d{4}-\d{2}-\d{2}T\d{2}\.\d{2}\.\d{2}/ig)
|
2023-07-04 15:56:15 +02:00
|
|
|
log.debug(' finder - match findFormattedDate', match)
|
2023-07-01 12:35:33 +02:00
|
|
|
let result = ''
|
|
|
|
if (match && match[0]) {
|
|
|
|
result = match[0]
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
static findFileExtension (inputString) {
|
|
|
|
let result = inputString.match(/\.\w{3,4}$/i)
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* find the section of file name which contains the free text to describe the picture
|
|
|
|
* @param fileName
|
|
|
|
* @returns {*|string}
|
|
|
|
*/
|
|
|
|
static findFileNameFreeTextPart (fileName) {
|
|
|
|
fileName = fileName.replace(this.findFileExtension(fileName), '')
|
2023-07-04 16:14:25 +02:00
|
|
|
let boom = fileName.split(rangement_instance.tagSectionSeparator)
|
2023-07-01 12:35:33 +02:00
|
|
|
if (boom.length) {
|
|
|
|
let freeTextPart = boom[0].trim()
|
2023-07-04 15:56:15 +02:00
|
|
|
log.debug(' finder - freeTextPart', freeTextPart)
|
2023-07-01 12:35:33 +02:00
|
|
|
return freeTextPart
|
|
|
|
}
|
|
|
|
return fileName.trim()
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* find an array of tags
|
|
|
|
* @param inputString
|
|
|
|
* @returns {[]}
|
|
|
|
*/
|
|
|
|
static findTagSectionInString (inputString) {
|
|
|
|
let listOfTags = []
|
|
|
|
// remove extension
|
|
|
|
let extensionFile = finder.findFileExtension(inputString)
|
|
|
|
|
|
|
|
if (extensionFile) {
|
|
|
|
extensionFile = extensionFile[0]
|
|
|
|
} else {
|
2023-07-04 15:56:15 +02:00
|
|
|
log.debug(' finder - no extensionFile', extensionFile, inputString)
|
2023-07-01 12:35:33 +02:00
|
|
|
extensionFile = ''
|
|
|
|
}
|
|
|
|
inputString = inputString.replace(extensionFile, '')
|
|
|
|
|
2023-07-04 15:56:15 +02:00
|
|
|
log.debug(' finder - extensionFile', extensionFile)
|
2023-07-04 16:14:25 +02:00
|
|
|
if (inputString.includes(rangement_instance.tagSectionSeparator)) {
|
2023-07-04 15:56:15 +02:00
|
|
|
log.debug(' finder - inputString', inputString)
|
2023-07-01 12:35:33 +02:00
|
|
|
if (inputString.length) {
|
|
|
|
|
2023-07-04 16:14:25 +02:00
|
|
|
let boom = inputString.split(rangement_instance.tagSectionSeparator)
|
|
|
|
log.debug(' finder - boom', boom)
|
2023-07-01 12:35:33 +02:00
|
|
|
if (boom.length) {
|
2023-07-04 16:14:25 +02:00
|
|
|
let fileSectionsName = boom.splice(rangement_instance.tagSeparator)
|
|
|
|
listOfTags = [...fileSectionsName[1].trim().split(rangement_instance.tagSeparator)]
|
2023-07-04 15:56:15 +02:00
|
|
|
log.debug(' finder - listOfTags', listOfTags)
|
2023-07-01 12:35:33 +02:00
|
|
|
} else {
|
2023-07-04 15:56:15 +02:00
|
|
|
log.debug(' finder - no boom', boom)
|
2023-07-01 12:35:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return listOfTags
|
|
|
|
}
|
|
|
|
|
|
|
|
static cleanSpaces (inputString) {
|
|
|
|
return inputString.trim().replace(/ *g/, ' ')
|
|
|
|
}
|
|
|
|
|
|
|
|
static searchAndReplaceInFileName (searchString, replaceString, fileName) {
|
|
|
|
return this.cleanSpaces(fileName.replace(searchString, replaceString))
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* search screenshot clues and rename
|
|
|
|
*/
|
|
|
|
static searchAndRenameScreenshots (fileName) {
|
|
|
|
if (finder.findScreenshot(fileName)) {
|
|
|
|
let tags = this.findTagSectionInString(fileName)
|
2023-07-04 15:56:15 +02:00
|
|
|
log.debug(' finder - tags', tags)
|
2023-07-01 12:35:33 +02:00
|
|
|
if (!tags.includes('screenshot')) {
|
|
|
|
|
|
|
|
fileName = this.addTagInFileName('screenshot', fileName)
|
|
|
|
fileName = this.searchAndReplaceInFileName('Screenshot', '', fileName)
|
2023-07-04 15:56:15 +02:00
|
|
|
log.debug(' finder - screenShotMockFileName:', fileName)
|
2023-07-01 12:35:33 +02:00
|
|
|
return this.cleanSpaces(fileName)
|
|
|
|
}
|
2023-07-04 15:56:15 +02:00
|
|
|
log.debug(' finder - is a screenshot, remove screenshot in name, and add tag screenshot')
|
2023-07-01 12:35:33 +02:00
|
|
|
} else {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static addTagInFileName (tagName, fileName) {
|
|
|
|
|
|
|
|
let tags = this.findTagSectionInString(fileName)
|
|
|
|
let firstPart = this.findFileNameFreeTextPart(fileName)
|
|
|
|
|
|
|
|
tags.push(tagName)
|
|
|
|
let uniqueArray = [...new Set(tags)]
|
|
|
|
|
2023-07-04 16:14:25 +02:00
|
|
|
let newFileName = firstPart + ' ' + rangement_instance.tagSectionSeparator + ' ' + tags.join(rangement_instance.tagSeparator)
|
2023-07-01 12:35:33 +02:00
|
|
|
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) {
|
2023-07-05 22:36:34 +02:00
|
|
|
let [folderPath, fileNameOriginal] = this.findFolderPath(fullPath)
|
2023-07-01 12:35:33 +02:00
|
|
|
let dateStampInFileNameOriginal = this.findFormattedDate(fileNameOriginal)
|
|
|
|
return {
|
|
|
|
fullPath,
|
|
|
|
folderPath,
|
|
|
|
fileNameOriginal,
|
|
|
|
dateStampInFileNameOriginal,
|
|
|
|
dateStampExif: '',
|
|
|
|
freeText: this.findFileNameFreeTextPart(fileNameOriginal),
|
|
|
|
tags: this.findTagSectionInString(fileNameOriginal),
|
|
|
|
extension: this.findFileExtension(fileNameOriginal),
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* finds the earliest part in several exif date info
|
|
|
|
* @param exifData
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
static findEarliestDateInExifData (exifData) {
|
|
|
|
if (exifData) {
|
|
|
|
|
|
|
|
let moments = []
|
|
|
|
|
2023-07-04 15:56:15 +02:00
|
|
|
log.debug(' finder - exif data : ', exifData) // Do something with your data!
|
2023-07-01 12:35:33 +02:00
|
|
|
if (exifData.DateTimeOriginal) {
|
2023-07-04 15:56:15 +02:00
|
|
|
log.debug(' finder - image créée le : DateTimeOriginal : ', exifData.DateTimeOriginal) // Do something with your data!
|
2023-07-01 12:35:33 +02:00
|
|
|
moments.push(exifData.DateTimeOriginal)
|
|
|
|
}
|
|
|
|
if (exifData.ModificationDateTime) {
|
2023-07-04 15:56:15 +02:00
|
|
|
log.debug(' finder - image créée le : ModificationDateTime : ', exifData.ModificationDateTime) // Do something with your data!
|
2023-07-01 12:35:33 +02:00
|
|
|
moments.push(exifData.ModificationDateTime)
|
|
|
|
}
|
|
|
|
if (exifData.ModifyDate) {
|
2023-07-04 15:56:15 +02:00
|
|
|
log.debug(' finder - image créée le : ModifyDate : ', exifData.ModifyDate) // Do something with your data!
|
2023-07-01 12:35:33 +02:00
|
|
|
moments.push(exifData.ModifyDate)
|
|
|
|
}
|
|
|
|
if (exifData.FileAccessDateTime) {
|
|
|
|
moments.push(exifData.FileAccessDateTime)
|
|
|
|
}
|
|
|
|
if (exifData.FileInodeChangeDateTime) {
|
|
|
|
moments.push(exifData.FileInodeChangeDateTime)
|
|
|
|
}
|
|
|
|
if (exifData.FileModificationDateTime) {
|
2023-07-04 15:56:15 +02:00
|
|
|
log.debug(' finder - image créée le : FileModificationDateTime : ', exifData.FileModificationDateTime) // Do something with your data!
|
2023-07-01 12:35:33 +02:00
|
|
|
moments.push(exifData.FileModificationDateTime)
|
|
|
|
}
|
|
|
|
if (exifData.CreateDate) {
|
2023-07-04 15:56:15 +02:00
|
|
|
log.debug(' finder - image créée le : CreateDate : ', exifData.CreateDate) // Do something with your data!
|
2023-07-01 12:35:33 +02:00
|
|
|
moments.push(exifData.CreateDate)
|
|
|
|
}
|
|
|
|
|
|
|
|
moments = moments.map(d => {
|
|
|
|
let newdate = moment(d)
|
|
|
|
return newdate
|
|
|
|
})
|
|
|
|
let minDate = moment.min(moments)
|
|
|
|
|
2023-07-04 15:56:15 +02:00
|
|
|
log.debug(' finder - minDate :::::::::', minDate)
|
2023-07-04 16:14:25 +02:00
|
|
|
log.info(' finder - minDate :::::::::', minDate.format(rangement_instance.iso_date_format))
|
2023-07-01 12:35:33 +02:00
|
|
|
|
2023-07-04 16:14:25 +02:00
|
|
|
return minDate.format(rangement_instance.iso_date_format)
|
2023-07-01 12:35:33 +02:00
|
|
|
} else {
|
2023-07-04 15:56:15 +02:00
|
|
|
log.debug(' finder - pas de exif data')
|
2023-07-01 12:35:33 +02:00
|
|
|
return ''
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-05 22:36:34 +02:00
|
|
|
static findTemplateInFileName (fileName) {
|
2023-07-04 15:56:15 +02:00
|
|
|
// test all templates from configuration
|
|
|
|
}
|
|
|
|
|
2023-07-01 12:35:33 +02:00
|
|
|
/**
|
|
|
|
* examine plusieurs propriétés exif de date et retourne la plus ancienne
|
|
|
|
* @param filepath
|
|
|
|
*/
|
|
|
|
static async findExifCreationDate (filepath) {
|
|
|
|
|
2023-07-04 15:56:15 +02:00
|
|
|
log.debug(' finder - filepath', filepath)
|
2023-07-01 12:35:33 +02:00
|
|
|
let dateAlreadyInFileName = finder.findFormattedDate(filepath)
|
|
|
|
if (dateAlreadyInFileName) {
|
|
|
|
|
2023-07-04 15:56:15 +02:00
|
|
|
log.debug(' finder - ------ dateAlreadyInFileName', dateAlreadyInFileName)
|
2023-07-01 12:35:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return await exifr.parse(filepath)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static findFolderPath (filePath) {
|
|
|
|
let folders = filePath.split('/')
|
|
|
|
let fileName = folders.pop()
|
|
|
|
folders = filePath.replace(fileName, '')
|
|
|
|
|
2023-07-04 15:56:15 +02:00
|
|
|
log.debug(' finder - \n - folders', folders)
|
|
|
|
log.debug(' finder - - fileName', fileName, '\n')
|
2023-07-05 22:36:34 +02:00
|
|
|
return [folders, fileName]
|
2023-07-01 12:35:33 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|