scripts/rangement/index.mjs

140 lines
4.1 KiB
JavaScript
Raw Normal View History

2023-06-27 15:40:50 +02:00
import fs from 'node-fs'
// list.map(folder => {
// getExifCreationDate(filepath)
// })
2023-06-27 16:06:22 +02:00
import exifr from 'exifr'
2023-06-27 15:40:50 +02:00
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')
}
2023-06-27 16:06:22 +02:00
function findFormattedDate(inputString){
return inputString.match(/^[0-9]{4}-?[0-9]{2}-?[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}$/)
}
2023-06-27 15:40:50 +02:00
function getExifCreationDate (filepath) {
2023-06-27 16:06:22 +02:00
2023-06-27 15:40:50 +02:00
console.log('filepath', filepath)
2023-06-27 16:06:22 +02:00
exifr.parse(filepath).then(exifData => {
if (exifData) {
2023-06-27 15:40:50 +02:00
let moments = []
2023-06-27 16:19:39 +02:00
// console.log('exif data : ', exifData) // Do something with your data!
2023-06-27 16:06:22 +02:00
if (exifData.DateTimeOriginal) {
2023-06-27 15:40:50 +02:00
2023-06-27 16:06:22 +02:00
// console.log('image créée le : DateTimeOriginal : ', exifData.DateTimeOriginal) // Do something with your data!
moments.push(exifData.DateTimeOriginal)
2023-06-27 15:40:50 +02:00
}
2023-06-27 16:06:22 +02:00
if (exifData.ModifyDate) {
2023-06-27 15:40:50 +02:00
2023-06-27 16:06:22 +02:00
// console.log('image modifiée le : ModifyDate : ', exifData.ModifyDate) // Do something with your data!
moments.push(exifData.ModifyDate)
2023-06-27 15:40:50 +02:00
}
2023-06-27 16:06:22 +02:00
if (exifData.FileModificationDateTime) {
2023-06-27 15:40:50 +02:00
2023-06-27 16:06:22 +02:00
// console.log('image créée le : FileModificationDateTime : ', exifData.FileModificationDateTime) // Do something with your data!
moments.push(exifData.FileModificationDateTime)
2023-06-27 15:40:50 +02:00
}
2023-06-27 16:06:22 +02:00
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
})
2023-06-27 15:40:50 +02:00
let minDate = moment.min(moments)
return minDate
} else {
console.log('pas de exif data')
return null
}
2023-06-27 16:06:22 +02:00
}).catch(error => console.log('Error: ' + error.message))
2023-06-27 15:40:50 +02:00
}
2023-06-27 16:19:39 +02:00
const tagSeparator = ' '
const tagSectionSeparator = '--'
function findTagSectionInString(inputString){
let listOfTags = []
if(inputString.includes(tagSectionSeparator)){
let boom = inputString.splice(tagSectionSeparator)
if(boom.length){
listOfTags = [...boom.splice(tagSeparator)]
}
}
return listOfTags;
}
2023-06-27 15:40:50 +02:00
/**
* é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)
})
}
2023-06-27 16:06:22 +02:00
// let list = getDirectories(pathFolder)
// console.log('list', list)
let originalFileName = '2023-06-23T08.55.05.jpg'
let formattedDatePIMBefore = findFormattedDate(originalFileName)
console.log('formattedDatePIMBefore', formattedDatePIMBefore)
2023-06-27 15:40:50 +02:00
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
2023-06-27 16:06:22 +02:00
}
2023-06-27 16:19:39 +02:00
// getExifCreationDate('/home/poule/encrypted/stockage-syncable/photos/a_dispatcher/2023-06-23T18.36.47 -- machin bidule.jpg')
findTagSectionInString('2023-06-23T18.36.47 -- machin bidule.jpg')