1
0
mirror of https://github.com/BoostIo/Boostnote synced 2025-12-14 02:06:29 +00:00

CodeReview

This commit is contained in:
ehhc
2018-06-05 11:16:50 +02:00
parent 225916fbba
commit b526d48946

View File

@@ -245,41 +245,41 @@ function deleteAttachmentFolder (storageKey, noteKey) {
* @param noteKey NoteKey of the current note. Is used to determine the belonging attachment folder. * @param noteKey NoteKey of the current note. Is used to determine the belonging attachment folder.
*/ */
function deleteAttachmentsNotPresentInNote (markdownContent, storageKey, noteKey) { function deleteAttachmentsNotPresentInNote (markdownContent, storageKey, noteKey) {
if (storageKey != null && noteKey != null && markdownContent != null) { if (storageKey == null || noteKey == null || markdownContent == null) {
const targetStorage = findStorage.findStorage(storageKey) return
const attachmentFolder = path.join(targetStorage.path, DESTINATION_FOLDER, noteKey) }
const attachmentsInNote = getAttachmentsInContent(markdownContent) const targetStorage = findStorage.findStorage(storageKey)
const attachmentsInNoteOnlyFileNames = [] const attachmentFolder = path.join(targetStorage.path, DESTINATION_FOLDER, noteKey)
if (attachmentsInNote) { const attachmentsInNote = getAttachmentsInContent(markdownContent)
for (let i = 0; i < attachmentsInNote.length; i++) { const attachmentsInNoteOnlyFileNames = []
attachmentsInNoteOnlyFileNames.push(attachmentsInNote[i].replace(new RegExp(STORAGE_FOLDER_PLACEHOLDER + escapeStringRegexp(path.sep) + noteKey + escapeStringRegexp(path.sep), 'g'), '')) 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)) {
fs.readdir(attachmentFolder, (err, files) => {
if (err) {
console.error('Error reading directory \'' + attachmentFolder + '\'. Error:')
console.error(err)
return
} }
} files.forEach(file => {
if (!attachmentsInNoteOnlyFileNames.includes(file)) {
if (fs.existsSync(attachmentFolder)) { const absolutePathOfFile = path.join(targetStorage.path, DESTINATION_FOLDER, noteKey, file)
fs.readdir(attachmentFolder, (err, files) => { fs.unlink(absolutePathOfFile, (err) => {
if (err) { if (err) {
console.error("Error reading directory '" + attachmentFolder + "'. Error:") console.error('Could not delete \'%s\'', absolutePathOfFile)
console.error(err) console.error(err)
return return
}
console.info('File \'' + absolutePathOfFile + '\' deleted because it was not included in the content of the note')
})
} }
files.forEach(file => {
if (!attachmentsInNoteOnlyFileNames.includes(file)) {
const absolutePathOfFile = path.join(targetStorage.path, DESTINATION_FOLDER, noteKey, file)
fs.unlink(absolutePathOfFile, (err) => {
if (err) {
console.error("Could not delete '%s'", absolutePathOfFile)
console.error(err)
return
}
console.info("File '" + absolutePathOfFile + "' deleted because it was not included in the content of the note")
})
}
})
}) })
} else { })
console.info("Attachment folder ('" + attachmentFolder + "') did not exist..") } else {
} console.debug('Attachment folder (\'' + attachmentFolder + '\') did not exist..')
} }
} }