2023-06-27 15:40:50 +02:00
|
|
|
import fs from 'node-fs'
|
2023-06-29 17:09:18 +02:00
|
|
|
import finders from './finders.mjs'
|
|
|
|
|
2023-06-27 15:40:50 +02:00
|
|
|
// 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'
|
2023-06-29 18:00:38 +02:00
|
|
|
import {tagSectionSeparator, tagSeparator} from "./configs.mjs";
|
2023-06-27 15:40:50 +02:00
|
|
|
|
2023-06-29 17:09:18 +02:00
|
|
|
const pathFolder = '/home/poule/encrypted/stockage-syncable/photos/a_dispatcher/tout'
|
|
|
|
const sortingFolder = '/home/poule/encrypted/stockage-syncable/photos/a_dispatcher'
|
2023-06-27 15:40:50 +02:00
|
|
|
|
|
|
|
// Replace with path to source directory
|
|
|
|
|
|
|
|
/**
|
|
|
|
* obtenir une liste des dossiers uniquement dans le dossier courant
|
|
|
|
* @param path
|
|
|
|
* @returns {*}
|
|
|
|
*/
|
2023-06-29 17:09:18 +02:00
|
|
|
function getDirectories(path) {
|
|
|
|
return fs.readdirSync(path).filter(function (file) {
|
|
|
|
return fs.statSync(path + '/' + file).isDirectory()
|
|
|
|
})
|
2023-06-27 15:40:50 +02:00
|
|
|
}
|
|
|
|
|
2023-06-29 17:09:18 +02:00
|
|
|
function convertDateToTimeInFileName(inputDate) {
|
|
|
|
return inputDate.replace(' ', 'T')
|
2023-06-27 23:03:35 +02:00
|
|
|
}
|
2023-06-27 16:06:22 +02:00
|
|
|
|
2023-06-27 18:12:43 +02:00
|
|
|
|
2023-06-29 17:09:18 +02:00
|
|
|
function getExifCreationDate(filepath) {
|
2023-06-27 16:06:22 +02:00
|
|
|
|
2023-06-29 17:09:18 +02:00
|
|
|
console.log('filepath', filepath)
|
|
|
|
let dateAlreadyInFileName = finders.findFormattedDate(filepath)
|
|
|
|
console.log('------ dateAlreadyInFileName', dateAlreadyInFileName)
|
2023-06-27 15:40:50 +02:00
|
|
|
|
2023-06-29 17:09:18 +02:00
|
|
|
exifr.parse(filepath).then(exifData => {
|
2023-06-27 18:12:43 +02:00
|
|
|
|
2023-06-29 17:09:18 +02:00
|
|
|
if (exifData) {
|
2023-06-27 15:40:50 +02:00
|
|
|
|
2023-06-29 17:09:18 +02:00
|
|
|
let moments = []
|
2023-06-27 15:40:50 +02:00
|
|
|
|
2023-06-29 17:09:18 +02:00
|
|
|
// console.log('exif data : ', exifData) // Do something with your data!
|
|
|
|
if (exifData.DateTimeOriginal) {
|
2023-06-27 15:40:50 +02:00
|
|
|
|
2023-06-29 17:09:18 +02:00
|
|
|
// console.log('image créée le : DateTimeOriginal : ', exifData.DateTimeOriginal) // Do something with your data!
|
|
|
|
moments.push(exifData.DateTimeOriginal)
|
|
|
|
}
|
|
|
|
if (exifData.ModifyDate) {
|
2023-06-27 15:40:50 +02:00
|
|
|
|
2023-06-29 17:09:18 +02:00
|
|
|
// console.log('image modifiée le : ModifyDate : ', exifData.ModifyDate) // Do something with your data!
|
|
|
|
moments.push(exifData.ModifyDate)
|
|
|
|
}
|
|
|
|
if (exifData.FileModificationDateTime) {
|
2023-06-27 18:12:43 +02:00
|
|
|
|
2023-06-29 17:09:18 +02:00
|
|
|
// console.log('image créée le : FileModificationDateTime : ', exifData.FileModificationDateTime) // Do something with your data!
|
|
|
|
moments.push(exifData.FileModificationDateTime)
|
|
|
|
}
|
|
|
|
if (exifData.CreateDate) {
|
2023-06-27 18:12:43 +02:00
|
|
|
|
2023-06-29 17:09:18 +02:00
|
|
|
// console.log('image créée le : CreateDate : ', exifData.CreateDate) // Do something with your data!
|
|
|
|
moments.push(exifData.CreateDate)
|
|
|
|
}
|
2023-06-27 18:12:43 +02:00
|
|
|
|
2023-06-29 17:09:18 +02:00
|
|
|
moments = moments.map(d => {
|
|
|
|
let newdate = moment(d)
|
|
|
|
return newdate
|
|
|
|
})
|
|
|
|
let minDate = moment.min(moments)
|
2023-06-27 18:12:43 +02:00
|
|
|
|
2023-06-29 17:09:18 +02:00
|
|
|
return minDate
|
|
|
|
} else {
|
|
|
|
console.log('pas de exif data')
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
}).catch(error => console.log('Error: ' + error.message))
|
2023-06-27 16:06:22 +02:00
|
|
|
|
2023-06-27 15:40:50 +02:00
|
|
|
}
|
|
|
|
|
2023-06-27 23:03:35 +02:00
|
|
|
let fileSectionsName
|
|
|
|
|
2023-06-27 18:12:43 +02:00
|
|
|
|
2023-06-29 17:09:18 +02:00
|
|
|
function findTagSectionInString(inputString) {
|
|
|
|
let listOfTags = []
|
|
|
|
// remove extension
|
|
|
|
let extensionFile = finders.findFileExtension(inputString)
|
|
|
|
|
|
|
|
if (extensionFile) {
|
|
|
|
|
|
|
|
extensionFile = extensionFile[0]
|
|
|
|
} else {
|
|
|
|
|
|
|
|
console.log('no extensionFile', extensionFile, inputString)
|
|
|
|
extensionFile = ''
|
|
|
|
}
|
|
|
|
|
|
|
|
inputString = inputString.replace(extensionFile, '')
|
|
|
|
|
|
|
|
console.log('extensionFile', extensionFile)
|
|
|
|
if (inputString.includes(tagSectionSeparator)) {
|
|
|
|
console.log('inputString', inputString)
|
|
|
|
if (inputString.length) {
|
|
|
|
|
|
|
|
let boom = inputString.split(tagSectionSeparator)
|
|
|
|
console.log('boom', boom)
|
|
|
|
if (boom.length) {
|
|
|
|
fileSectionsName = boom.splice(tagSeparator)
|
|
|
|
listOfTags = [...fileSectionsName[1].trim().split(tagSeparator)]
|
|
|
|
console.log('listOfTags', listOfTags)
|
|
|
|
} else {
|
|
|
|
console.log('no boom', boom)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return listOfTags
|
2023-06-27 16:19:39 +02:00
|
|
|
}
|
|
|
|
|
2023-06-29 17:09:18 +02:00
|
|
|
function renameFile(originalFileName, fileMixedNewName) {
|
|
|
|
fs.rename(originalFileName, fileMixedNewName, function (err) {
|
|
|
|
if (err) console.log('rename ERROR: ' + err)
|
|
|
|
})
|
2023-06-27 15:40:50 +02:00
|
|
|
}
|
|
|
|
|
2023-06-27 16:06:22 +02:00
|
|
|
// let list = getDirectories(pathFolder)
|
|
|
|
// console.log('list', list)
|
|
|
|
|
|
|
|
let originalFileName = '2023-06-23T08.55.05.jpg'
|
2023-06-29 17:09:18 +02:00
|
|
|
let formattedDatePIMBefore = finders.findFormattedDate(originalFileName)
|
2023-06-27 16:06:22 +02:00
|
|
|
console.log('formattedDatePIMBefore', formattedDatePIMBefore)
|
2023-06-27 15:40:50 +02:00
|
|
|
|
|
|
|
let creationDateFound = getExifCreationDate(pathFolder + '/' + originalFileName)
|
|
|
|
let convertedToName = ''
|
|
|
|
if (creationDateFound) {
|
2023-06-29 17:09:18 +02:00
|
|
|
convertedToName = convertDateToTimeInFileName(creationDateFound)
|
2023-06-27 15:40:50 +02:00
|
|
|
}
|
|
|
|
console.log('convertedToName', convertedToName)
|
|
|
|
let fileMixedNewName = mixDateNameWithFileName(convertedToName, originalFileName)
|
|
|
|
|
|
|
|
console.log('new name', fileMixedNewName)
|
|
|
|
|
|
|
|
if (fileMixedNewName !== originalFileName) {
|
2023-06-29 17:09:18 +02:00
|
|
|
console.log('renommage =>', fileMixedNewName)
|
|
|
|
// renameFile(originalFileName, fileMixedNewName)
|
2023-06-27 15:40:50 +02:00
|
|
|
}
|
|
|
|
|
2023-06-27 23:03:35 +02:00
|
|
|
|
2023-06-29 17:09:18 +02:00
|
|
|
function addTagInFileName(tagName, fileName) {
|
2023-06-27 23:03:35 +02:00
|
|
|
|
2023-06-29 17:09:18 +02:00
|
|
|
let tags = findTagSectionInString(fileName)
|
2023-06-29 18:00:38 +02:00
|
|
|
let firstPart = finders.findFileNameFreeTextPart(fileName)
|
2023-06-27 23:03:35 +02:00
|
|
|
|
2023-06-29 17:09:18 +02:00
|
|
|
tags.push(tagName)
|
|
|
|
let uniqueArray = [...new Set(tags)]
|
2023-06-27 23:03:35 +02:00
|
|
|
|
2023-06-29 17:09:18 +02:00
|
|
|
let newFileName = firstPart + ' ' + tagSectionSeparator + ' ' + tags.join(tagSeparator)
|
|
|
|
newFileName = newFileName.replace(/ {*}/, '') + finders.findFileExtension(fileName)
|
|
|
|
return cleanSpaces(newFileName)
|
2023-06-27 23:03:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const fileDefinition = {
|
2023-06-29 17:09:18 +02:00
|
|
|
dateStamp: '',
|
|
|
|
freeText: '',
|
|
|
|
tags: [],
|
|
|
|
extension: '',
|
2023-06-27 23:03:35 +02:00
|
|
|
}
|
|
|
|
|
2023-06-29 17:09:18 +02:00
|
|
|
function destructurateFileName(fileName) {
|
|
|
|
return {
|
|
|
|
dateStamp: finders.findFormattedDate(fileName),
|
2023-06-29 18:00:38 +02:00
|
|
|
freeText: finders.findFileNameFreeTextPart(fileName),
|
2023-06-29 17:09:18 +02:00
|
|
|
tags: findTagSectionInString(fileName),
|
|
|
|
extension: finders.findFileExtension(fileName),
|
|
|
|
}
|
2023-06-27 23:03:35 +02:00
|
|
|
}
|
|
|
|
|
2023-06-29 17:09:18 +02:00
|
|
|
function cleanSpaces(inputString) {
|
|
|
|
return inputString.trim().replace(/ *g/, ' ')
|
2023-06-27 23:03:35 +02:00
|
|
|
}
|
|
|
|
|
2023-06-29 17:09:18 +02:00
|
|
|
function makeFileNameFromProperties(fileProperties) {
|
|
|
|
return cleanSpaces(fileProperties.dateStamp + ' ' + fileProperties.freeText + ' ' + tagSectionSeparator + ' ' + fileProperties.tags.join(tagSeparator) + fileProperties.extension)
|
2023-06-27 23:03:35 +02:00
|
|
|
}
|
|
|
|
|
2023-06-29 17:09:18 +02:00
|
|
|
function appendFileName(fileProperties, newText) {
|
|
|
|
fileProperties.freeText = cleanSpaces(fileProperties.freeText + ' ' + newText)
|
|
|
|
return fileProperties
|
2023-06-27 23:03:35 +02:00
|
|
|
}
|
|
|
|
|
2023-06-29 17:09:18 +02:00
|
|
|
function prependFileName(fileProperties, newText) {
|
|
|
|
fileProperties.freeText = cleanSpaces(newText + ' ' + fileProperties.freeText)
|
|
|
|
return fileProperties
|
2023-06-27 23:03:35 +02:00
|
|
|
}
|
|
|
|
|
2023-06-29 17:09:18 +02:00
|
|
|
function searchAndReplaInFileName(searchString, replaceString, fileName) {
|
|
|
|
return cleanSpaces(fileName.replace(searchString, replaceString))
|
2023-06-27 23:03:35 +02:00
|
|
|
}
|
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')
|
2023-06-27 23:03:35 +02:00
|
|
|
// findTagSectionInString('2023-06-23T18.36.47 -- machin bidule.jpg')
|
|
|
|
|
2023-06-29 17:09:18 +02:00
|
|
|
function searchAndRenameScreenshots(fileName) {
|
|
|
|
if (finders.findScreenshot(fileName)) {
|
|
|
|
let tags = findTagSectionInString(fileName)
|
|
|
|
console.log('tags', tags)
|
|
|
|
if (!tags.includes('screenshot')) {
|
|
|
|
|
|
|
|
fileName = addTagInFileName('screenshot', fileName)
|
|
|
|
fileName = searchAndReplaInFileName('Screenshot', '', fileName)
|
|
|
|
console.log('screenShotMockFileName:', fileName)
|
|
|
|
return cleanSpaces(fileName)
|
|
|
|
}
|
|
|
|
console.log('is a screenshot, remove screenshot in name, and add tag screenshot')
|
|
|
|
} else {
|
|
|
|
return null
|
|
|
|
}
|
2023-06-27 23:18:00 +02:00
|
|
|
}
|
2023-06-29 17:09:18 +02:00
|
|
|
|
|
|
|
function TestScreenShotIsFoundAndRenamed() {
|
|
|
|
|
|
|
|
let screenShotMockFileName = 'Screenshot 2023-06-15 at 15-28-21 Instance Panoramax OSM-FR.png'
|
|
|
|
let screenShotMockFileNameExpected = '2023-06-15 at 15-28-21 Instance Panoramax OSM-FR -- screenshot.png'
|
|
|
|
let found = searchAndRenameScreenshots(screenShotMockFileName)
|
|
|
|
console.log('found', found)
|
|
|
|
if (found == screenShotMockFileNameExpected) {
|
|
|
|
console.log('TestScreenShotIsFoundAndRenamed : test succès')
|
|
|
|
} else {
|
|
|
|
console.log('TestScreenShotIsFoundAndRenamed : FAIL:')
|
|
|
|
console.log(found)
|
|
|
|
console.log(screenShotMockFileNameExpected)
|
|
|
|
}
|
2023-06-27 23:03:35 +02:00
|
|
|
}
|
2023-06-29 17:09:18 +02:00
|
|
|
|
2023-06-27 23:18:00 +02:00
|
|
|
TestScreenShotIsFoundAndRenamed()
|
|
|
|
|
|
|
|
/**
|
|
|
|
* work in progress
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2023-06-29 17:09:18 +02:00
|
|
|
function TestDateIsDetectedInFileName() {
|
|
|
|
let mockFileName = 'Capture d\'écran 2023-06-15T10:11:12.png'
|
|
|
|
let expectedFileNameAfterRename = '2023-06-15T10:11:12 -- screeenshot.png'
|
|
|
|
let foundDate = finders.findFormattedDate(mockFileName)
|
|
|
|
console.log('foundDate', foundDate)
|
2023-06-27 23:18:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// run tests
|
|
|
|
TestDateIsDetectedInFileName()
|
|
|
|
|
|
|
|
|
2023-06-27 23:03:35 +02:00
|
|
|
/**
|
|
|
|
----------------------- parties non réalisées -----------------------
|
2023-06-29 17:09:18 +02:00
|
|
|
// TODO
|
2023-06-27 23:03:35 +02:00
|
|
|
---------------------------------------------------------------------
|
|
|
|
**/
|
|
|
|
|
2023-06-29 17:09:18 +02:00
|
|
|
function TestDownloadedTelegramPictureRename(fileName) {
|
|
|
|
let fileProperties = destructurateFileName(fileName)
|
2023-06-27 23:03:35 +02:00
|
|
|
}
|
|
|
|
|
2023-06-29 17:09:18 +02:00
|
|
|
function hasDifferentDateInNameThanExif(fileName) {
|
|
|
|
let foundDate = finders.findFormattedDate(fileName);
|
2023-06-27 23:03:35 +02:00
|
|
|
|
2023-06-29 17:09:18 +02:00
|
|
|
if (foundDate && foundDate != getExifCreationDate(fileName)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2023-06-27 23:03:35 +02:00
|
|
|
}
|
|
|
|
|
2023-06-29 17:09:18 +02:00
|
|
|
function moveToArchive(targetDirectory, fileFullPath) {
|
|
|
|
// find current directory,
|
|
|
|
// rename file to move it
|
|
|
|
}
|
|
|
|
|
|
|
|
function moveToSortingFolder(fileFullPath) {
|
|
|
|
|
2023-06-27 23:18:00 +02:00
|
|
|
}
|
|
|
|
|
2023-06-27 23:03:35 +02:00
|
|
|
/**
|
|
|
|
* écrit un nouveau nom de fichier formatté
|
|
|
|
* @param convertedToName
|
|
|
|
* @param originalFileName
|
|
|
|
* @returns {*}
|
|
|
|
*/
|
2023-06-29 17:09:18 +02:00
|
|
|
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
|
2023-06-27 23:03:35 +02:00
|
|
|
}
|