1
0
mirror of https://github.com/BoostIo/Boostnote synced 2025-12-13 17:56:25 +00:00

Allow user to view attachments and clear unused attachments

This commit is contained in:
Nguyễn Việt Hưng
2019-08-27 22:24:36 +12:00
committed by Junyoung Choi
parent cf324d93fe
commit c7d33fbd83
3 changed files with 168 additions and 4 deletions

View File

@@ -624,6 +624,52 @@ function deleteAttachmentsNotPresentInNote (markdownContent, storageKey, noteKey
}
}
function getAttachments (markdownContent, storageKey, noteKey) {
if (storageKey == null || noteKey == null || markdownContent == null) {
return
}
const targetStorage = findStorage.findStorage(storageKey)
const attachmentFolder = path.join(targetStorage.path, DESTINATION_FOLDER, noteKey)
const attachmentsInNote = getAttachmentsInMarkdownContent(markdownContent)
const attachmentsInNoteOnlyFileNames = []
if (attachmentsInNote) {
for (let i = 0; i < attachmentsInNote.length; i++) {
attachmentsInNoteOnlyFileNames.push(attachmentsInNote[i].replace(new RegExp(STORAGE_FOLDER_PLACEHOLDER + escapeStringRegexp(path.sep) + noteKey + escapeStringRegexp(path.sep), 'g'), ''))
}
}
if (fs.existsSync(attachmentFolder)) {
return new Promise((resolve, reject) => {
fs.readdir(attachmentFolder, (err, files) => {
if (err) {
console.error('Error reading directory "' + attachmentFolder + '". Error:')
console.error(err)
reject(err)
return
}
const attachmentsNotInNotePaths = []
const attachmentsInNotePaths = []
const allAttachments = []
for (const file of files) {
const absolutePathOfFile = path.join(targetStorage.path, DESTINATION_FOLDER, noteKey, file)
if (!attachmentsInNoteOnlyFileNames.includes(file)) {
attachmentsNotInNotePaths.push(absolutePathOfFile)
} else {
attachmentsInNotePaths.push(absolutePathOfFile)
}
allAttachments.push(absolutePathOfFile)
}
resolve({
allAttachments,
attachmentsNotInNotePaths,
attachmentsInNotePaths
})
})
})
} else {
return null
}
}
/**
* Clones the attachments of a given note.
* Copies the attachments to their new destination and updates the content of the new note so that the attachment-links again point to the correct destination.
@@ -728,6 +774,7 @@ module.exports = {
removeStorageAndNoteReferences,
deleteAttachmentFolder,
deleteAttachmentsNotPresentInNote,
getAttachments,
moveAttachments,
cloneAttachments,
isAttachmentLink,