145 lines
4.2 KiB
JavaScript
145 lines
4.2 KiB
JavaScript
/**---------------------
|
|
* @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'
|
|
import log from 'loglevel';
|
|
|
|
/** ---------------------
|
|
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
|
|
log.setLevel('info')
|
|
log.info(' ')
|
|
|
|
function parseArguments () {
|
|
mini_arguments = minimist(process.argv.slice(2))
|
|
log.info('arguments', mini_arguments)
|
|
}
|
|
|
|
parseArguments()
|
|
|
|
function renameFile (originalFileName, fileMixedNewName) {
|
|
fs.rename(originalFileName, fileMixedNewName, function (err) {
|
|
log.info('name changed', fileMixedNewName)
|
|
if (err) log.info('rename ERROR: ' + err)
|
|
})
|
|
}
|
|
|
|
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) {
|
|
log.info(' ______ allez hop fini la recherche on fait un nouveau nom')
|
|
log.info('structureForFile', structureForFile)
|
|
let newName = makeFileNameFromProperties(structureForFile)
|
|
if (structureForFile.fileNameOriginal !== newName) {
|
|
|
|
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)
|
|
if(! mini_arguments.dryRun){
|
|
renameFile(structureForFile.fullPath, structureForFile.folderPath + newName)
|
|
}
|
|
else{
|
|
log.info('no renaming for real, this is a dry run')
|
|
}
|
|
} else {
|
|
log.info(' rien à changer')
|
|
}
|
|
|
|
}
|
|
|
|
async function guessFileNameOnAllFilesFromArguments () {
|
|
|
|
// parcourir les dossiers
|
|
// parcourir les fichiers
|
|
|
|
log.info('liste des fichiers', mini_arguments._)
|
|
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) {
|
|
log.info(' le nom de fichier ne contient pas de date formatée au début')
|
|
|
|
finder.findExifCreationDate(structureForFile.fullPath)
|
|
.then(data => {
|
|
log.info(' ... chercher la date de création')
|
|
let foundDate = finder.findEarliestDateInExifData(data)
|
|
|
|
log.info(' =>>>>>>> foundDate : ', foundDate)
|
|
if (foundDate) {
|
|
|
|
|
|
// finder.findEarliestDateInExifData(fullPath).then(response => {
|
|
// log.info(' ... trouvée')
|
|
// if (response) {
|
|
structureForFile.dateStampExif = foundDate
|
|
|
|
shouldWeChangeName(structureForFile)
|
|
// }
|
|
// })
|
|
|
|
} else {
|
|
log.info('pas de date trouvée dans le nom')
|
|
}
|
|
|
|
}
|
|
,
|
|
(error) => {
|
|
log.info('/////////// Error in reading exif of file: ' + error.message)
|
|
return ''
|
|
})
|
|
}
|
|
}
|
|
)
|
|
|
|
}
|
|
|
|
guessFileNameOnAllFilesFromArguments()
|
|
|
|
// run tests
|
|
if (enableTestsLocally) {
|
|
|
|
TestTagsAreDetectedInFileName()
|
|
TestFindFormattedDate()
|
|
TestScreenShotIsFoundAndRenamed()
|
|
}
|
|
if (reportStatistics || mini_arguments.stats) {
|
|
finder.reportStatistics()
|
|
}
|