guess file name in Rangement

This commit is contained in:
Tykayn 2023-06-27 15:40:50 +02:00 committed by tykayn
parent c7d4fa91c2
commit 00207c8e39
2 changed files with 116 additions and 0 deletions

4
rangement/README.md Normal file
View File

@ -0,0 +1,4 @@
# rangement
renomme des fichiers selon un pattern de date
trouve des infos exif et prend la plus ancienne pour renseigner le nom de fichier.

112
rangement/index.mjs Normal file
View File

@ -0,0 +1,112 @@
import fs from 'node-fs'
// list.map(folder => {
// getExifCreationDate(filepath)
// })
import exif from 'node-exif'
import moment from 'moment'
const pathFolder = '/home/poule/encrypted/stockage-syncable/photos/a_dispatcher'
// Replace with path to source directory
/**
* obtenir une liste des dossiers uniquement dans le dossier courant
* @param path
* @returns {*}
*/
function getDirectories (path) {
return fs.readdirSync(path).filter(function (file) {
return fs.statSync(path + '/' + file).isDirectory()
})
}
function convertDateToTimeInFileName (inputDate) {
return inputDate.replace(' ', 'T')
}
function getExifCreationDate (filepath) {
console.log('filepath', filepath)
try {
new exif.ExifImage({ image: filepath }, function (error, exifData) {
if (error) {
console.log(exifData)
console.log('Error: ' + error.message)
} else if (exifData) {
let moments = []
moments = moments.map(d => moment(d))
console.log('exif data : ', exifData) // Do something with your data!
if (exifData.exif.DateTimeOriginal) {
console.log('image créée le : DateTimeOriginal : ', exifData.exif.DateTimeOriginal) // Do something with your data!
moments.push(exifData.exif.DateTimeOriginal)
}
if (exifData.exif.FileModificationDateTime) {
console.log('image créée le : FileModificationDateTime : ', exifData.exif.FileModificationDateTime) // Do something with your data!
moments.push(exifData.exif.FileModificationDateTime)
}
if (exifData.exif.CreateDate) {
console.log('image créée le : CreateDate : ', exifData.exif.CreateDate) // Do something with your data!
moments.push(exifData.exif.CreateDate)
}
let minDate = moment.min(moments)
console.log('minDate', minDate)
return minDate
} else {
console.log('pas de exif data')
return null
}
})
} catch (error) {
console.log('Error: ' + error.message)
}
}
/**
* écrit un nouveau nom de fichier formatté
* @param convertedToName
* @param originalFileName
* @returns {*}
*/
function mixDateNameWithFileName (convertedToName, originalFileName) {
// enlever l'ancien timestamp si il existe
// ajouter en début de nom le nouveau timestamp avec un espace et conserver le reste du nom
return originalFileName
}
function renameFile (originalFileName, fileMixedNewName) {
fs.rename(originalFileName, fileMixedNewName, function (err) {
if (err) console.log('rename ERROR: ' + err)
})
}
let list = getDirectories(pathFolder)
console.log('list', list)
let originalFileName = 'FzPHzOrWcAE6ALV.jpg'
let creationDateFound = getExifCreationDate(pathFolder + '/' + originalFileName)
let convertedToName = ''
if (creationDateFound) {
convertedToName = convertDateToTimeInFileName(creationDateFound)
}
console.log('convertedToName', convertedToName)
let fileMixedNewName = mixDateNameWithFileName(convertedToName, originalFileName)
console.log('new name', fileMixedNewName)
if (fileMixedNewName !== originalFileName) {
console.log('renommage =>', fileMixedNewName)
// renameFile(originalFileName, fileMixedNewName)
}
const patternsFiles = {
'downloaded_pic': '', // FyB8cZnWIAc21rw.jpg
'telegram_pic': '', // -4900281569878475578_1109.jpg
'open_camera': '^IMG_OC', // IMG_OC_20230617_092120_3.jpg
'screenshot': '^Screenshot', // Screenshot 2023-06-15 at 15-26-04 Instance Panoramax OSM-FR.png
}