1
0
mirror of https://github.com/BoostIo/Boostnote synced 2025-12-14 10:16:26 +00:00

updated function name and return type

This commit is contained in:
Nguyễn Việt Hưng
2019-08-28 11:17:51 +12:00
committed by Junyoung Choi
parent c7d33fbd83
commit 8355e1e006
2 changed files with 32 additions and 36 deletions

View File

@@ -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<Array<{path: String, isInUse: bool}>>} Promise returning the
list of attachments with their properties */
function getAttachmentsPathAndStatus (markdownContent, storageKey, noteKey) {
if (storageKey == null || noteKey == null || markdownContent == null) { if (storageKey == null || noteKey == null || markdownContent == null) {
return return
} }
@@ -646,23 +654,16 @@ function getAttachments (markdownContent, storageKey, noteKey) {
reject(err) reject(err)
return return
} }
const attachmentsNotInNotePaths = [] const attachments = []
const attachmentsInNotePaths = []
const allAttachments = []
for (const file of files) { for (const file of files) {
const absolutePathOfFile = path.join(targetStorage.path, DESTINATION_FOLDER, noteKey, file) const absolutePathOfFile = path.join(targetStorage.path, DESTINATION_FOLDER, noteKey, file)
if (!attachmentsInNoteOnlyFileNames.includes(file)) { if (!attachmentsInNoteOnlyFileNames.includes(file)) {
attachmentsNotInNotePaths.push(absolutePathOfFile) attachments.push({ path: absolutePathOfFile, isInUse: false })
} else { } else {
attachmentsInNotePaths.push(absolutePathOfFile) attachments.push({ path: absolutePathOfFile, isInUse: true })
} }
allAttachments.push(absolutePathOfFile)
} }
resolve({ resolve(attachments)
allAttachments,
attachmentsNotInNotePaths,
attachmentsInNotePaths
})
}) })
}) })
} else { } else {
@@ -774,7 +775,7 @@ module.exports = {
removeStorageAndNoteReferences, removeStorageAndNoteReferences,
deleteAttachmentFolder, deleteAttachmentFolder,
deleteAttachmentsNotPresentInNote, deleteAttachmentsNotPresentInNote,
getAttachments, getAttachmentsPathAndStatus,
moveAttachments, moveAttachments,
cloneAttachments, cloneAttachments,
isAttachmentLink, isAttachmentLink,

View File

@@ -60,7 +60,7 @@ class StoragesTab extends React.Component {
loadAttachmentStorage () { loadAttachmentStorage () {
const promises = [] const promises = []
this.props.data.noteMap.map(note => { this.props.data.noteMap.map(note => {
const promise = attachmentManagement.getAttachments( const promise = attachmentManagement.getAttachmentsPathAndStatus(
note.content, note.content,
note.storage, note.storage,
note.key note.key
@@ -69,7 +69,10 @@ class StoragesTab extends React.Component {
}) })
Promise.all(promises) 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) .catch(console.error)
} }
@@ -118,32 +121,24 @@ class StoragesTab extends React.Component {
const { data, boundingBox } = this.props const { data, boundingBox } = this.props
const { attachments } = this.state const { attachments } = this.state
const totalUnusedAttachments = attachments const unusedAttachments = attachments.filter(attachment => !attachment.isInUse)
.reduce((acc, curr) => acc + curr.attachmentsNotInNotePaths.length, 0) const inUseAttachments = attachments.filter(attachment => attachment.isInUse)
const totalInuseAttachments = attachments
.reduce((acc, curr) => acc + curr.attachmentsInNotePaths.length, 0) const totalUnusedAttachments = unusedAttachments.length
const totalInuseAttachments = inUseAttachments.length
const totalAttachments = totalUnusedAttachments + totalInuseAttachments const totalAttachments = totalUnusedAttachments + totalInuseAttachments
const unusedAttachments = attachments.reduce((acc, curr) => { const totalUnusedAttachmentsSize = unusedAttachments
acc.push(curr.attachmentsNotInNotePaths)
return acc
}, [])
const totalUnusedAttachmentsSize = attachments
.reduce((acc, curr) => { .reduce((acc, curr) => {
return acc + curr.attachmentsNotInNotePaths.reduce((racc, rcurr) => { const stats = fs.statSync(curr.path)
const stats = fs.statSync(rcurr)
const fileSizeInBytes = stats.size const fileSizeInBytes = stats.size
return racc + fileSizeInBytes return acc + fileSizeInBytes
}, 0) }, 0)
}, 0) const totalInuseAttachmentsSize = inUseAttachments
const totalInuseAttachmentsSize = attachments
.reduce((acc, curr) => { .reduce((acc, curr) => {
return acc + curr.attachmentsInNotePaths.reduce((racc, rcurr) => { const stats = fs.statSync(curr.path)
const stats = fs.statSync(rcurr)
const fileSizeInBytes = stats.size const fileSizeInBytes = stats.size
return racc + fileSizeInBytes return acc + fileSizeInBytes
}, 0)
}, 0) }, 0)
const totalAttachmentsSize = totalUnusedAttachmentsSize + totalInuseAttachmentsSize const totalAttachmentsSize = totalUnusedAttachmentsSize + totalInuseAttachmentsSize