Electron: Fixes resources being incorrectly auto-deleted when inside an IMG tag

pull/833/head
Laurent Cozic 2018-09-30 19:24:02 +01:00
parent a816498fc6
commit 0cd7ebf9d3
3 changed files with 39 additions and 4 deletions

View File

@ -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);
}));
});

View File

@ -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: <img src=":/' + resource1.id + '"/>' });
await service.indexNoteResources();
await service.deleteOrphanResources(0);
expect(!!(await Resource.load(resource1.id))).toBe(true);
}));
});

View File

@ -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) {