diff --git a/browser/main/lib/dataApi/attachmentManagement.js b/browser/main/lib/dataApi/attachmentManagement.js index c5b715c3..ff961433 100644 --- a/browser/main/lib/dataApi/attachmentManagement.js +++ b/browser/main/lib/dataApi/attachmentManagement.js @@ -624,7 +624,15 @@ function deleteAttachmentsNotPresentInNote (markdownContent, storageKey, noteKey } } -function getAttachments (markdownContent, storageKey, noteKey) { +/** + * @description Get all existing attachments related to a specific note + including their status (in use or not) and their path. Return null if there're no attachment related to note or specified parametters are invalid + * @param storageKey StorageKey of the current note + * @param noteKey NoteKey of the currentNote + * @param linkText Text that was pasted + * @return {Promise>} Promise returning the + list of attachments with their properties */ +function getAttachmentsPathAndStatus (markdownContent, storageKey, noteKey) { if (storageKey == null || noteKey == null || markdownContent == null) { return } @@ -646,23 +654,16 @@ function getAttachments (markdownContent, storageKey, noteKey) { reject(err) return } - const attachmentsNotInNotePaths = [] - const attachmentsInNotePaths = [] - const allAttachments = [] + const attachments = [] for (const file of files) { const absolutePathOfFile = path.join(targetStorage.path, DESTINATION_FOLDER, noteKey, file) if (!attachmentsInNoteOnlyFileNames.includes(file)) { - attachmentsNotInNotePaths.push(absolutePathOfFile) + attachments.push({ path: absolutePathOfFile, isInUse: false }) } else { - attachmentsInNotePaths.push(absolutePathOfFile) + attachments.push({ path: absolutePathOfFile, isInUse: true }) } - allAttachments.push(absolutePathOfFile) } - resolve({ - allAttachments, - attachmentsNotInNotePaths, - attachmentsInNotePaths - }) + resolve(attachments) }) }) } else { @@ -774,7 +775,7 @@ module.exports = { removeStorageAndNoteReferences, deleteAttachmentFolder, deleteAttachmentsNotPresentInNote, - getAttachments, + getAttachmentsPathAndStatus, moveAttachments, cloneAttachments, isAttachmentLink, diff --git a/browser/main/modals/PreferencesModal/StoragesTab.js b/browser/main/modals/PreferencesModal/StoragesTab.js index b2c3b32e..f1b186eb 100644 --- a/browser/main/modals/PreferencesModal/StoragesTab.js +++ b/browser/main/modals/PreferencesModal/StoragesTab.js @@ -60,7 +60,7 @@ class StoragesTab extends React.Component { loadAttachmentStorage () { const promises = [] this.props.data.noteMap.map(note => { - const promise = attachmentManagement.getAttachments( + const promise = attachmentManagement.getAttachmentsPathAndStatus( note.content, note.storage, note.key @@ -69,7 +69,10 @@ class StoragesTab extends React.Component { }) Promise.all(promises) - .then(data => this.setState({attachments: data})) + .then(data => { + const result = data.reduce((acc, curr) => acc.concat(curr), []) + this.setState({attachments: result}) + }) .catch(console.error) } @@ -118,32 +121,24 @@ class StoragesTab extends React.Component { const { data, boundingBox } = this.props const { attachments } = this.state - const totalUnusedAttachments = attachments - .reduce((acc, curr) => acc + curr.attachmentsNotInNotePaths.length, 0) - const totalInuseAttachments = attachments - .reduce((acc, curr) => acc + curr.attachmentsInNotePaths.length, 0) + const unusedAttachments = attachments.filter(attachment => !attachment.isInUse) + const inUseAttachments = attachments.filter(attachment => attachment.isInUse) + + const totalUnusedAttachments = unusedAttachments.length + const totalInuseAttachments = inUseAttachments.length const totalAttachments = totalUnusedAttachments + totalInuseAttachments - const unusedAttachments = attachments.reduce((acc, curr) => { - acc.push(curr.attachmentsNotInNotePaths) - return acc - }, []) - - const totalUnusedAttachmentsSize = attachments + const totalUnusedAttachmentsSize = unusedAttachments .reduce((acc, curr) => { - return acc + curr.attachmentsNotInNotePaths.reduce((racc, rcurr) => { - const stats = fs.statSync(rcurr) - const fileSizeInBytes = stats.size - return racc + fileSizeInBytes - }, 0) + const stats = fs.statSync(curr.path) + const fileSizeInBytes = stats.size + return acc + fileSizeInBytes }, 0) - const totalInuseAttachmentsSize = attachments + const totalInuseAttachmentsSize = inUseAttachments .reduce((acc, curr) => { - return acc + curr.attachmentsInNotePaths.reduce((racc, rcurr) => { - const stats = fs.statSync(rcurr) - const fileSizeInBytes = stats.size - return racc + fileSizeInBytes - }, 0) + const stats = fs.statSync(curr.path) + const fileSizeInBytes = stats.size + return acc + fileSizeInBytes }, 0) const totalAttachmentsSize = totalUnusedAttachmentsSize + totalInuseAttachmentsSize