chaining promises to find exif data before renaming
This commit is contained in:
parent
30c1189393
commit
aaf00bd4ba
6
.idea/jsLibraryMappings.xml
generated
Normal file
6
.idea/jsLibraryMappings.xml
generated
Normal file
@ -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 { tagSectionSeparator, tagSeparator } from './configs.mjs'
|
||||||
import exifr from 'exifr'
|
import exifr from 'exifr'
|
||||||
import moment from 'moment'
|
import moment from 'moment'
|
||||||
|
import path from 'path'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* finds patterns for file name
|
* finds patterns for file name
|
||||||
@ -11,7 +12,7 @@ import moment from 'moment'
|
|||||||
export default class finder {
|
export default class finder {
|
||||||
|
|
||||||
static statistics = {
|
static statistics = {
|
||||||
filesModified : 0,
|
filesModified: 0,
|
||||||
}
|
}
|
||||||
static patternsFiles = {
|
static patternsFiles = {
|
||||||
'downloaded_pic': /^\-\w{15}\.jpg/, // FyB8cZnWIAc21rw.jpg
|
'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
|
'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
|
'screenshot': /^Screenshot/i, // Screenshot 2023-06-15 at 15-26-04 Instance Panoramax OSM-FR.png
|
||||||
}
|
}
|
||||||
static reportStatistics(){
|
|
||||||
|
static reportStatistics () {
|
||||||
console.log('statistics',
|
console.log('statistics',
|
||||||
this.statistics)
|
this.statistics)
|
||||||
}
|
}
|
||||||
|
|
||||||
static findScreenshot (inputString) {
|
static findScreenshot (inputString) {
|
||||||
return inputString.match(/screenshot/i) || inputString.match(/capture d'écran/i)
|
return inputString.match(/screenshot/i) || inputString.match(/capture d'écran/i)
|
||||||
}
|
}
|
||||||
@ -39,11 +42,6 @@ export default class finder {
|
|||||||
|
|
||||||
static findFileExtension (inputString) {
|
static findFileExtension (inputString) {
|
||||||
let result = inputString.match(/\.\w{3,4}$/i)
|
let result = inputString.match(/\.\w{3,4}$/i)
|
||||||
// console.log('match findFileExtension', match)
|
|
||||||
// let result = ''
|
|
||||||
// if (match && match[0]) {
|
|
||||||
// result = match[0]
|
|
||||||
// }
|
|
||||||
return result
|
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
|
* 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: *[]}}
|
* @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 {
|
return {
|
||||||
dateStamp: this.findFormattedDate(fileName),
|
fullPath,
|
||||||
freeText: this.findFileNameFreeTextPart(fileName),
|
folderPath,
|
||||||
tags: this.findTagSectionInString(fileName),
|
fileNameOriginal,
|
||||||
extension: this.findFileExtension(fileName),
|
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)
|
console.log('filepath', filepath)
|
||||||
let dateAlreadyInFileName = finder.findFormattedDate(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
|
custom utilities and configuration
|
||||||
--------------------- */
|
--------------------- */
|
||||||
import { enableTestsLocally, reportStatistics, tagSectionSeparator, tagSeparator } from './configs.mjs'
|
import { enableTestsLocally, reportStatistics,tagSectionSeparator, tagSeparator } from './configs.mjs'
|
||||||
import {
|
import {
|
||||||
TestFindFormattedDate,
|
TestFindFormattedDate,
|
||||||
TestScreenShotIsFoundAndRenamed,
|
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) {
|
function appendFileName (fileProperties, newText) {
|
||||||
fileProperties.freeText = finder.cleanSpaces(fileProperties.freeText + ' ' + newText)
|
fileProperties.freeText = finder.cleanSpaces(fileProperties.freeText + ' ' + newText)
|
||||||
return fileProperties
|
return fileProperties
|
||||||
@ -54,6 +45,31 @@ function prependFileName (fileProperties, newText) {
|
|||||||
return fileProperties
|
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 () {
|
async function guessFileNameOnAllFilesFromArguments () {
|
||||||
|
|
||||||
// parcourir les dossiers
|
// parcourir les dossiers
|
||||||
@ -62,31 +78,42 @@ async function guessFileNameOnAllFilesFromArguments () {
|
|||||||
console.log('liste des fichiers', mini_arguments._)
|
console.log('liste des fichiers', mini_arguments._)
|
||||||
let fileList = 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
|
// examiner les infos exif de chaque fichier pour proposer un nouveau nom
|
||||||
if (!structureForFile.dateStamp) {
|
if (!structureForFile.dateStampInFileNameOriginal) {
|
||||||
let foundDate = finder.findExifCreationDate(fileName).then((data) => {
|
console.log(' le nom de fichier ne contient pas de date formatée au début')
|
||||||
|
|
||||||
console.log(' =>>>>>>> foundDate : ', foundDate)
|
finder.findExifCreationDate(structureForFile.fullPath)
|
||||||
if (foundDate) {
|
.then(data => {
|
||||||
structureForFile.dateStamp = finder.findExifCreationDate(fileName)
|
console.log(' ... chercher la date de création')
|
||||||
|
let foundDate = finder.findEarliestDateInExifData(data)
|
||||||
|
|
||||||
} else {
|
console.log(' =>>>>>>> foundDate : ', foundDate)
|
||||||
console.log(' pas de date trouvée dans le nom')
|
if (foundDate) {
|
||||||
}
|
|
||||||
let newname = makeFileNameFromProperties(structureForFile)
|
|
||||||
if (fileName !== newname) {
|
|
||||||
|
|
||||||
console.log(' nouveau nom:', newname)
|
|
||||||
} else {
|
|
||||||
console.log(' rien à changer')
|
|
||||||
}
|
|
||||||
|
|
||||||
}, (err) => {
|
// finder.findEarliestDateInExifData(fullPath).then(response => {
|
||||||
console.error(err)
|
// 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
Block a user