chaining promises to find exif data before renaming
This commit is contained in:
parent
30c1189393
commit
aaf00bd4ba
|
@ -0,0 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="JavaScriptLibraryMappings">
|
||||
<includedPredefinedLibrary name="Node.js Core" />
|
||||
</component>
|
||||
</project>
|
|
@ -4,6 +4,7 @@
|
|||
import { tagSectionSeparator, tagSeparator } from './configs.mjs'
|
||||
import exifr from 'exifr'
|
||||
import moment from 'moment'
|
||||
import path from 'path'
|
||||
|
||||
/**
|
||||
* finds patterns for file name
|
||||
|
@ -11,7 +12,7 @@ import moment from 'moment'
|
|||
export default class finder {
|
||||
|
||||
static statistics = {
|
||||
filesModified : 0,
|
||||
filesModified: 0,
|
||||
}
|
||||
static patternsFiles = {
|
||||
'downloaded_pic': /^\-\w{15}\.jpg/, // FyB8cZnWIAc21rw.jpg
|
||||
|
@ -19,10 +20,12 @@ export default class finder {
|
|||
'open_camera': /^IMG_OC_\d{8}/i, // IMG_OC_20230617_092120_3.jpg
|
||||
'screenshot': /^Screenshot/i, // Screenshot 2023-06-15 at 15-26-04 Instance Panoramax OSM-FR.png
|
||||
}
|
||||
static reportStatistics(){
|
||||
|
||||
static reportStatistics () {
|
||||
console.log('statistics',
|
||||
this.statistics)
|
||||
}
|
||||
|
||||
static findScreenshot (inputString) {
|
||||
return inputString.match(/screenshot/i) || inputString.match(/capture d'écran/i)
|
||||
}
|
||||
|
@ -39,11 +42,6 @@ export default class finder {
|
|||
|
||||
static findFileExtension (inputString) {
|
||||
let result = inputString.match(/\.\w{3,4}$/i)
|
||||
// console.log('match findFileExtension', match)
|
||||
// let result = ''
|
||||
// if (match && match[0]) {
|
||||
// result = match[0]
|
||||
// }
|
||||
return result
|
||||
}
|
||||
|
||||
|
@ -143,15 +141,76 @@ export default class finder {
|
|||
|
||||
/**
|
||||
* convertit un nom de fichier en une structure décrivant plusieurs parties correspondant au pattern d'archivage
|
||||
* @param fileName
|
||||
* @param fullPath
|
||||
* @returns {{extension: *, dateStamp: string, freeText: (*|string), tags: *[]}}
|
||||
*/
|
||||
static destructurateFileName (fileName) {
|
||||
static destructurateFileName (fullPath) {
|
||||
let [folderPath, fileNameOriginal] = this.findFolderPath(fullPath)
|
||||
let dateStampInFileNameOriginal = this.findFormattedDate(fileNameOriginal)
|
||||
return {
|
||||
dateStamp: this.findFormattedDate(fileName),
|
||||
freeText: this.findFileNameFreeTextPart(fileName),
|
||||
tags: this.findTagSectionInString(fileName),
|
||||
extension: this.findFileExtension(fileName),
|
||||
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 = []
|
||||
|
||||
// console.log('exif data : ', exifData) // Do something with your data!
|
||||
if (exifData.DateTimeOriginal) {
|
||||
// console.log('image créée le : DateTimeOriginal : ', exifData.DateTimeOriginal) // Do something with your data!
|
||||
moments.push(exifData.DateTimeOriginal)
|
||||
}
|
||||
if (exifData.ModificationDateTime) {
|
||||
// console.log('image créée le : ModificationDateTime : ', exifData.ModificationDateTime) // Do something with your data!
|
||||
moments.push(exifData.ModificationDateTime)
|
||||
}
|
||||
if (exifData.ModifyDate) {
|
||||
// console.log('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) {
|
||||
// console.log('image créée le : FileModificationDateTime : ', exifData.FileModificationDateTime) // Do something with your data!
|
||||
moments.push(exifData.FileModificationDateTime)
|
||||
}
|
||||
if (exifData.CreateDate) {
|
||||
// console.log('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)
|
||||
|
||||
// console.log('minDate :::::::::', minDate)
|
||||
console.log('minDate :::::::::', minDate.format('yyyy-MM-DDTHH:mm:ss'))
|
||||
|
||||
return minDate.format('yyyy-MM-DDTHH:mm:ss')
|
||||
} else {
|
||||
console.log('pas de exif data')
|
||||
return ''
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -163,63 +222,23 @@ export default class finder {
|
|||
|
||||
console.log('filepath', filepath)
|
||||
let dateAlreadyInFileName = finder.findFormattedDate(filepath)
|
||||
if(dateAlreadyInFileName){
|
||||
if (dateAlreadyInFileName) {
|
||||
|
||||
console.log('------ dateAlreadyInFileName', dateAlreadyInFileName)
|
||||
console.log('------ dateAlreadyInFileName', dateAlreadyInFileName)
|
||||
}
|
||||
|
||||
await exifr.parse(filepath).then(exifData => {
|
||||
return await exifr.parse(filepath)
|
||||
|
||||
if (exifData) {
|
||||
|
||||
let moments = []
|
||||
|
||||
// console.log('exif data : ', exifData) // Do something with your data!
|
||||
if (exifData.DateTimeOriginal) {
|
||||
// console.log('image créée le : DateTimeOriginal : ', exifData.DateTimeOriginal) // Do something with your data!
|
||||
moments.push(exifData.DateTimeOriginal)
|
||||
}
|
||||
if (exifData.ModificationDateTime) {
|
||||
// console.log('image créée le : ModificationDateTime : ', exifData.ModificationDateTime) // Do something with your data!
|
||||
moments.push(exifData.ModificationDateTime)
|
||||
}
|
||||
if (exifData.ModifyDate) {
|
||||
// console.log('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) {
|
||||
// console.log('image créée le : FileModificationDateTime : ', exifData.FileModificationDateTime) // Do something with your data!
|
||||
moments.push(exifData.FileModificationDateTime)
|
||||
}
|
||||
if (exifData.CreateDate) {
|
||||
// console.log('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)
|
||||
|
||||
// console.log('minDate :::::::::', minDate)
|
||||
console.log('minDate :::::::::', minDate.format('yyyy-MM-DDTHH:mm:ss'))
|
||||
|
||||
return minDate.format('yyyy-MM-DDTHH:mm:ss')
|
||||
} else {
|
||||
console.log('pas de exif data')
|
||||
return ''
|
||||
}
|
||||
}).catch(error => {
|
||||
console.log('/////////// Error in reading exif of file: ' + error.message)
|
||||
return ''
|
||||
})
|
||||
}
|
||||
|
||||
static findFolderPath (filePath) {
|
||||
let folders = filePath.split('/')
|
||||
let fileName = folders.pop()
|
||||
folders = filePath.replace(fileName, '')
|
||||
|
||||
console.log('\n - folders', folders)
|
||||
console.log(' - fileName', fileName, '\n')
|
||||
return [folders, fileName]
|
||||
|
||||
}
|
||||
}
|
|
@ -11,7 +11,7 @@ import minimist from 'minimist'
|
|||
/** ---------------------
|
||||
custom utilities and configuration
|
||||
--------------------- */
|
||||
import { enableTestsLocally, reportStatistics, tagSectionSeparator, tagSeparator } from './configs.mjs'
|
||||
import { enableTestsLocally, reportStatistics,tagSectionSeparator, tagSeparator } from './configs.mjs'
|
||||
import {
|
||||
TestFindFormattedDate,
|
||||
TestScreenShotIsFoundAndRenamed,
|
||||
|
@ -35,15 +35,6 @@ function renameFile (originalFileName, fileMixedNewName) {
|
|||
})
|
||||
}
|
||||
|
||||
function makeFileNameFromProperties (fileProperties) {
|
||||
|
||||
let tagPlace = ''
|
||||
if (fileProperties.tags.length) {
|
||||
tagPlace = ' ' + tagSectionSeparator + ' '
|
||||
}
|
||||
return finder.cleanSpaces(fileProperties.dateStamp + ' ' + fileProperties.freeText + tagPlace + fileProperties.tags.join(tagSeparator) + fileProperties.extension).replace(+' ' + tagSectionSeparator + ' ' + '.', '.')
|
||||
}
|
||||
|
||||
function appendFileName (fileProperties, newText) {
|
||||
fileProperties.freeText = finder.cleanSpaces(fileProperties.freeText + ' ' + newText)
|
||||
return fileProperties
|
||||
|
@ -54,6 +45,31 @@ function prependFileName (fileProperties, newText) {
|
|||
return fileProperties
|
||||
}
|
||||
|
||||
function makeFileNameFromProperties(fileProperties) {
|
||||
|
||||
let tagPlace = ''
|
||||
if (fileProperties.tags.length) {
|
||||
tagPlace = ' ' + tagSectionSeparator + ' '
|
||||
}
|
||||
// return finder.cleanSpaces(fileProperties.dateStamp + ' ' + fileProperties.freeText + tagPlace + fileProperties.tags.join(tagSeparator) + fileProperties.extension).replace(+' ' + tagSectionSeparator + ' ' + '.', '.')
|
||||
return ''+fileProperties.dateStampExif + ' ' + fileProperties.freeText + tagPlace + fileProperties.tags.join(tagSeparator) + fileProperties.extension
|
||||
}
|
||||
|
||||
function shouldWeChangeName (structureForFile) {
|
||||
console.log(' ______ allez hop fini la recherche on fait un nouveau nom')
|
||||
console.log('structureForFile', structureForFile)
|
||||
let newName = makeFileNameFromProperties(structureForFile)
|
||||
if (structureForFile.fileNameOriginal !== newName) {
|
||||
|
||||
console.log('\n ancien nom :', structureForFile.fileNameOriginal)
|
||||
// console.log(' nouveau nom:', foundDate +structureForFile.freeText + structureForFile.tags.join(tagSeparator) + structureForFile.extension )
|
||||
console.log(' nouveau nom:', newName)
|
||||
} else {
|
||||
console.log(' rien à changer')
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
async function guessFileNameOnAllFilesFromArguments () {
|
||||
|
||||
// parcourir les dossiers
|
||||
|
@ -62,31 +78,42 @@ async function guessFileNameOnAllFilesFromArguments () {
|
|||
console.log('liste des fichiers', mini_arguments._)
|
||||
let fileList = mini_arguments._
|
||||
|
||||
fileList.forEach(fileName => {
|
||||
fileList.forEach(fullPath => {
|
||||
|
||||
let structureForFile = finder.destructurateFileName(fullPath)
|
||||
|
||||
let structureForFile = finder.destructurateFileName(fileName)
|
||||
// examiner les infos exif de chaque fichier pour proposer un nouveau nom
|
||||
if (!structureForFile.dateStamp) {
|
||||
let foundDate = finder.findExifCreationDate(fileName).then((data) => {
|
||||
if (!structureForFile.dateStampInFileNameOriginal) {
|
||||
console.log(' le nom de fichier ne contient pas de date formatée au début')
|
||||
|
||||
console.log(' =>>>>>>> foundDate : ', foundDate)
|
||||
if (foundDate) {
|
||||
structureForFile.dateStamp = finder.findExifCreationDate(fileName)
|
||||
finder.findExifCreationDate(structureForFile.fullPath)
|
||||
.then(data => {
|
||||
console.log(' ... chercher la date de création')
|
||||
let foundDate = finder.findEarliestDateInExifData(data)
|
||||
|
||||
} else {
|
||||
console.log(' pas de date trouvée dans le nom')
|
||||
}
|
||||
let newname = makeFileNameFromProperties(structureForFile)
|
||||
if (fileName !== newname) {
|
||||
console.log(' =>>>>>>> foundDate : ', foundDate)
|
||||
if (foundDate) {
|
||||
|
||||
console.log(' nouveau nom:', newname)
|
||||
} else {
|
||||
console.log(' rien à changer')
|
||||
}
|
||||
|
||||
}, (err) => {
|
||||
console.error(err)
|
||||
})
|
||||
// finder.findEarliestDateInExifData(fullPath).then(response => {
|
||||
// console.log(' ... trouvée')
|
||||
// if (response) {
|
||||
structureForFile.dateStampExif = foundDate
|
||||
|
||||
shouldWeChangeName(structureForFile)
|
||||
// }
|
||||
// })
|
||||
|
||||
} else {
|
||||
console.log('pas de date trouvée dans le nom')
|
||||
}
|
||||
|
||||
}
|
||||
,
|
||||
(error) => {
|
||||
console.log('/////////// Error in reading exif of file: ' + error.message)
|
||||
return ''
|
||||
})
|
||||
}
|
||||
}
|
||||
)
|
||||
|
|
Loading…
Reference in New Issue