240 lines
6.7 KiB
JavaScript
240 lines
6.7 KiB
JavaScript
/**
|
|
* la classe qui repère des patterns
|
|
*/
|
|
import rangement_instance , { tagSectionSeparator, tagSeparator} from './configs.mjs'
|
|
import exifr from 'exifr'
|
|
import moment from 'moment'
|
|
import log from "loglevel";
|
|
log.setLevel(rangement_instance.log_level)
|
|
|
|
|
|
/**
|
|
* finds patterns for file name
|
|
*/
|
|
export default class finder {
|
|
|
|
static statistics = {
|
|
filesModified: 0,
|
|
}
|
|
|
|
static reportStatistics () {
|
|
log.info('statistics',
|
|
this.statistics)
|
|
}
|
|
|
|
static findScreenshot (inputString) {
|
|
return inputString.match(/screenshot/i) || inputString.match(/capture d'écran/i)
|
|
}
|
|
|
|
static findFormattedDate (filepath) {
|
|
let match = filepath.match(/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/ig)
|
|
// log.debug('match findFormattedDate', match)
|
|
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), '')
|
|
let boom = fileName.split(tagSectionSeparator)
|
|
if (boom.length) {
|
|
let freeTextPart = boom[0].trim()
|
|
log.debug('freeTextPart', freeTextPart)
|
|
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 {
|
|
log.debug('no extensionFile', extensionFile, inputString)
|
|
extensionFile = ''
|
|
}
|
|
inputString = inputString.replace(extensionFile, '')
|
|
|
|
// log.debug('extensionFile', extensionFile)
|
|
if (inputString.includes(tagSectionSeparator)) {
|
|
// log.debug('inputString', inputString)
|
|
if (inputString.length) {
|
|
|
|
let boom = inputString.split(tagSectionSeparator)
|
|
// log.debug('boom', boom)
|
|
if (boom.length) {
|
|
let fileSectionsName = boom.splice(tagSeparator)
|
|
listOfTags = [...fileSectionsName[1].trim().split(tagSeparator)]
|
|
// log.debug('listOfTags', listOfTags)
|
|
} else {
|
|
log.debug('no boom', boom)
|
|
}
|
|
}
|
|
}
|
|
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)
|
|
log.debug('tags', tags)
|
|
if (!tags.includes('screenshot')) {
|
|
|
|
fileName = this.addTagInFileName('screenshot', fileName)
|
|
fileName = this.searchAndReplaceInFileName('Screenshot', '', fileName)
|
|
log.debug('screenShotMockFileName:', fileName)
|
|
return this.cleanSpaces(fileName)
|
|
}
|
|
log.debug('is a screenshot, remove screenshot in name, and add tag screenshot')
|
|
} 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)]
|
|
|
|
let newFileName = firstPart + ' ' + tagSectionSeparator + ' ' + tags.join(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) {
|
|
let [folderPath, fileNameOriginal] = this.findFolderPath(fullPath)
|
|
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 = []
|
|
|
|
// log.debug('exif data : ', exifData) // Do something with your data!
|
|
if (exifData.DateTimeOriginal) {
|
|
// log.debug('image créée le : DateTimeOriginal : ', exifData.DateTimeOriginal) // Do something with your data!
|
|
moments.push(exifData.DateTimeOriginal)
|
|
}
|
|
if (exifData.ModificationDateTime) {
|
|
// log.debug('image créée le : ModificationDateTime : ', exifData.ModificationDateTime) // Do something with your data!
|
|
moments.push(exifData.ModificationDateTime)
|
|
}
|
|
if (exifData.ModifyDate) {
|
|
// log.debug('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('image créée le : FileModificationDateTime : ', exifData.FileModificationDateTime) // Do something with your data!
|
|
moments.push(exifData.FileModificationDateTime)
|
|
}
|
|
if (exifData.CreateDate) {
|
|
// log.debug('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('minDate :::::::::', minDate)
|
|
log.debug('minDate :::::::::', minDate.format('yyyy-MM-DDTHH:mm:ss'))
|
|
|
|
return minDate.format('yyyy-MM-DDTHH:mm:ss')
|
|
} else {
|
|
log.debug('pas de exif data')
|
|
return ''
|
|
}
|
|
}
|
|
|
|
/**
|
|
* examine plusieurs propriétés exif de date et retourne la plus ancienne
|
|
* @param filepath
|
|
*/
|
|
static async findExifCreationDate (filepath) {
|
|
|
|
log.debug('filepath', filepath)
|
|
let dateAlreadyInFileName = finder.findFormattedDate(filepath)
|
|
if (dateAlreadyInFileName) {
|
|
|
|
log.debug('------ dateAlreadyInFileName', dateAlreadyInFileName)
|
|
}
|
|
|
|
return await exifr.parse(filepath)
|
|
|
|
}
|
|
|
|
static findFolderPath (filePath) {
|
|
let folders = filePath.split('/')
|
|
let fileName = folders.pop()
|
|
folders = filePath.replace(fileName, '')
|
|
|
|
log.debug('\n - folders', folders)
|
|
log.debug(' - fileName', fileName, '\n')
|
|
return [folders, fileName]
|
|
|
|
}
|
|
} |