From d59d27cd741935d0ee7c1d9300fee2174fc51b00 Mon Sep 17 00:00:00 2001 From: Alec Rust Date: Thu, 26 Oct 2023 14:04:47 +0100 Subject: [PATCH] Improve handling of different file types with similar filenames --- src/index.js | 13 ++++++++++++- src/index.test.js | 49 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 83db047..1fa898f 100755 --- a/src/index.js +++ b/src/index.js @@ -48,7 +48,18 @@ const processFolder = async (srcFolder, destFolder) => { if (candidates.length === 0) continue - const mediaFileToCopy = candidates.find((f) => f.includes('-edited')) || candidates[0] + const mediaType = path.extname(title).toLowerCase() + + const mediaFileToCopy = + candidates.find((f) => { + return f.toLowerCase().includes(mediaType) && f.includes('-edited') + }) || + candidates.find((f) => { + return f.toLowerCase().includes(mediaType) + }) + + if (!mediaFileToCopy) continue + const srcMediaFilePath = path.join(srcFolder, mediaFileToCopy) let destMediaFilePath = path.join(destFolder, mediaFileToCopy) diff --git a/src/index.test.js b/src/index.test.js index 23f392a..dd1d039 100644 --- a/src/index.test.js +++ b/src/index.test.js @@ -183,6 +183,55 @@ describe('Tests', () => { ], ) }) + + it('handles different file types with similar filenames', async () => { + await runTest( + [ + { + name: 'IMG_0001-edited.JPG', + filesize: '10', + }, + { + name: 'IMG_0001.JPG.jpeg', + filesize: '20', + }, + { + name: 'IMG_0001.JPG.json', + content: { + title: 'IMG_0001.JPG', + photoTakenTime: { + timestamp: '86400', + }, + }, + }, + { + name: 'IMG_0001.MOV.mp4', + filesize: '30', + }, + { + name: 'IMG_0001.MOV.json', + content: { + title: 'IMG_0001.MOV', + photoTakenTime: { + timestamp: '86400', + }, + }, + }, + ], + [ + { + name: 'IMG_0001.JPG', + filesize: '10', + timestamp: '86400', + }, + { + name: 'IMG_0001.MOV.mp4', + filesize: '30', + timestamp: '86400', + }, + ], + ) + }) }) const runTest = async (inputFiles, outputFiles) => {