-
Notifications
You must be signed in to change notification settings - Fork 592
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
make changes for import/export module to work properly #4270
Changes from 22 commits
30ab5da
5cbec97
c714926
3b06f5e
7c94ea6
ccb5c18
eaf5179
b376997
fc15e25
bea905a
f1eb181
a560591
5346f26
e9f7a0c
66989c4
2b62ecf
6d88220
26df2d0
b1b72c2
2c6f00a
734e292
90f9fb3
92b8054
526c519
8af5ac0
4ba007d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,5 +1,13 @@ | ||||||
# Changelog | ||||||
|
||||||
## Unreleased | ||||||
|
||||||
### Add | ||||||
|
||||||
* Allows to insert attachments with a given ID, as well as with `docIds` and `atchiveDocIds` to preserve related docs. | ||||||
* Adds an `update` method to the attachment module, that update the mongoDB doc and the associated file. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
* Adds an option to the `http` `remote` method to allow receiving the original response from `node-fetch` that is a stream. | ||||||
|
||||||
## 3.57.0 2023-09-27 | ||||||
|
||||||
### Adds | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -386,8 +386,7 @@ module.exports = { | |
// This method returns `attachment` where `attachment` is an attachment | ||
// object, suitable for passing to the `url` API and for use as the value | ||
// of a `type: 'attachment'` schema field. | ||
async insert(req, file, options) { | ||
options = options || {}; | ||
async insert(req, file, options = {}) { | ||
let extension = path.extname(file.name); | ||
if (extension && extension.length) { | ||
extension = extension.substr(1); | ||
|
@@ -402,16 +401,21 @@ module.exports = { | |
extensions: accepted.join(req.t('apostrophe:listJoiner')) | ||
})); | ||
} | ||
|
||
if (options.attachmentId && await self.apos.attachment.db.findOne({ _id: options.attachmentId })) { | ||
throw self.apos.error('invalid', 'duplicate'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This could hit a race condition and imposes an unnecessary delay, try/catch the insert and check for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As discussed I kept the behavior as it is for now, we might create a ticket to update slightly the insert method in order to insert the doc before to copy to file to uploadfs. |
||
} | ||
|
||
const info = { | ||
_id: self.apos.util.generateId(), | ||
_id: options.attachmentId ?? self.apos.util.generateId(), | ||
group: group.name, | ||
createdAt: new Date(), | ||
name: self.apos.util.slugify(path.basename(file.name, path.extname(file.name))), | ||
title: self.apos.util.sortify(path.basename(file.name, path.extname(file.name))), | ||
extension: extension, | ||
type: 'attachment', | ||
docIds: [], | ||
archivedDocIds: [] | ||
docIds: options.docIds ?? [], | ||
archivedDocIds: options.archivedDocIds ?? [] | ||
}; | ||
if (!(options.permissions === false)) { | ||
if (!self.apos.permission.can(req, 'upload-attachment')) { | ||
|
@@ -432,7 +436,11 @@ module.exports = { | |
} | ||
if (self.isSized(extension)) { | ||
// For images we correct automatically for common file extension mistakes | ||
const result = await Promise.promisify(self.uploadfs.copyImageIn)(file.path, '/attachments/' + info._id + '-' + info.name, { sizes: self.imageSizes }); | ||
const result = await Promise.promisify(self.uploadfs.copyImageIn)( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. only indentation. |
||
file.path, | ||
'/attachments/' + info._id + '-' + info.name, | ||
{ sizes: self.imageSizes } | ||
); | ||
info.extension = result.extension; | ||
info.width = result.width; | ||
info.height = result.height; | ||
|
@@ -454,6 +462,42 @@ module.exports = { | |
await self.db.insertOne(info); | ||
return info; | ||
}, | ||
|
||
async update(req, file, attachment) { | ||
const existing = await self.db.findOne({ _id: attachment._id }); | ||
if (!existing) { | ||
throw self.apos.error('notfound'); | ||
} | ||
|
||
const projection = { | ||
_id: 1, | ||
archived: 1 | ||
}; | ||
|
||
const existingRelatedDocs = await self.apos.doc.db.find({ | ||
_id: { $in: [ ...attachment.docIds, ...attachment.archivedDocIds ] } | ||
}, { projection }).toArray(); | ||
|
||
const { docIds, archivedDocIds } = existingRelatedDocs | ||
.reduce(({ docIds, archivedDocIds }, doc) => { | ||
return { | ||
docIds: [ ...docIds, ...!doc.archived ? [ doc._id ] : [] ], | ||
archivedDocIds: [ ...archivedDocIds, ...doc.archived ? [ doc._id ] : [] ] | ||
}; | ||
}, { | ||
docIds: [], | ||
archivedDocIds: [] | ||
}); | ||
|
||
await self.alterAttachment(existing, 'remove'); | ||
await self.db.deleteOne({ _id: existing._id }); | ||
await self.insert(req, file, { | ||
attachmentId: attachment._id, | ||
docIds: _.uniq([ ...docIds, ...existing.docIds || [] ]), | ||
archivedDocIds: _.uniq([ ...archivedDocIds, ...existing.archivedDocIds || [] ]) | ||
}); | ||
}, | ||
|
||
// Given a path to a local svg file, sanitize any XSS attack vectors that | ||
// may be present in the file. The caller is responsible for catching any | ||
// exception thrown and treating that as an invalid file but there is no | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -56,6 +56,7 @@ | |
:labels="moduleLabels" | ||
:disable="relationshipErrors === 'min'" | ||
:displayed-items="items.length" | ||
:checked="checked" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Huh, was this just an unrelated bug? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not a bug, it has been added to be consistent with the other vue changes. |
||
:checked-count="checked.length" | ||
:module-name="moduleName" | ||
@page-change="updatePage" | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.