From 9a841fead74d5447da8106f45242e94661fd5d7d Mon Sep 17 00:00:00 2001 From: Tom Boutell Date: Thu, 5 Sep 2024 09:36:57 -0400 Subject: [PATCH] do not try to replace existing attachments, just merge crops and use recomputeAllDocReferences (#80) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * do not try to replace existing attachments, just merge crops and use recomputeAllDocReferences * keep attachment name --------- Co-authored-by: Harouna Traoré --- .gitignore | 2 ++ lib/methods/import.js | 28 ++++++++++++++++++---------- test/index.js | 8 ++++---- 3 files changed, 24 insertions(+), 14 deletions(-) diff --git a/.gitignore b/.gitignore index ec6ab602..c69861e0 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,5 @@ node_modules # vim swp files .*.sw* +/test/public/uploads +/test/public/exports diff --git a/lib/methods/import.js b/lib/methods/import.js index 0bdf2eec..d70d0322 100644 --- a/lib/methods/import.js +++ b/lib/methods/import.js @@ -166,6 +166,9 @@ module.exports = self => { }); } + // One call to fix all bookkeeping re: docIds, archivedDocIds, etc. + await self.apos.attachment.recomputeAllDocReferences(); + const results = { overrideLocale, duplicatedDocs, @@ -682,21 +685,26 @@ module.exports = self => { } } - try { + const existing = await self.apos.attachment.db.findOne({ + _id: attachment._id + }); + if (!existing) { await self.apos.attachment.insert( req, file, { attachmentId: attachment._id } ); - } catch (err) { - if (err.message === 'duplicate' && !self.options.preventUpdateAssets) { - await self.apos.attachment.update( - req, - file, - attachment - ); - } else { - throw err; + } else { + // Same _id === same original file, same scaled versions, etc. + // The only differences will be in the crops and the + // docIds etc. We will take care of docIds in one shot later via + // recomputeAllDocReferences + // + // Perform any crops not found in the existing attachment + for (const crop of [ ...(attachment.crops || []), ...(existing.crops || []) ]) { + if (!existing.crops.find(c => JSON.stringify(c) === JSON.stringify(crop))) { + await self.apos.attachment.crop(req, attachment._id, crop); + } } } }, diff --git a/test/index.js b/test/index.js index e7d6eafe..f9fbcb88 100644 --- a/test/index.js +++ b/test/index.js @@ -434,9 +434,9 @@ describe('@apostrophecms/import-export', function () { 'topic2', 'topic1' ], - attachmentNames: [ 'test-image' ], + attachmentNames: [ 'new-name' ], attachmentFileNames: new Array(apos.attachment.imageSizes.length + 1) - .fill('test-image'), + .fill('new-name'), job: { good: 9, total: 11 @@ -591,9 +591,9 @@ describe('@apostrophecms/import-export', function () { 'page1', 'page1' ], - attachmentNames: [ 'test-image' ], + attachmentNames: [ 'new-name' ], attachmentFileNames: new Array(apos.attachment.imageSizes.length + 1) - .fill('test-image'), + .fill('new-name'), job: { good: 7, total: 7