Skip to content

Commit

Permalink
do not try to replace existing attachments, just merge crops and use …
Browse files Browse the repository at this point in the history
…recomputeAllDocReferences (#80)

* do not try to replace existing attachments, just merge crops and use recomputeAllDocReferences

* keep attachment name

---------

Co-authored-by: Harouna Traoré <[email protected]>
  • Loading branch information
boutell and haroun authored Sep 5, 2024
1 parent d181956 commit 9a841fe
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 14 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ node_modules

# vim swp files
.*.sw*
/test/public/uploads
/test/public/exports
28 changes: 18 additions & 10 deletions lib/methods/import.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
}
}
}
},
Expand Down
8 changes: 4 additions & 4 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 9a841fe

Please sign in to comment.