Skip to content

Commit 2a6d950

Browse files
authored
Merge pull request #1902 from ehhc/Issue_1900
Deleting a note should also delete the attachments -> fixes #1900
2 parents b03b9d5 + 905d686 commit 2a6d950

File tree

4 files changed

+44
-0
lines changed

4 files changed

+44
-0
lines changed

browser/main/lib/dataApi/attachmentManagement.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const path = require('path')
44
const findStorage = require('browser/lib/findStorage')
55
const mdurl = require('mdurl')
66
const escapeStringRegexp = require('escape-string-regexp')
7+
const sander = require('sander')
78

89
const STORAGE_FOLDER_PLACEHOLDER = ':storage'
910
const DESTINATION_FOLDER = 'attachments'
@@ -190,6 +191,17 @@ function removeStorageAndNoteReferences (input, noteKey) {
190191
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)
191192
}
192193

194+
/**
195+
* @description Deletes the attachment folder specified by the given storageKey and noteKey
196+
* @param storageKey Key of the storage of the note to be deleted
197+
* @param noteKey Key of the note to be deleted
198+
*/
199+
function deleteAttachmentFolder (storageKey, noteKey) {
200+
const storagePath = findStorage.findStorage(storageKey)
201+
const noteAttachmentPath = path.join(storagePath.path, DESTINATION_FOLDER, noteKey)
202+
sander.rimrafSync(noteAttachmentPath)
203+
}
204+
193205
/**
194206
* @description Deletes all attachments stored in the attachment folder of the give not that are not referenced in the markdownContent
195207
* @param markdownContent Content of the note. All unreferenced notes will be deleted
@@ -242,6 +254,7 @@ module.exports = {
242254
getAttachmentsInContent,
243255
getAbsolutePathsOfAttachmentsInContent,
244256
removeStorageAndNoteReferences,
257+
deleteAttachmentFolder,
245258
deleteAttachmentsNotPresentInNote,
246259
STORAGE_FOLDER_PLACEHOLDER,
247260
DESTINATION_FOLDER

browser/main/lib/dataApi/deleteNote.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const resolveStorageData = require('./resolveStorageData')
22
const path = require('path')
33
const sander = require('sander')
4+
const attachmentManagement = require('./attachmentManagement')
45
const { findStorage } = require('browser/lib/findStorage')
56

67
function deleteNote (storageKey, noteKey) {
@@ -25,6 +26,10 @@ function deleteNote (storageKey, noteKey) {
2526
storageKey
2627
}
2728
})
29+
.then(function deleteAttachments (storageInfo) {
30+
attachmentManagement.deleteAttachmentFolder(storageInfo.storageKey, storageInfo.noteKey)
31+
return storageInfo
32+
})
2833
}
2934

3035
module.exports = deleteNote

tests/dataApi/attachmentManagement.test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const findStorage = require('browser/lib/findStorage')
77
jest.mock('unique-slug')
88
const uniqueSlug = require('unique-slug')
99
const mdurl = require('mdurl')
10+
const sander = require('sander')
1011

1112
const systemUnderTest = require('browser/main/lib/dataApi/attachmentManagement')
1213

@@ -261,6 +262,19 @@ it('should remove the all ":storage" and noteKey references', function () {
261262
expect(actual).toEqual(expectedOutput)
262263
})
263264

265+
it('should delete the correct attachment folder if a note is deleted', function () {
266+
const dummyStorage = {path: 'dummyStoragePath'}
267+
const storageKey = 'storageKey'
268+
const noteKey = 'noteKey'
269+
findStorage.findStorage = jest.fn(() => dummyStorage)
270+
sander.rimrafSync = jest.fn()
271+
272+
const expectedPathToBeDeleted = path.join(dummyStorage.path, systemUnderTest.DESTINATION_FOLDER, noteKey)
273+
systemUnderTest.deleteAttachmentFolder(storageKey, noteKey)
274+
expect(findStorage.findStorage).toHaveBeenCalledWith(storageKey)
275+
expect(sander.rimrafSync).toHaveBeenCalledWith(expectedPathToBeDeleted)
276+
})
277+
264278
it('should test that deleteAttachmentsNotPresentInNote deletes all unreferenced attachments ', function () {
265279
const dummyStorage = {path: 'dummyStoragePath'}
266280
const noteKey = 'noteKey'

tests/dataApi/deleteNote-test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ const sander = require('sander')
1414
const os = require('os')
1515
const CSON = require('@rokt33r/season')
1616
const faker = require('faker')
17+
const fs = require('fs')
18+
const attachmentManagement = require('browser/main/lib/dataApi/attachmentManagement')
1719

1820
const storagePath = path.join(os.tmpdir(), 'test/delete-note')
1921

@@ -42,6 +44,11 @@ test.serial('Delete a note', (t) => {
4244
return Promise.resolve()
4345
.then(function doTest () {
4446
return createNote(storageKey, input1)
47+
.then(function createAttachmentFolder (data) {
48+
fs.mkdirSync(path.join(storagePath, attachmentManagement.DESTINATION_FOLDER))
49+
fs.mkdirSync(path.join(storagePath, attachmentManagement.DESTINATION_FOLDER, data.key))
50+
return data
51+
})
4552
.then(function (data) {
4653
return deleteNote(storageKey, data.key)
4754
})
@@ -52,8 +59,13 @@ test.serial('Delete a note', (t) => {
5259
t.fail('note cson must be deleted.')
5360
} catch (err) {
5461
t.is(err.code, 'ENOENT')
62+
return data
5563
}
5664
})
65+
.then(function assertAttachmentFolderDeleted (data) {
66+
const attachmentFolderPath = path.join(storagePath, attachmentManagement.DESTINATION_FOLDER, data.noteKey)
67+
t.is(fs.existsSync(attachmentFolderPath), false)
68+
})
5769
})
5870

5971
test.after(function after () {

0 commit comments

Comments
 (0)