1
0
mirror of https://github.com/BoostIo/Boostnote synced 2025-12-13 09:46:22 +00:00

Deleting a note should also delete the attachments -> fixes #1900

This commit is contained in:
ehhc
2018-05-10 21:36:58 +02:00
parent 90e8dd038d
commit 73ba8b8b13
4 changed files with 42 additions and 0 deletions

View File

@@ -4,6 +4,7 @@ const path = require('path')
const findStorage = require('browser/lib/findStorage') const findStorage = require('browser/lib/findStorage')
const mdurl = require('mdurl') const mdurl = require('mdurl')
const escapeStringRegexp = require('escape-string-regexp') const escapeStringRegexp = require('escape-string-regexp')
const sander = require('sander')
const STORAGE_FOLDER_PLACEHOLDER = ':storage' const STORAGE_FOLDER_PLACEHOLDER = ':storage'
const DESTINATION_FOLDER = 'attachments' const DESTINATION_FOLDER = 'attachments'
@@ -190,6 +191,17 @@ function removeStorageAndNoteReferences (input, noteKey) {
return input.replace(new RegExp(mdurl.encode(path.sep), 'g'), path.sep).replace(new RegExp(STORAGE_FOLDER_PLACEHOLDER + escapeStringRegexp(path.sep) + noteKey, 'g'), DESTINATION_FOLDER) return input.replace(new RegExp(mdurl.encode(path.sep), 'g'), path.sep).replace(new RegExp(STORAGE_FOLDER_PLACEHOLDER + escapeStringRegexp(path.sep) + noteKey, 'g'), DESTINATION_FOLDER)
} }
/**
* @description Deletes the attachment folder specified by the given storageKey and noteKey
* @param storageKey Key of the storage of the note to be deleted
* @param noteKey Key of the note to be deleted
*/
function deleteAttachmentFolder (storageKey, noteKey) {
const storagePath = findStorage.findStorage(storageKey)
const noteAttachmentPath = path.join(storagePath.path, DESTINATION_FOLDER, noteKey)
sander.rimraf(noteAttachmentPath)
}
module.exports = { module.exports = {
copyAttachment, copyAttachment,
fixLocalURLS, fixLocalURLS,
@@ -199,6 +211,7 @@ module.exports = {
getAttachmentsInContent, getAttachmentsInContent,
getAbsolutePathsOfAttachmentsInContent, getAbsolutePathsOfAttachmentsInContent,
removeStorageAndNoteReferences, removeStorageAndNoteReferences,
deleteAttachmentFolder,
STORAGE_FOLDER_PLACEHOLDER, STORAGE_FOLDER_PLACEHOLDER,
DESTINATION_FOLDER DESTINATION_FOLDER
} }

View File

@@ -1,6 +1,7 @@
const resolveStorageData = require('./resolveStorageData') const resolveStorageData = require('./resolveStorageData')
const path = require('path') const path = require('path')
const sander = require('sander') const sander = require('sander')
const attachmentManagement = require('./attachmentManagement')
const { findStorage } = require('browser/lib/findStorage') const { findStorage } = require('browser/lib/findStorage')
function deleteNote (storageKey, noteKey) { function deleteNote (storageKey, noteKey) {
@@ -25,6 +26,10 @@ function deleteNote (storageKey, noteKey) {
storageKey storageKey
} }
}) })
.then(function deleteAttachments (storageInfo) {
attachmentManagement.deleteAttachmentFolder(storageInfo.storageKey, storageInfo.noteKey)
return storageInfo
})
} }
module.exports = deleteNote module.exports = deleteNote

View File

@@ -7,6 +7,7 @@ const findStorage = require('browser/lib/findStorage')
jest.mock('unique-slug') jest.mock('unique-slug')
const uniqueSlug = require('unique-slug') const uniqueSlug = require('unique-slug')
const mdurl = require('mdurl') const mdurl = require('mdurl')
const sander = require('sander')
const systemUnderTest = require('browser/main/lib/dataApi/attachmentManagement') const systemUnderTest = require('browser/main/lib/dataApi/attachmentManagement')
@@ -260,3 +261,16 @@ it('should remove the all ":storage" and noteKey references', function () {
const actual = systemUnderTest.removeStorageAndNoteReferences(testInput, noteKey) const actual = systemUnderTest.removeStorageAndNoteReferences(testInput, noteKey)
expect(actual).toEqual(expectedOutput) expect(actual).toEqual(expectedOutput)
}) })
it('should delete the correct attachment folder if a note is deleted', function () {
const dummyStorage = {path: 'dummyStoragePath'}
const storageKey = 'storageKey'
const noteKey = 'noteKey'
findStorage.findStorage = jest.fn(() => dummyStorage)
sander.rimraf = jest.fn()
const expectedPathToBeDeleted = path.join(dummyStorage.path, systemUnderTest.DESTINATION_FOLDER, noteKey)
systemUnderTest.deleteAttachmentFolder(storageKey, noteKey)
expect(findStorage.findStorage).toHaveBeenCalledWith(storageKey)
expect(sander.rimraf).toHaveBeenCalledWith(expectedPathToBeDeleted)
})

View File

@@ -14,6 +14,8 @@ const sander = require('sander')
const os = require('os') const os = require('os')
const CSON = require('@rokt33r/season') const CSON = require('@rokt33r/season')
const faker = require('faker') const faker = require('faker')
const fs = require('fs')
const attachmentManagement = require('browser/main/lib/dataApi/attachmentManagement')
const storagePath = path.join(os.tmpdir(), 'test/delete-note') const storagePath = path.join(os.tmpdir(), 'test/delete-note')
@@ -42,6 +44,10 @@ test.serial('Delete a note', (t) => {
return Promise.resolve() return Promise.resolve()
.then(function doTest () { .then(function doTest () {
return createNote(storageKey, input1) return createNote(storageKey, input1)
.then(function createAttachmentFolder (data) {
fs.mkdirSync(path.join(storagePath, attachmentManagement.DESTINATION_FOLDER, data.noteKey))
return data
})
.then(function (data) { .then(function (data) {
return deleteNote(storageKey, data.key) return deleteNote(storageKey, data.key)
}) })
@@ -54,6 +60,10 @@ test.serial('Delete a note', (t) => {
t.is(err.code, 'ENOENT') t.is(err.code, 'ENOENT')
} }
}) })
.then(function assertAttachmentFolderDeleted (data) {
const attachmentFolderPath = path.join(storagePath, attachmentManagement.DESTINATION_FOLDER, data.noteKey)
t.assert(fs.existsSync(attachmentFolderPath) === false, 'Attachment folder was not deleted')
})
}) })
test.after(function after () { test.after(function after () {