Skip to content

Commit

Permalink
Electron: Fixes resources being incorrectly auto-deleted when inside …
Browse files Browse the repository at this point in the history
…an IMG tag
  • Loading branch information
laurent22 committed Sep 30, 2018
1 parent a816498 commit 0cd7ebf
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
6 changes: 6 additions & 0 deletions CliClient/tests/models_Note.js
Expand Up @@ -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 += '<img alt="bla" src=":/' + resource.id + '"/>';
note2.body += '<img src=\':/' + resource.id + '\' />';
items = await Note.linkedItems(note2.body);
expect(items.length).toBe(4);
}));

});
20 changes: 19 additions & 1 deletion CliClient/tests/services_ResourceService.js
Expand Up @@ -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();

Expand All @@ -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: <img src=":/' + resource1.id + '"/>' });

await service.indexNoteResources();

await service.deleteOrphanResources(0);

expect(!!(await Resource.load(resource1.id))).toBe(true);
}));

});
17 changes: 14 additions & 3 deletions ReactNativeClient/lib/models/Note.js
Expand Up @@ -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: <img src=":/fcca2938a96a22570e8eae2565bc6b0b"/>
const imgRegex = /<img.*?src=["']:\/([a-zA-Z0-9]{32})["']/g
const imgMatches = [];
while (true) {
const m = imgRegex.exec(body);
if (!m) break;
imgMatches.push(m[1]);
}

return matches.concat(imgMatches);
}

static async linkedItems(body) {
Expand Down

3 comments on commit 0cd7ebf

@tessus
Copy link
Collaborator

@tessus tessus commented on 0cd7ebf Sep 30, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this really only apply to Electron? I would imagine this applied to all applications. Or am I missing something?

@laurent22
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, that actually applies to all applications as this code is shared by all apps.

@tessus
Copy link
Collaborator

@tessus tessus commented on 0cd7ebf Sep 30, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the info. The commit message puzzled me a bit...

Please sign in to comment.