rangement/index.mjs

225 lines
6.0 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
2023-07-04 15:26:27 +02:00
--------------------- */
2023-07-01 12:35:33 +02:00
/** ---------------------
2023-07-04 15:26:27 +02:00
libs
--------------------- */
2023-07-01 12:35:33 +02:00
import fs from 'node-fs'
import minimist from 'minimist'
2023-07-18 23:28:48 +02:00
import log from 'loglevel'
import path from 'node:path'
2023-07-01 12:35:33 +02:00
/** ---------------------
2023-07-04 15:26:27 +02:00
custom utilities and configuration
2023-07-01 12:35:33 +02:00
--------------------- */
2023-07-19 10:04:08 +02:00
import rangement_instance from './conf/configs.js'
2023-07-01 12:35:33 +02:00
import {
2023-07-18 23:28:48 +02:00
TestFindFormattedDate,
TestScreenShotIsFoundAndRenamed,
TestTagsAreDetectedInFileName
2023-07-01 12:35:33 +02:00
} from './testFunctions.mjs'
import finder from './finder.mjs'
let mini_arguments
2023-07-04 15:26:27 +02:00
log.setLevel(rangement_instance.log_level)
2023-07-04 12:08:05 +02:00
log.info(' ')
2023-07-01 12:35:33 +02:00
2023-07-18 23:28:48 +02:00
function parseArguments () {
mini_arguments = minimist(process.argv.slice(2))
log.debug('arguments', mini_arguments)
2023-07-01 12:35:33 +02:00
}
2023-07-18 23:28:48 +02:00
function addOriginalFileNameIfMissing (originalFileName, fileMixedNewName) {
// const ep = new exiftool.ExiftoolProcess()
//
// ep
// .open(fileMixedNewName)
// .then(() => ep.writeMetadata(fileMixedNewName, {
// 'OriginalFileName+': originalFileName,
// }))
// .then(console.log, console.error)
// .then(() => ep.close())
// .catch(console.error)
2023-07-01 12:35:33 +02:00
}
2023-07-18 23:28:48 +02:00
function renameFile (originalFileName, fileMixedNewName) {
fs.rename(originalFileName, fileMixedNewName, function (err) {
log.info('name changed', fileMixedNewName)
if (err) {
log.info('rename ERROR: ' + err)
} else {
// otherRenames.push(originalFileName)
otherRenames.push(fileMixedNewName)
addOriginalFileNameIfMissing(originalFileName, fileMixedNewName)
// rangement_instance.statistics['filesModified']++
}
})
2023-07-01 12:35:33 +02:00
}
2023-07-18 23:28:48 +02:00
export function makeFileNameFromProperties (fileProperties) {
let tagPlace = ''
log.info('fileProperties.tags', fileProperties.tags)
if (fileProperties.tags.length && rangement_instance.keepTags) {
tagPlace = ' ' + rangement_instance.tagSectionSeparator + ' ' + fileProperties.tags.join(rangement_instance.tagSeparator)
}
console.log('fileProperties.dateStampExif', fileProperties.dateStampExif)
let newName = ''
+ fileProperties.dateStampExif
+ ' '
+ (rangement_instance.keepFreeText ? fileProperties.freeText : '')
+ tagPlace
+ fileProperties.extension
if (rangement_instance.replaceUnderscoreWithSpaces) {
newName = newName.replace('_', ' ')
}
newName = finder.cleanSpaces(newName)
return newName
2023-07-01 12:35:33 +02:00
}
2023-07-18 23:28:48 +02:00
let otherRenames = []
function shouldWeChangeName (structureForFile) {
log.info(' ______ shouldWeChangeName ', structureForFile.fileNameOriginal)
let newName = makeFileNameFromProperties(structureForFile)
log.debug('newName', newName)
if (structureForFile.fileNameOriginal !== newName && !otherRenames.includes(newName)) {
log.info('\n ancien nom :', structureForFile.fileNameOriginal)
log.info(' nouveau nom:', newName)
if (!mini_arguments['dry-run']) {
renameFile(structureForFile.fullPath, structureForFile.folderPath + newName)
} else {
log.info('no renaming for real, this is a dry run')
}
} else {
log.info(' rien à changer')
}
2023-07-04 15:26:27 +02:00
}
2023-07-01 12:35:33 +02:00
2023-07-18 23:28:48 +02:00
2023-07-04 15:26:27 +02:00
/**
* guess file name on one file which is not a directory
* @param fullPath
*/
2023-07-18 23:28:48 +02:00
function guessFileNameOnOnefile (fullPath) {
log.info('go guess file name on file: ', fullPath)
fs.stat(fullPath, (err, stats) => {
if (err) {
log.error('échec fichier', err)
log.error('ce fichier n existe pas: ', fullPath)
return
} else {
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 "' + structureForFile.freeText + '" ne contient pas de date formatée au début')
finder.findExifCreationDate(structureForFile.fullPath)
.then(data => {
log.info(' ... chercher la date de création : "' + structureForFile.freeText + '"')
let foundDate = finder.findEarliestDateInExifData(data)
log.info(' =>>>>>>> foundDate : ', foundDate)
if (foundDate) {
structureForFile.dateStampExif = foundDate
} else {
log.info('pas de date trouvée dans le nom')
}
shouldWeChangeName(structureForFile)
}
,
(error) => {
log.warn('/////////// Error in reading exif of file: ' + error.message)
return ''
})
}
}
})
2023-07-04 15:26:27 +02:00
}
2023-07-01 12:35:33 +02:00
2023-07-04 15:26:27 +02:00
let expandedFileList = []
2023-07-18 23:28:48 +02:00
let cwd = path.dirname(process.cwd()) + '/' + path.basename(process.cwd())
2023-07-04 15:26:27 +02:00
console.log('cwd', cwd)
2023-07-01 12:35:33 +02:00
2023-07-18 23:28:48 +02:00
function guessFileNameOnAllFilesFromArguments () {
2023-07-01 12:35:33 +02:00
2023-07-18 23:28:48 +02:00
// parcourir les fichiers
log.debug('liste des fichiers', mini_arguments._)
let fileList = mini_arguments._
2023-07-01 12:35:33 +02:00
2023-07-18 23:28:48 +02:00
// test file exists
fileList.forEach(fullPath => {
// parcourir les dossiers
isFolderOrFile(`${fullPath}`)
}
)
2023-07-01 12:35:33 +02:00
2023-07-18 23:28:48 +02:00
log.info('expanded file list :', expandedFileList)
expandedFileList.forEach(filePath => guessFileNameOnOnefile(filePath))
2023-07-01 12:35:33 +02:00
2023-07-04 15:26:27 +02:00
}
2023-07-01 12:35:33 +02:00
2023-07-18 23:28:48 +02:00
function readSubdirectories (baseDir) {
const newGlob = baseDir
fs.readdir(baseDir, (err, files) => {
if (err) throw err
2023-07-04 15:26:27 +02:00
2023-07-18 23:28:48 +02:00
console.log('readSubdirectories - files', files)
files.forEach((subDirOrFile) => {
const newFullPath = cwd + '/' + subDirOrFile
2023-07-04 15:26:27 +02:00
2023-07-18 23:28:48 +02:00
if (fs.existsSync(newFullPath)) {
const s = fs.statSync(newFullPath)
2023-07-04 15:26:27 +02:00
2023-07-18 23:28:48 +02:00
if (s.isFile()) {
expandedFileList.push(cwd + '/' + subDirOrFile)
}
}
})
})
2023-07-01 12:35:33 +02:00
}
2023-07-18 23:28:48 +02:00
function isFolderOrFile (fileName) {
// const stat = fs.statSync(cwd + '/' + fileName);
const stat = fs.statSync(fileName)
if (stat.isDirectory()) {
let fileList = readSubdirectories(fileName)
console.log('fileList in directory ', fileName, '\n', fileList)
if (fileList) {
expandedFileList.push(...fileList)
}
} else if (stat.isFile()) {
expandedFileList.push(cwd + '/' + fileName)
}
2023-07-04 15:26:27 +02:00
}
parseArguments()
2023-07-01 12:35:33 +02:00
guessFileNameOnAllFilesFromArguments()
// run tests
2023-07-04 15:26:27 +02:00
if (rangement_instance.enableTestsLocally) {
2023-07-01 12:35:33 +02:00
2023-07-18 23:28:48 +02:00
TestTagsAreDetectedInFileName()
TestFindFormattedDate()
TestScreenShotIsFoundAndRenamed()
2023-07-01 12:35:33 +02:00
}
2023-07-04 15:26:27 +02:00
if (rangement_instance.reportStatistics || mini_arguments.stats) {
2023-07-18 23:28:48 +02:00
finder.reportStatistics()
2023-07-01 12:35:33 +02:00
}