split rangement files

This commit is contained in:
Tykayn 2023-06-29 17:09:18 +02:00 committed by tykayn
parent 30ec55c1c8
commit 6427502a43
3 changed files with 224 additions and 198 deletions

3
rangement/configs.mjs Normal file
View File

@ -0,0 +1,3 @@
export const tagSeparator = ' '
export const tagSectionSeparator = '--'

45
rangement/finders.mjs Normal file
View File

@ -0,0 +1,45 @@
/**
* la classe qui repère des patterns
*/
import {tagSectionSeparator} from "./configs";
export default class finders{
patternsFiles = {
'downloaded_pic': /^\-\w{15}\.jpg/, // FyB8cZnWIAc21rw.jpg
'telegram_pic': /^\-\d{19}_\d{4}/, // -4900281569878475578_1109.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
}
static findScreenshot (inputString) {
return inputString.match(/screenshot/i) || inputString.match(/capture d'écran/i)
}
static findFormattedDate(filepath) {
return filepath.match(/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/ig)
}
static findFileExtension (inputString) {
let match = inputString.match(/\.\w{3,4}$/i)
return match
}
/**
* find the section of file name which contains the free text to describe the picture
* @param fileName
* @returns {*|string}
*/
static findFileNameFreeTextPart(fileName) {
fileName = fileName.replace(this.findFileExtension(fileName), '')
let boom = fileName.split(tagSectionSeparator)
if (boom.length) {
let freeTextPart = boom[0].trim()
console.log('freeTextPart', freeTextPart)
return freeTextPart
}
return fileName.trim()
}
}

View File

@ -1,11 +1,15 @@
import fs from 'node-fs' import fs from 'node-fs'
import finders from './finders.mjs'
// list.map(folder => { // list.map(folder => {
// getExifCreationDate(filepath) // getExifCreationDate(filepath)
// }) // })
import exifr from 'exifr' import exifr from 'exifr'
import moment from 'moment' import moment from 'moment'
import {tagSectionSeparator, tagSeparator} from "./configs";
const pathFolder = '/home/poule/encrypted/stockage-syncable/photos/a_dispatcher' const pathFolder = '/home/poule/encrypted/stockage-syncable/photos/a_dispatcher/tout'
const sortingFolder = '/home/poule/encrypted/stockage-syncable/photos/a_dispatcher'
// Replace with path to source directory // Replace with path to source directory
@ -14,135 +18,121 @@ const pathFolder = '/home/poule/encrypted/stockage-syncable/photos/a_dispatcher'
* @param path * @param path
* @returns {*} * @returns {*}
*/ */
function getDirectories (path) { function getDirectories(path) {
return fs.readdirSync(path).filter(function (file) { return fs.readdirSync(path).filter(function (file) {
return fs.statSync(path + '/' + file).isDirectory() return fs.statSync(path + '/' + file).isDirectory()
}) })
} }
function convertDateToTimeInFileName (inputDate) { function convertDateToTimeInFileName(inputDate) {
return inputDate.replace(' ', 'T') return inputDate.replace(' ', 'T')
} }
function findFormattedDate (inputString) {
return inputString.match(/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/ig)
}
function findScreenshot (inputString) { function getExifCreationDate(filepath) {
return inputString.match(/screenshot/i) || inputString.match(/capture d'écran/i)
}
function getExifCreationDate (filepath) { console.log('filepath', filepath)
let dateAlreadyInFileName = finders.findFormattedDate(filepath)
console.log('------ dateAlreadyInFileName', dateAlreadyInFileName)
console.log('filepath', filepath) exifr.parse(filepath).then(exifData => {
let dateAlreadyInFileName = findFormattedDate(filepath)
console.log('------ dateAlreadyInFileName', dateAlreadyInFileName)
exifr.parse(filepath).then(exifData => { if (exifData) {
if (exifData) { let moments = []
let moments = [] // console.log('exif data : ', exifData) // Do something with your data!
if (exifData.DateTimeOriginal) {
// console.log('exif data : ', exifData) // Do something with your data! // console.log('image créée le : DateTimeOriginal : ', exifData.DateTimeOriginal) // Do something with your data!
if (exifData.DateTimeOriginal) { moments.push(exifData.DateTimeOriginal)
}
if (exifData.ModifyDate) {
// console.log('image créée le : DateTimeOriginal : ', exifData.DateTimeOriginal) // Do something with your data! // console.log('image modifiée le : ModifyDate : ', exifData.ModifyDate) // Do something with your data!
moments.push(exifData.DateTimeOriginal) moments.push(exifData.ModifyDate)
} }
if (exifData.ModifyDate) { if (exifData.FileModificationDateTime) {
// console.log('image modifiée le : ModifyDate : ', exifData.ModifyDate) // Do something with your data! // console.log('image créée le : FileModificationDateTime : ', exifData.FileModificationDateTime) // Do something with your data!
moments.push(exifData.ModifyDate) moments.push(exifData.FileModificationDateTime)
} }
if (exifData.FileModificationDateTime) { if (exifData.CreateDate) {
// console.log('image créée le : FileModificationDateTime : ', exifData.FileModificationDateTime) // Do something with your data! // console.log('image créée le : CreateDate : ', exifData.CreateDate) // Do something with your data!
moments.push(exifData.FileModificationDateTime) moments.push(exifData.CreateDate)
} }
if (exifData.CreateDate) {
// console.log('image créée le : CreateDate : ', exifData.CreateDate) // Do something with your data! moments = moments.map(d => {
moments.push(exifData.CreateDate) let newdate = moment(d)
} return newdate
})
let minDate = moment.min(moments)
moments = moments.map(d => { return minDate
let newdate = moment(d) } else {
return newdate console.log('pas de exif data')
}) return null
let minDate = moment.min(moments) }
}).catch(error => console.log('Error: ' + error.message))
return minDate
} else {
console.log('pas de exif data')
return null
}
}).catch(error => console.log('Error: ' + error.message))
} }
const tagSeparator = ' '
const tagSectionSeparator = '--'
let fileSectionsName let fileSectionsName
function findFileExtension (inputString) {
let match = inputString.match(/\.\w{3,4}$/i) function findTagSectionInString(inputString) {
// console.log('match', match) let listOfTags = []
return match // 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
} }
function findTagSectionInString (inputString) { function renameFile(originalFileName, fileMixedNewName) {
let listOfTags = [] fs.rename(originalFileName, fileMixedNewName, function (err) {
// remove extension if (err) console.log('rename ERROR: ' + err)
let extensionFile = 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
}
function renameFile (originalFileName, fileMixedNewName) {
fs.rename(originalFileName, fileMixedNewName, function (err) {
if (err) console.log('rename ERROR: ' + err)
})
} }
// let list = getDirectories(pathFolder) // let list = getDirectories(pathFolder)
// console.log('list', list) // console.log('list', list)
let originalFileName = '2023-06-23T08.55.05.jpg' let originalFileName = '2023-06-23T08.55.05.jpg'
let formattedDatePIMBefore = findFormattedDate(originalFileName) let formattedDatePIMBefore = finders.findFormattedDate(originalFileName)
console.log('formattedDatePIMBefore', formattedDatePIMBefore) console.log('formattedDatePIMBefore', formattedDatePIMBefore)
let creationDateFound = getExifCreationDate(pathFolder + '/' + originalFileName) let creationDateFound = getExifCreationDate(pathFolder + '/' + originalFileName)
let convertedToName = '' let convertedToName = ''
if (creationDateFound) { if (creationDateFound) {
convertedToName = convertDateToTimeInFileName(creationDateFound) convertedToName = convertDateToTimeInFileName(creationDateFound)
} }
console.log('convertedToName', convertedToName) console.log('convertedToName', convertedToName)
let fileMixedNewName = mixDateNameWithFileName(convertedToName, originalFileName) let fileMixedNewName = mixDateNameWithFileName(convertedToName, originalFileName)
@ -150,117 +140,97 @@ let fileMixedNewName = mixDateNameWithFileName(convertedToName, originalFileName
console.log('new name', fileMixedNewName) console.log('new name', fileMixedNewName)
if (fileMixedNewName !== originalFileName) { if (fileMixedNewName !== originalFileName) {
console.log('renommage =>', fileMixedNewName) console.log('renommage =>', fileMixedNewName)
// renameFile(originalFileName, fileMixedNewName) // renameFile(originalFileName, fileMixedNewName)
} }
/**
* find the section of file name which contains the free text to describe the picture function addTagInFileName(tagName, fileName) {
* @param fileName
* @returns {*|string} let tags = findTagSectionInString(fileName)
*/ let firstPart = findFileNameFreeTextPart(fileName)
function findFileNameFreeTextPart (fileName) {
fileName = fileName.replace(findFileExtension(fileName), '') tags.push(tagName)
let boom = fileName.split(tagSectionSeparator) let uniqueArray = [...new Set(tags)]
if (boom.length) {
let freeTextPart = boom[0].trim() let newFileName = firstPart + ' ' + tagSectionSeparator + ' ' + tags.join(tagSeparator)
console.log('freeTextPart', freeTextPart) newFileName = newFileName.replace(/ {*}/, '') + finders.findFileExtension(fileName)
return freeTextPart return cleanSpaces(newFileName)
}
return fileName.trim()
} }
function addTagInFileName (tagName, fileName) {
let tags = findTagSectionInString(fileName)
let firstPart = findFileNameFreeTextPart(fileName)
tags.push(tagName)
let uniqueArray = [...new Set(tags)]
let newFileName = firstPart + ' ' + tagSectionSeparator + ' ' + tags.join(tagSeparator)
newFileName = newFileName.replace(/ {*}/, '') + findFileExtension(fileName)
return cleanSpaces( newFileName)
}
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
}
const fileDefinition = { const fileDefinition = {
dateStamp: '', dateStamp: '',
freeText: '', freeText: '',
tags: [], tags: [],
extension: '', extension: '',
} }
function destructurateFileName (fileName) { function destructurateFileName(fileName) {
return { return {
dateStamp: findFormattedDate(fileName), dateStamp: finders.findFormattedDate(fileName),
freeText: findFileNameFreeTextPart(fileName), freeText: findFileNameFreeTextPart(fileName),
tags: findTagSectionInString(fileName), tags: findTagSectionInString(fileName),
extension: findFileExtension(fileName), extension: finders.findFileExtension(fileName),
} }
} }
function cleanSpaces (inputString) { function cleanSpaces(inputString) {
return inputString.trim().replace(/ *g/, ' ')
return inputString.trim().replace(/ *g/, ' ')
} }
function makeFileNameFromProperties (fileProperties) { function makeFileNameFromProperties(fileProperties) {
return cleanSpaces(fileProperties.dateStamp + ' ' + fileProperties.freeText + ' ' + tagSectionSeparator + ' ' + fileProperties.tags.join(tagSeparator) + fileProperties.extension) return cleanSpaces(fileProperties.dateStamp + ' ' + fileProperties.freeText + ' ' + tagSectionSeparator + ' ' + fileProperties.tags.join(tagSeparator) + fileProperties.extension)
} }
function appendFileName (fileProperties, newText) { function appendFileName(fileProperties, newText) {
fileProperties.freeText = cleanSpaces(fileProperties.freeText + ' ' + newText) fileProperties.freeText = cleanSpaces(fileProperties.freeText + ' ' + newText)
return fileProperties return fileProperties
} }
function prependFileName (fileProperties, newText) { function prependFileName(fileProperties, newText) {
fileProperties.freeText = cleanSpaces(newText + ' ' + fileProperties.freeText) fileProperties.freeText = cleanSpaces(newText + ' ' + fileProperties.freeText)
return fileProperties return fileProperties
} }
function searchAndReplaInFileName (searchString, replaceString, fileName) { function searchAndReplaInFileName(searchString, replaceString, fileName) {
return cleanSpaces(fileName.replace(searchString, replaceString)) return cleanSpaces(fileName.replace(searchString, replaceString))
} }
// getExifCreationDate('/home/poule/encrypted/stockage-syncable/photos/a_dispatcher/2023-06-23T18.36.47 -- machin bidule.jpg') // 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') // findTagSectionInString('2023-06-23T18.36.47 -- machin bidule.jpg')
function searchAndRenameScreenshots(fileName){ function searchAndRenameScreenshots(fileName) {
if (findScreenshot(fileName)) { if (finders.findScreenshot(fileName)) {
let tags = findTagSectionInString(fileName) let tags = findTagSectionInString(fileName)
console.log('tags', tags) console.log('tags', tags)
if (!tags.includes('screenshot')) { if (!tags.includes('screenshot')) {
fileName = addTagInFileName('screenshot', fileName) fileName = addTagInFileName('screenshot', fileName)
fileName = searchAndReplaInFileName('Screenshot', '', fileName) fileName = searchAndReplaInFileName('Screenshot', '', fileName)
console.log('screenShotMockFileName:', fileName) console.log('screenShotMockFileName:', fileName)
return cleanSpaces( fileName) return cleanSpaces(fileName)
} }
console.log('is a screenshot, remove screenshot in name, and add tag screenshot') console.log('is a screenshot, remove screenshot in name, and add tag screenshot')
}else{ } else {
return null return null
} }
} }
function TestScreenShotIsFoundAndRenamed () {
let screenShotMockFileName = 'Screenshot 2023-06-15 at 15-28-21 Instance Panoramax OSM-FR.png' function TestScreenShotIsFoundAndRenamed() {
let screenShotMockFileNameExpected = '2023-06-15 at 15-28-21 Instance Panoramax OSM-FR -- screenshot.png'
let found = searchAndRenameScreenshots(screenShotMockFileName) let screenShotMockFileName = 'Screenshot 2023-06-15 at 15-28-21 Instance Panoramax OSM-FR.png'
console.log('found', found) let screenShotMockFileNameExpected = '2023-06-15 at 15-28-21 Instance Panoramax OSM-FR -- screenshot.png'
if(found == screenShotMockFileNameExpected){ let found = searchAndRenameScreenshots(screenShotMockFileName)
console.log('TestScreenShotIsFoundAndRenamed : test succès') console.log('found', found)
}else{ if (found == screenShotMockFileNameExpected) {
console.log('TestScreenShotIsFoundAndRenamed : FAIL:') console.log('TestScreenShotIsFoundAndRenamed : test succès')
console.log(found) } else {
console.log(screenShotMockFileNameExpected) console.log('TestScreenShotIsFoundAndRenamed : FAIL:')
} console.log(found)
console.log(screenShotMockFileNameExpected)
}
} }
TestScreenShotIsFoundAndRenamed() TestScreenShotIsFoundAndRenamed()
/** /**
@ -269,35 +239,43 @@ TestScreenShotIsFoundAndRenamed()
*/ */
function TestDateIsDetectedInFileName () { function TestDateIsDetectedInFileName() {
let mockFileName = 'Capture d\'écran 2023-06-15T10:11:12.png' let mockFileName = 'Capture d\'écran 2023-06-15T10:11:12.png'
let expectedFileNameAfterRename = '2023-06-15T10:11:12 -- screeenshot.png' let expectedFileNameAfterRename = '2023-06-15T10:11:12 -- screeenshot.png'
let foundDate = findFormattedDate(mockFileName) let foundDate = finders.findFormattedDate(mockFileName)
console.log('foundDate', foundDate) console.log('foundDate', foundDate)
} }
// run tests // run tests
TestDateIsDetectedInFileName() TestDateIsDetectedInFileName()
/** /**
----------------------- parties non réalisées ----------------------- ----------------------- parties non réalisées -----------------------
// TODO // TODO
--------------------------------------------------------------------- ---------------------------------------------------------------------
**/ **/
function TestDownloadedTelegramPictureRename (fileName) { function TestDownloadedTelegramPictureRename(fileName) {
let fileProperties = destructurateFileName(fileName) let fileProperties = destructurateFileName(fileName)
} }
function hasDifferentDateInNameThanExif () { function hasDifferentDateInNameThanExif(fileName) {
let foundDate = finders.findFormattedDate(fileName);
if (foundDate && foundDate != getExifCreationDate(fileName)) {
return true;
}
return false;
} }
function moveToArchive (targetDirectory, fileFullPath) { function moveToArchive(targetDirectory, fileFullPath) {
// find current directory, // find current directory,
// rename file to move it // rename file to move it
}
function moveToSortingFolder(fileFullPath) {
} }
/** /**
@ -306,8 +284,8 @@ function moveToArchive (targetDirectory, fileFullPath) {
* @param originalFileName * @param originalFileName
* @returns {*} * @returns {*}
*/ */
function mixDateNameWithFileName (convertedToName, originalFileName) { function mixDateNameWithFileName(convertedToName, originalFileName) {
// enlever l'ancien timestamp si il existe // 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 // ajouter en début de nom le nouveau timestamp avec un espace et conserver le reste du nom
return originalFileName return originalFileName
} }