Skip to content

Commit 8355e1e

Browse files
ZeroX-DGRokt33r
authored andcommitted
updated function name and return type
1 parent c7d33fb commit 8355e1e

File tree

2 files changed

+32
-36
lines changed

2 files changed

+32
-36
lines changed

browser/main/lib/dataApi/attachmentManagement.js

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -624,7 +624,15 @@ function deleteAttachmentsNotPresentInNote (markdownContent, storageKey, noteKey
624624
}
625625
}
626626

627-
function getAttachments (markdownContent, storageKey, noteKey) {
627+
/**
628+
* @description Get all existing attachments related to a specific note
629+
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
630+
* @param storageKey StorageKey of the current note
631+
* @param noteKey NoteKey of the currentNote
632+
* @param linkText Text that was pasted
633+
* @return {Promise<Array<{path: String, isInUse: bool}>>} Promise returning the
634+
list of attachments with their properties */
635+
function getAttachmentsPathAndStatus (markdownContent, storageKey, noteKey) {
628636
if (storageKey == null || noteKey == null || markdownContent == null) {
629637
return
630638
}
@@ -646,23 +654,16 @@ function getAttachments (markdownContent, storageKey, noteKey) {
646654
reject(err)
647655
return
648656
}
649-
const attachmentsNotInNotePaths = []
650-
const attachmentsInNotePaths = []
651-
const allAttachments = []
657+
const attachments = []
652658
for (const file of files) {
653659
const absolutePathOfFile = path.join(targetStorage.path, DESTINATION_FOLDER, noteKey, file)
654660
if (!attachmentsInNoteOnlyFileNames.includes(file)) {
655-
attachmentsNotInNotePaths.push(absolutePathOfFile)
661+
attachments.push({ path: absolutePathOfFile, isInUse: false })
656662
} else {
657-
attachmentsInNotePaths.push(absolutePathOfFile)
663+
attachments.push({ path: absolutePathOfFile, isInUse: true })
658664
}
659-
allAttachments.push(absolutePathOfFile)
660665
}
661-
resolve({
662-
allAttachments,
663-
attachmentsNotInNotePaths,
664-
attachmentsInNotePaths
665-
})
666+
resolve(attachments)
666667
})
667668
})
668669
} else {
@@ -774,7 +775,7 @@ module.exports = {
774775
removeStorageAndNoteReferences,
775776
deleteAttachmentFolder,
776777
deleteAttachmentsNotPresentInNote,
777-
getAttachments,
778+
getAttachmentsPathAndStatus,
778779
moveAttachments,
779780
cloneAttachments,
780781
isAttachmentLink,

browser/main/modals/PreferencesModal/StoragesTab.js

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class StoragesTab extends React.Component {
6060
loadAttachmentStorage () {
6161
const promises = []
6262
this.props.data.noteMap.map(note => {
63-
const promise = attachmentManagement.getAttachments(
63+
const promise = attachmentManagement.getAttachmentsPathAndStatus(
6464
note.content,
6565
note.storage,
6666
note.key
@@ -69,7 +69,10 @@ class StoragesTab extends React.Component {
6969
})
7070

7171
Promise.all(promises)
72-
.then(data => this.setState({attachments: data}))
72+
.then(data => {
73+
const result = data.reduce((acc, curr) => acc.concat(curr), [])
74+
this.setState({attachments: result})
75+
})
7376
.catch(console.error)
7477
}
7578

@@ -118,32 +121,24 @@ class StoragesTab extends React.Component {
118121
const { data, boundingBox } = this.props
119122
const { attachments } = this.state
120123

121-
const totalUnusedAttachments = attachments
122-
.reduce((acc, curr) => acc + curr.attachmentsNotInNotePaths.length, 0)
123-
const totalInuseAttachments = attachments
124-
.reduce((acc, curr) => acc + curr.attachmentsInNotePaths.length, 0)
125-
const totalAttachments = totalUnusedAttachments + totalInuseAttachments
124+
const unusedAttachments = attachments.filter(attachment => !attachment.isInUse)
125+
const inUseAttachments = attachments.filter(attachment => attachment.isInUse)
126126

127-
const unusedAttachments = attachments.reduce((acc, curr) => {
128-
acc.push(curr.attachmentsNotInNotePaths)
129-
return acc
130-
}, [])
127+
const totalUnusedAttachments = unusedAttachments.length
128+
const totalInuseAttachments = inUseAttachments.length
129+
const totalAttachments = totalUnusedAttachments + totalInuseAttachments
131130

132-
const totalUnusedAttachmentsSize = attachments
131+
const totalUnusedAttachmentsSize = unusedAttachments
133132
.reduce((acc, curr) => {
134-
return acc + curr.attachmentsNotInNotePaths.reduce((racc, rcurr) => {
135-
const stats = fs.statSync(rcurr)
136-
const fileSizeInBytes = stats.size
137-
return racc + fileSizeInBytes
138-
}, 0)
133+
const stats = fs.statSync(curr.path)
134+
const fileSizeInBytes = stats.size
135+
return acc + fileSizeInBytes
139136
}, 0)
140-
const totalInuseAttachmentsSize = attachments
137+
const totalInuseAttachmentsSize = inUseAttachments
141138
.reduce((acc, curr) => {
142-
return acc + curr.attachmentsInNotePaths.reduce((racc, rcurr) => {
143-
const stats = fs.statSync(rcurr)
144-
const fileSizeInBytes = stats.size
145-
return racc + fileSizeInBytes
146-
}, 0)
139+
const stats = fs.statSync(curr.path)
140+
const fileSizeInBytes = stats.size
141+
return acc + fileSizeInBytes
147142
}, 0)
148143
const totalAttachmentsSize = totalUnusedAttachmentsSize + totalInuseAttachmentsSize
149144

0 commit comments

Comments
 (0)