From e89447dd85e9ebc547aea9dca56c65cd7a621d5c Mon Sep 17 00:00:00 2001 From: Laurent Cozic Date: Fri, 23 Jun 2017 23:19:59 +0100 Subject: [PATCH] Fixed OneDrive sync issue --- .../src/file-api-driver-onedrive.js | 19 +++++++++++++++++-- ReactNativeClient/src/logger.js | 16 ++++++++++++++-- ReactNativeClient/src/models/folder.js | 2 +- ReactNativeClient/src/synchronizer.js | 3 ++- 4 files changed, 34 insertions(+), 6 deletions(-) diff --git a/ReactNativeClient/src/file-api-driver-onedrive.js b/ReactNativeClient/src/file-api-driver-onedrive.js index 18e2e8bbdc..a52598a2f4 100644 --- a/ReactNativeClient/src/file-api-driver-onedrive.js +++ b/ReactNativeClient/src/file-api-driver-onedrive.js @@ -40,7 +40,7 @@ class FileApiDriverOneDrive { }; } - async stat(path) { + async statRaw_(path) { let item = null; try { item = await this.api_.execJson('GET', this.makePath_(path), this.itemFilter_()); @@ -48,6 +48,12 @@ class FileApiDriverOneDrive { if (error.error.code == 'itemNotFound') return null; throw error; } + return item; + } + + async stat(path) { + let item = await this.statRaw_(path); + if (!item) return null; return this.makeItem_(item); } @@ -57,7 +63,8 @@ class FileApiDriverOneDrive { lastModifiedDateTime: moment.unix(timestamp / 1000).utc().format('YYYY-MM-DDTHH:mm:ss.SSS') + 'Z', } }; - await this.api_.exec('PATCH', this.makePath_(path), null, body); + let item = await this.api_.execJson('PATCH', this.makePath_(path), null, body); + return this.makeItem_(item); } async list(path) { @@ -101,12 +108,20 @@ class FileApiDriverOneDrive { } async move(oldPath, newPath) { + let previousItem = await this.statRaw_(oldPath); + let newDir = dirname(newPath); let newName = basename(newPath); + // We don't want the modification date to change when we move the file so retrieve it + // now set it in the PATCH operation. + let item = await this.api_.execJson('PATCH', this.makePath_(oldPath), this.itemFilter_(), { name: newName, parentReference: { path: newDir }, + fileSystemInfo: { + lastModifiedDateTime: previousItem.fileSystemInfo.lastModifiedDateTime, + }, }); return this.makeItem_(item); diff --git a/ReactNativeClient/src/logger.js b/ReactNativeClient/src/logger.js index 49a1e0f831..b00d739f11 100644 --- a/ReactNativeClient/src/logger.js +++ b/ReactNativeClient/src/logger.js @@ -52,8 +52,20 @@ class Logger { console[fn](line + object); } } else if (t.type == 'file') { - if (typeof object === 'object') object = JSON.stringify(object); - fs.appendFile(t.path, line + object + "\n", (error) => { + let serializedObject = ''; + + if (typeof object === 'object') { + if (object instanceof Error) { + serializedObject = object.toString(); + if (object.stack) serializedObject += "\n" + object.stack; + } else { + serializedObject = JSON.stringify(object); + } + } else { + serializedObject = object; + } + + fs.appendFile(t.path, line + serializedObject + "\n", (error) => { if (error) throw error; }); } diff --git a/ReactNativeClient/src/models/folder.js b/ReactNativeClient/src/models/folder.js index 30f8990d72..8388f7e7f8 100644 --- a/ReactNativeClient/src/models/folder.js +++ b/ReactNativeClient/src/models/folder.js @@ -86,7 +86,7 @@ class Folder extends BaseItem { } static loadNoteByField(folderId, field, value) { - return this.modelSelectAll('SELECT * FROM notes WHERE is_conflict = 0 AND `parent_id` = ? AND `' + field + '` = ?', [folderId, value]); + return this.modelSelectOne('SELECT * FROM notes WHERE is_conflict = 0 AND `parent_id` = ? AND `' + field + '` = ?', [folderId, value]); } static async all(includeNotes = false) { diff --git a/ReactNativeClient/src/synchronizer.js b/ReactNativeClient/src/synchronizer.js index f7c1f697b3..6ba45fe0f0 100644 --- a/ReactNativeClient/src/synchronizer.js +++ b/ReactNativeClient/src/synchronizer.js @@ -93,10 +93,11 @@ class Synchronizer { // Make the operation atomic by doing the work on a copy of the file // and then copying it back to the original location. let tempPath = this.syncDirName_ + '/' + path; + await this.api().put(tempPath, content); await this.api().setTimestamp(tempPath, local.updated_time); await this.api().move(tempPath, path); - + await ItemClass.save({ id: local.id, sync_time: time.unixMs(), type_: local.type_ }, { autoTimestamp: false }); } else if (action == 'folderConflict') {