From 00207c8e39f2054e6a3ed806eca72fdf732f4700 Mon Sep 17 00:00:00 2001 From: Tykayn Date: Tue, 27 Jun 2023 15:40:50 +0200 Subject: [PATCH] guess file name in Rangement --- rangement/README.md | 4 ++ rangement/index.mjs | 112 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 116 insertions(+) create mode 100644 rangement/README.md create mode 100644 rangement/index.mjs diff --git a/rangement/README.md b/rangement/README.md new file mode 100644 index 00000000..4ad21a81 --- /dev/null +++ b/rangement/README.md @@ -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. diff --git a/rangement/index.mjs b/rangement/index.mjs new file mode 100644 index 00000000..59894efc --- /dev/null +++ b/rangement/index.mjs @@ -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 +} \ No newline at end of file