destructurate file name

This commit is contained in:
Tykayn 2023-06-27 23:03:35 +02:00 committed by tykayn
parent b1a5f6fe48
commit 4e1b774e8c
1 changed files with 134 additions and 20 deletions

View File

@ -25,13 +25,18 @@ function convertDateToTimeInFileName (inputDate) {
} }
function findFormattedDate (inputString) { function findFormattedDate (inputString) {
return inputString.matchAll(/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/i)
}
return inputString.match(/^[0-9]{4}-?[0-9]{2}-?[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}$/) function findScreenshot (inputString) {
return inputString.match(/screenshot/i) || inputString.match(/capture d'écran/i)
} }
function getExifCreationDate (filepath) { function getExifCreationDate (filepath) {
console.log('filepath', filepath) console.log('filepath', filepath)
let dateAlreadyInFileName = findFormattedDate(filepath)
console.log('------ dateAlreadyInFileName', dateAlreadyInFileName)
exifr.parse(filepath).then(exifData => { exifr.parse(filepath).then(exifData => {
@ -78,14 +83,27 @@ function getExifCreationDate (filepath) {
const tagSeparator = ' ' const tagSeparator = ' '
const tagSectionSeparator = '--' const tagSectionSeparator = '--'
let fileSectionsName; let fileSectionsName
function findFileExtension (inputString) {
let match = inputString.match(/\.\w{3,4}$/i)
// console.log('match', match)
return match
}
function findTagSectionInString (inputString) { function findTagSectionInString (inputString) {
let listOfTags = [] let listOfTags = []
// remove extension // remove extension
let extensionFile = inputString.match(/\.\w{3,4}$/i) let extensionFile = findFileExtension(inputString)
if (extensionFile) {
extensionFile = extensionFile[0] extensionFile = extensionFile[0]
} else {
console.log('no extensionFile', extensionFile, inputString)
extensionFile = ''
}
inputString = inputString.replace(extensionFile, '') inputString = inputString.replace(extensionFile, '')
@ -97,7 +115,7 @@ function findTagSectionInString (inputString) {
let boom = inputString.split(tagSectionSeparator) let boom = inputString.split(tagSectionSeparator)
console.log('boom', boom) console.log('boom', boom)
if (boom.length) { if (boom.length) {
fileSectionsName = boom.splice(tagSeparator); fileSectionsName = boom.splice(tagSeparator)
listOfTags = [...fileSectionsName[1].trim().split(tagSeparator)] listOfTags = [...fileSectionsName[1].trim().split(tagSeparator)]
console.log('listOfTags', listOfTags) console.log('listOfTags', listOfTags)
} else { } else {
@ -108,18 +126,6 @@ function findTagSectionInString (inputString) {
return listOfTags return listOfTags
} }
/**
* é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) { function renameFile (originalFileName, fileMixedNewName) {
fs.rename(originalFileName, fileMixedNewName, function (err) { fs.rename(originalFileName, fileMixedNewName, function (err) {
if (err) console.log('rename ERROR: ' + err) if (err) console.log('rename ERROR: ' + err)
@ -148,12 +154,120 @@ if (fileMixedNewName !== originalFileName) {
// renameFile(originalFileName, fileMixedNewName) // 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) {
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(/ {*}/, '')
return newFileName
}
const patternsFiles = { const patternsFiles = {
'downloaded_pic': '', // FyB8cZnWIAc21rw.jpg 'downloaded_pic': '', // FyB8cZnWIAc21rw.jpg
'telegram_pic': '', // -4900281569878475578_1109.jpg 'telegram_pic': '', // -4900281569878475578_1109.jpg
'open_camera': '^IMG_OC', // IMG_OC_20230617_092120_3.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 'screenshot': '^Screenshot', // Screenshot 2023-06-15 at 15-26-04 Instance Panoramax OSM-FR.png
} }
const fileDefinition = {
dateStamp: '',
freeText: '',
tags: [],
extension: '',
}
function destructurateFileName (fileName) {
return {
dateStamp: findFormattedDate(fileName),
freeText: findFileNameFreeTextPart(fileName),
tags: findTagSectionInString(fileName),
extension: findFileExtension(fileName),
}
}
function cleanSpaces (inputString) {
return inputString.replace(/ *g/, ' ')
}
function makeFileNameFromProperties (fileProperties) {
return cleanSpaces(fileProperties.dateStamp + ' ' + fileProperties.freeText + ' ' + tagSectionSeparator + ' ' + fileProperties.tags.join(tagSeparator) + fileProperties.extension)
}
function appendFileName (fileProperties, newText) {
fileProperties.freeText = cleanSpaces(fileProperties.freeText + ' ' + newText)
return fileProperties
}
function prependFileName (fileProperties, newText) {
fileProperties.freeText = cleanSpaces(newText + ' ' + fileProperties.freeText)
return fileProperties
}
function searchAndReplaInFileName(searchString, replaceString, fileName){
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')
let screenShotMockFileName = 'Screenshot 2023-06-15 at 15-28-21 Instance Panoramax OSM-FR.png'
screenShotMockFileName = 'Capture d\'écran 2023-06-15.png'
if (findScreenshot(screenShotMockFileName)) {
let tags = findTagSectionInString(screenShotMockFileName)
console.log('tags', tags)
if (!tags.includes('screenshot')) {
screenShotMockFileName.replace('Screenshot', '')
screenShotMockFileName = addTagInFileName('screenshot', screenShotMockFileName)
console.log('screenShotMockFileName:', screenShotMockFileName)
}
console.log('is a screenshot, remove screenshot in name, and add tag screenshot')
}
/**
----------------------- parties non réalisées -----------------------
// TODO
---------------------------------------------------------------------
**/
function DownloadedTelegramPictureRename (fileName) {
let fileProperties = destructurateFileName(fileName)
}
function hasDifferentDateInNameThanExif () {
}
/**
* é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
}