From 0cd7ebf9d389754e79153303e4e6135782d9ce47 Mon Sep 17 00:00:00 2001 From: Laurent Cozic Date: Sun, 30 Sep 2018 19:24:02 +0100 Subject: [PATCH] Electron: Fixes resources being incorrectly auto-deleted when inside an IMG tag --- CliClient/tests/models_Note.js | 6 ++++++ CliClient/tests/services_ResourceService.js | 20 +++++++++++++++++++- ReactNativeClient/lib/models/Note.js | 17 ++++++++++++++--- 3 files changed, 39 insertions(+), 4 deletions(-) diff --git a/CliClient/tests/models_Note.js b/CliClient/tests/models_Note.js index 145cf298cc2..ddbd761932c 100644 --- a/CliClient/tests/models_Note.js +++ b/CliClient/tests/models_Note.js @@ -34,6 +34,12 @@ describe('models_Note', function() { expect(items.length).toBe(2); expect(items[0].type_).toBe(BaseModel.TYPE_NOTE); expect(items[1].type_).toBe(BaseModel.TYPE_RESOURCE); + + const resource = items[1]; + note2.body += 'bla'; + note2.body += ''; + items = await Note.linkedItems(note2.body); + expect(items.length).toBe(4); })); }); \ No newline at end of file diff --git a/CliClient/tests/services_ResourceService.js b/CliClient/tests/services_ResourceService.js index f861e280770..73280a5db88 100644 --- a/CliClient/tests/services_ResourceService.js +++ b/CliClient/tests/services_ResourceService.js @@ -79,7 +79,6 @@ describe('services_ResourceService', function() { let note2 = await Note.save({ title: 'ma deuxième note', parent_id: folder1.id }); note1 = await shim.attachFileToNote(note1, __dirname + '/../tests/support/photo.jpg'); let resource1 = (await Resource.all())[0]; - const resourcePath = Resource.fullPath(resource1); await service.indexNoteResources(); @@ -106,4 +105,23 @@ describe('services_ResourceService', function() { expect((await Resource.all()).length).toBe(1); })); + it('should not delete resource if it is used in an IMG tag', asyncTest(async () => { + const service = new ResourceService(); + + let folder1 = await Folder.save({ title: "folder1" }); + let note1 = await Note.save({ title: 'ma note', parent_id: folder1.id }); + note1 = await shim.attachFileToNote(note1, __dirname + '/../tests/support/photo.jpg'); + let resource1 = (await Resource.all())[0]; + + await service.indexNoteResources(); + + await Note.save({ id: note1.id, body: 'This is HTML: ' }); + + await service.indexNoteResources(); + + await service.deleteOrphanResources(0); + + expect(!!(await Resource.load(resource1.id))).toBe(true); + })); + }); \ No newline at end of file diff --git a/ReactNativeClient/lib/models/Note.js b/ReactNativeClient/lib/models/Note.js index 49ebbe4a2c9..f79831d7993 100644 --- a/ReactNativeClient/lib/models/Note.js +++ b/ReactNativeClient/lib/models/Note.js @@ -114,9 +114,20 @@ class Note extends BaseItem { static linkedItemIds(body) { // For example: ![](:/fcca2938a96a22570e8eae2565bc6b0b) if (!body || body.length <= 32) return []; - const matches = body.match(/\(:\/.{32}\)/g); - if (!matches) return []; - return matches.map((m) => m.substr(3, 32)); + let matches = body.match(/\(:\/[a-zA-Z0-9]{32}\)/g); + if (!matches) matches = []; + matches = matches.map((m) => m.substr(3, 32)); + + // For example: + const imgRegex = /