From 131af9ab12188db406a0245ac4ef2c09cb562196 Mon Sep 17 00:00:00 2001 From: azivner Date: Sun, 18 Feb 2018 22:55:36 -0500 Subject: [PATCH] fix attachment sync --- src/routes/api/attachments.js | 4 ++-- src/routes/api/sync.js | 5 ++++- src/services/sync.js | 11 ++++++++++- src/services/sync_update.js | 9 ++++++++- 4 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/routes/api/attachments.js b/src/routes/api/attachments.js index 0da5b5e14..f319f11a6 100644 --- a/src/routes/api/attachments.js +++ b/src/routes/api/attachments.js @@ -32,8 +32,8 @@ router.post('/upload/:parentNoteId', auth.checkApiAuthOrElectron, multer.single( mime: file.mimetype }, req, sourceId)).noteId; - await attributes.createAttribute(noteId, "original_file_name", originalName); - await attributes.createAttribute(noteId, "file_size", size); + await attributes.createAttribute(noteId, "original_file_name", originalName, sourceId); + await attributes.createAttribute(noteId, "file_size", size, sourceId); res.send({ noteId: noteId diff --git a/src/routes/api/sync.js b/src/routes/api/sync.js index 922b5c503..3b23caa7c 100644 --- a/src/routes/api/sync.js +++ b/src/routes/api/sync.js @@ -79,9 +79,12 @@ router.get('/changed', auth.checkApiAuth, wrap(async (req, res, next) => { router.get('/notes/:noteId', auth.checkApiAuth, wrap(async (req, res, next) => { const noteId = req.params.noteId; + const entity = await sql.getRow("SELECT * FROM notes WHERE noteId = ?", [noteId]); + + sync.serializeNoteContentBuffer(entity); res.send({ - entity: await sql.getRow("SELECT * FROM notes WHERE noteId = ?", [noteId]) + entity: entity }); })); diff --git a/src/services/sync.js b/src/services/sync.js index 7d7e4cb0a..52ae320ad 100644 --- a/src/services/sync.js +++ b/src/services/sync.js @@ -204,6 +204,8 @@ async function pushEntity(sync, syncContext) { if (sync.entityName === 'notes') { entity = await sql.getRow('SELECT * FROM notes WHERE noteId = ?', [sync.entityId]); + + serializeNoteContentBuffer(entity); } else if (sync.entityName === 'note_tree') { entity = await sql.getRow('SELECT * FROM note_tree WHERE noteTreeId = ?', [sync.entityId]); @@ -258,6 +260,12 @@ async function pushEntity(sync, syncContext) { await syncRequest(syncContext, 'PUT', '/api/sync/' + sync.entityName, payload); } +function serializeNoteContentBuffer(note) { + if (note.type === 'file') { + note.content = note.content.toString("binary"); + } +} + async function checkContentHash(syncContext) { const resp = await syncRequest(syncContext, 'GET', '/api/sync/check'); @@ -350,5 +358,6 @@ sql.dbReady.then(() => { }); module.exports = { - sync + sync, + serializeNoteContentBuffer }; \ No newline at end of file diff --git a/src/services/sync_update.js b/src/services/sync_update.js index 31aeb42a7..007a47918 100644 --- a/src/services/sync_update.js +++ b/src/services/sync_update.js @@ -1,10 +1,17 @@ const sql = require('./sql'); const log = require('./log'); const eventLog = require('./event_log'); -const notes = require('./notes'); const sync_table = require('./sync_table'); +function deserializeNoteContentBuffer(note) { + if (note.type === 'file') { + note.content = new Buffer(note.content, 'binary'); + } +} + async function updateNote(entity, sourceId) { + deserializeNoteContentBuffer(entity); + const origNote = await sql.getRow("SELECT * FROM notes WHERE noteId = ?", [entity.noteId]); if (!origNote || origNote.dateModified <= entity.dateModified) {