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 finders from './finders.mjs'
// list.map(folder => {
// getExifCreationDate(filepath)
// })
import exifr from 'exifr'
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
@ -24,18 +28,11 @@ function convertDateToTimeInFileName (inputDate) {
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) {
return inputString.match(/screenshot/i) || inputString.match(/capture d'écran/i)
}
function getExifCreationDate(filepath) {
console.log('filepath', filepath)
let dateAlreadyInFileName = findFormattedDate(filepath)
let dateAlreadyInFileName = finders.findFormattedDate(filepath)
console.log('------ dateAlreadyInFileName', dateAlreadyInFileName)
exifr.parse(filepath).then(exifData => {
@ -81,20 +78,13 @@ function getExifCreationDate (filepath) {
}
const tagSeparator = ' '
const tagSectionSeparator = '--'
let fileSectionsName
function findFileExtension (inputString) {
let match = inputString.match(/\.\w{3,4}$/i)
// console.log('match', match)
return match
}
function findTagSectionInString(inputString) {
let listOfTags = []
// remove extension
let extensionFile = findFileExtension(inputString)
let extensionFile = finders.findFileExtension(inputString)
if (extensionFile) {
@ -136,7 +126,7 @@ function renameFile (originalFileName, fileMixedNewName) {
// console.log('list', list)
let originalFileName = '2023-06-23T08.55.05.jpg'
let formattedDatePIMBefore = findFormattedDate(originalFileName)
let formattedDatePIMBefore = finders.findFormattedDate(originalFileName)
console.log('formattedDatePIMBefore', formattedDatePIMBefore)
let creationDateFound = getExifCreationDate(pathFolder + '/' + originalFileName)
@ -154,21 +144,6 @@ if (fileMixedNewName !== originalFileName) {
// renameFile(originalFileName, fileMixedNewName)
}
/**
* find the section of file name which contains the free text to describe the picture
* @param fileName
* @returns {*|string}
*/
function findFileNameFreeTextPart (fileName) {
fileName = fileName.replace(findFileExtension(fileName), '')
let boom = fileName.split(tagSectionSeparator)
if (boom.length) {
let freeTextPart = boom[0].trim()
console.log('freeTextPart', freeTextPart)
return freeTextPart
}
return fileName.trim()
}
function addTagInFileName(tagName, fileName) {
@ -179,16 +154,10 @@ function addTagInFileName (tagName, fileName) {
let uniqueArray = [...new Set(tags)]
let newFileName = firstPart + ' ' + tagSectionSeparator + ' ' + tags.join(tagSeparator)
newFileName = newFileName.replace(/ {*}/, '') + findFileExtension(fileName)
newFileName = newFileName.replace(/ {*}/, '') + finders.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 = {
dateStamp: '',
freeText: '',
@ -198,15 +167,14 @@ const fileDefinition = {
function destructurateFileName(fileName) {
return {
dateStamp: findFormattedDate(fileName),
dateStamp: finders.findFormattedDate(fileName),
freeText: findFileNameFreeTextPart(fileName),
tags: findTagSectionInString(fileName),
extension: findFileExtension(fileName),
extension: finders.findFileExtension(fileName),
}
}
function cleanSpaces(inputString) {
return inputString.trim().replace(/ *g/, ' ')
}
@ -232,7 +200,7 @@ function searchAndReplaInFileName (searchString, replaceString, fileName) {
// findTagSectionInString('2023-06-23T18.36.47 -- machin bidule.jpg')
function searchAndRenameScreenshots(fileName) {
if (findScreenshot(fileName)) {
if (finders.findScreenshot(fileName)) {
let tags = findTagSectionInString(fileName)
console.log('tags', tags)
if (!tags.includes('screenshot')) {
@ -247,6 +215,7 @@ function searchAndRenameScreenshots(fileName){
return null
}
}
function TestScreenShotIsFoundAndRenamed() {
let screenShotMockFileName = 'Screenshot 2023-06-15 at 15-28-21 Instance Panoramax OSM-FR.png'
@ -261,6 +230,7 @@ function TestScreenShotIsFoundAndRenamed () {
console.log(screenShotMockFileNameExpected)
}
}
TestScreenShotIsFoundAndRenamed()
/**
@ -272,7 +242,7 @@ TestScreenShotIsFoundAndRenamed()
function TestDateIsDetectedInFileName() {
let mockFileName = 'Capture d\'écran 2023-06-15T10:11:12.png'
let expectedFileNameAfterRename = '2023-06-15T10:11:12 -- screeenshot.png'
let foundDate = findFormattedDate(mockFileName)
let foundDate = finders.findFormattedDate(mockFileName)
console.log('foundDate', foundDate)
}
@ -280,7 +250,6 @@ function TestDateIsDetectedInFileName () {
TestDateIsDetectedInFileName()
/**
----------------------- parties non réalisées -----------------------
// TODO
@ -291,8 +260,13 @@ function TestDownloadedTelegramPictureRename (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) {
@ -300,6 +274,10 @@ function moveToArchive (targetDirectory, fileFullPath) {
// rename file to move it
}
function moveToSortingFolder(fileFullPath) {
}
/**
* écrit un nouveau nom de fichier formatté
* @param convertedToName