rangement/index.mjs

145 lines
4.2 KiB
JavaScript
Raw Normal View History

2023-07-01 12:35:33 +02:00
/**---------------------
* @name tykayn Rangement
* @description Rangement sorts and rename files depending on their exif data
* @contact contact@cipherbliss.com
--------------------- */
/** ---------------------
libs
--------------------- */
import fs from 'node-fs'
import minimist from 'minimist'
2023-07-04 12:08:05 +02:00
import log from 'loglevel';
2023-07-01 12:35:33 +02:00
/** ---------------------
custom utilities and configuration
--------------------- */
import { enableTestsLocally, reportStatistics,tagSectionSeparator, tagSeparator } from './configs.mjs'
import {
TestFindFormattedDate,
TestScreenShotIsFoundAndRenamed,
TestTagsAreDetectedInFileName
} from './testFunctions.mjs'
import finder from './finder.mjs'
let mini_arguments
2023-07-04 12:08:05 +02:00
log.setLevel('info')
log.info(' ')
2023-07-01 12:35:33 +02:00
function parseArguments () {
mini_arguments = minimist(process.argv.slice(2))
2023-07-04 12:08:05 +02:00
log.info('arguments', mini_arguments)
2023-07-01 12:35:33 +02:00
}
parseArguments()
function renameFile (originalFileName, fileMixedNewName) {
fs.rename(originalFileName, fileMixedNewName, function (err) {
2023-07-04 12:08:05 +02:00
log.info('name changed', fileMixedNewName)
if (err) log.info('rename ERROR: ' + err)
2023-07-01 12:35:33 +02:00
})
}
function appendFileName (fileProperties, newText) {
fileProperties.freeText = finder.cleanSpaces(fileProperties.freeText + ' ' + newText)
return fileProperties
}
function prependFileName (fileProperties, newText) {
fileProperties.freeText = finder.cleanSpaces(newText + ' ' + fileProperties.freeText)
return fileProperties
}
function makeFileNameFromProperties(fileProperties) {
let tagPlace = ''
if (fileProperties.tags.length) {
tagPlace = ' ' + tagSectionSeparator + ' '
}
// return finder.cleanSpaces(fileProperties.dateStamp + ' ' + fileProperties.freeText + tagPlace + fileProperties.tags.join(tagSeparator) + fileProperties.extension).replace(+' ' + tagSectionSeparator + ' ' + '.', '.')
return ''+fileProperties.dateStampExif + ' ' + fileProperties.freeText + tagPlace + fileProperties.tags.join(tagSeparator) + fileProperties.extension
}
function shouldWeChangeName (structureForFile) {
2023-07-04 12:08:05 +02:00
log.info(' ______ allez hop fini la recherche on fait un nouveau nom')
log.info('structureForFile', structureForFile)
2023-07-01 12:35:33 +02:00
let newName = makeFileNameFromProperties(structureForFile)
if (structureForFile.fileNameOriginal !== newName) {
2023-07-04 12:08:05 +02:00
log.info('\n ancien nom :', structureForFile.fileNameOriginal)
// log.info(' nouveau nom:', foundDate +structureForFile.freeText + structureForFile.tags.join(tagSeparator) + structureForFile.extension )
log.info(' nouveau nom:', newName)
2023-07-03 21:14:31 +02:00
if(! mini_arguments.dryRun){
renameFile(structureForFile.fullPath, structureForFile.folderPath + newName)
}
else{
2023-07-04 12:08:05 +02:00
log.info('no renaming for real, this is a dry run')
2023-07-03 21:14:31 +02:00
}
2023-07-01 12:35:33 +02:00
} else {
2023-07-04 12:08:05 +02:00
log.info(' rien à changer')
2023-07-01 12:35:33 +02:00
}
}
async function guessFileNameOnAllFilesFromArguments () {
// parcourir les dossiers
// parcourir les fichiers
2023-07-04 12:08:05 +02:00
log.info('liste des fichiers', mini_arguments._)
2023-07-01 12:35:33 +02:00
let fileList = mini_arguments._
fileList.forEach(fullPath => {
let structureForFile = finder.destructurateFileName(fullPath)
// examiner les infos exif de chaque fichier pour proposer un nouveau nom
if (!structureForFile.dateStampInFileNameOriginal) {
2023-07-04 12:08:05 +02:00
log.info(' le nom de fichier ne contient pas de date formatée au début')
2023-07-01 12:35:33 +02:00
finder.findExifCreationDate(structureForFile.fullPath)
.then(data => {
2023-07-04 12:08:05 +02:00
log.info(' ... chercher la date de création')
2023-07-01 12:35:33 +02:00
let foundDate = finder.findEarliestDateInExifData(data)
2023-07-04 12:08:05 +02:00
log.info(' =>>>>>>> foundDate : ', foundDate)
2023-07-01 12:35:33 +02:00
if (foundDate) {
// finder.findEarliestDateInExifData(fullPath).then(response => {
2023-07-04 12:08:05 +02:00
// log.info(' ... trouvée')
2023-07-01 12:35:33 +02:00
// if (response) {
structureForFile.dateStampExif = foundDate
shouldWeChangeName(structureForFile)
// }
// })
} else {
2023-07-04 12:08:05 +02:00
log.info('pas de date trouvée dans le nom')
2023-07-01 12:35:33 +02:00
}
}
,
(error) => {
2023-07-04 12:08:05 +02:00
log.info('/////////// Error in reading exif of file: ' + error.message)
2023-07-01 12:35:33 +02:00
return ''
})
}
}
)
}
guessFileNameOnAllFilesFromArguments()
// run tests
if (enableTestsLocally) {
TestTagsAreDetectedInFileName()
TestFindFormattedDate()
TestScreenShotIsFoundAndRenamed()
}
if (reportStatistics || mini_arguments.stats) {
finder.reportStatistics()
}