file open bugfixes

This commit is contained in:
Antelle 2015-12-08 00:00:44 +03:00
parent 917c4fe6bf
commit 2d5fb037fd
9 changed files with 56 additions and 87 deletions

View File

@ -253,7 +253,7 @@ var DropboxLink = {
},
openFile: function(fileName, complete, errorAlertCallback) {
this._callAndHandleError('readFile', [fileName, { blob: true }], complete, errorAlertCallback);
this._callAndHandleError('readFile', [fileName, { arrayBuffer: true }], complete, errorAlertCallback);
},
getFileList: function(complete) {

View File

@ -253,7 +253,7 @@ var AppModel = Backbone.Model.extend({
} else if (params.fileData) {
// has user content: load it
this.openFileWithData(params, callback, fileInfo, params.fileData, true);
} else if (fileInfo && fileInfo.get('availOffline') && fileInfo.rev === params.rev) {
} else if (fileInfo && fileInfo.get('availOffline') && fileInfo.get('rev') === params.rev) {
// already latest in cache: use it
this.openFileFromCache(params, callback, fileInfo);
} else {
@ -277,7 +277,7 @@ var AppModel = Backbone.Model.extend({
openFileFromCache: function(params, callback, fileInfo) {
var that = this;
Storage.cache.load(fileInfo.id, function(data, err) {
Storage.cache.load(fileInfo.id, function(err, data) {
if (err) {
callback(err);
} else {
@ -329,7 +329,7 @@ var AppModel = Backbone.Model.extend({
availOffline: file.get('availOffline'),
modified: file.get('modified'),
editState: null,
pullRev: rev,
rev: rev,
pullDate: dt,
openDate: dt
});

View File

@ -11,7 +11,7 @@ var FileInfoModel = Backbone.Model.extend({
availOffline: false,
modified: false,
editState: null,
pullRev: null,
rev: null,
pullDate: null,
openDate: null
},

View File

@ -11,7 +11,7 @@ var StorageCache = {
init: function(callback) {
if (this.db) {
return callback();
return callback && callback();
}
var that = this;
try {
@ -19,11 +19,11 @@ var StorageCache = {
req.onerror = function (e) {
console.error('Error opening indexed db', e);
that.errorOpening = e;
callback(e);
if (callback) { callback(e); }
};
req.onsuccess = function (e) {
that.db = e.target.result;
callback();
if (callback) { callback(); }
};
req.onupgradeneeded = function (e) {
var db = e.target.result;
@ -31,31 +31,27 @@ var StorageCache = {
};
} catch (e) {
console.error('Error opening indexed db', e);
callback(e);
if (callback) { callback(e); }
}
},
save: function(id, data, callback) {
this.init((function(err) {
if (err) {
return callback(err);
return callback && callback(err);
}
try {
var req = this.db.transaction(['files'], 'readwrite').objectStore('files').put(data, id);
req.onsuccess = function () {
if (callback) {
callback();
}
if (callback) { callback(); }
};
req.onerror = function () {
console.error('Error saving to cache', id, req.error);
if (callback) {
callback(req.error);
}
if (callback) { callback(req.error); }
};
} catch (e) {
console.error('Error saving to cache', id, e);
callback(e);
if (callback) { callback(e); }
}
}).bind(this));
},
@ -63,24 +59,20 @@ var StorageCache = {
load: function(id, callback) {
this.init((function(err) {
if (err) {
return callback(null, err);
return callback && callback(err, null);
}
try {
var req = this.db.transaction(['files'], 'readonly').objectStore('files').get(id);
req.onsuccess = function () {
if (callback) {
callback(req.result);
}
if (callback) { callback(null, req.result); }
};
req.onerror = function () {
console.error('Error loading from cache', id, req.error);
if (callback) {
callback(null, req.error);
}
if (callback) { callback(req.error); }
};
} catch (e) {
console.error('Error loading from cache', id, e);
callback(null, e);
if (callback) { callback(e, null); }
}
}).bind(this));
},
@ -88,24 +80,20 @@ var StorageCache = {
remove: function(id, callback) {
this.init((function(err) {
if (err) {
return callback(err);
return callback && callback(err);
}
try {
var req = this.db.transaction(['files'], 'readwrite').objectStore('files').delete(id);
req.onsuccess = function () {
if (callback) {
callback();
}
if (callback) { callback(); }
};
req.onerror = function () {
console.error('Error removing from cache', id, req.error);
if (callback) {
callback(req.error);
}
if (callback) { callback(req.error); }
};
} catch(e) {
console.error('Error removing from cache', id, e);
callback(e);
if (callback) { callback(e); }
}
}).bind(this));
}

View File

@ -8,12 +8,12 @@ var StorageDropbox = {
load: function(path, callback) {
DropboxLink.openFile(path, function(err, data, stat) {
callback(err, data, stat ? stat.versionTag : null);
if (callback) { callback(err, data, stat ? stat.versionTag : null); }
});
},
save: function(path, data, callback) {
DropboxLink.saveFile(path, data, true, callback);
DropboxLink.saveFile(path, data, true, callback || _.noop);
}
};

View File

@ -14,36 +14,32 @@ var StorageFileCache = {
init: function(callback) {
if (this.path) {
return callback();
return callback && callback();
}
if (Launcher) {
try {
var path = Launcher.getUserDataPath('OfflineFiles');
var fs = Launcher.req('fs');
if (!fs.existsSync(path)) {
fs.mkdirSync(path);
}
this.path = path;
} catch (e) {
console.error('Error opening local offline storage', e);
callback(e);
try {
var path = Launcher.getUserDataPath('OfflineFiles');
var fs = Launcher.req('fs');
if (!fs.existsSync(path)) {
fs.mkdirSync(path);
}
this.path = path;
} catch (e) {
console.error('Error opening local offline storage', e);
if (callback) { callback(e); }
}
},
save: function(id, data, callback) {
this.init((function(err) {
if (err) {
return callback(err);
return callback && callback(err);
}
try {
if (Launcher) {
Launcher.writeFile(this.getPath(id), data);
return callback();
}
Launcher.writeFile(this.getPath(id), data);
if (callback) { callback(); }
} catch (e) {
console.error('Error saving to cache', id, e);
callback(e);
if (callback) { callback(e); }
}
}).bind(this));
},
@ -51,16 +47,14 @@ var StorageFileCache = {
load: function(id, callback) {
this.init((function(err) {
if (err) {
return callback(null, err);
return callback && callback(null, err);
}
try {
if (Launcher) {
var data = Launcher.readFile(this.getPath(id));
return callback(data.buffer);
}
var data = Launcher.readFile(this.getPath(id));
if (callback) { callback(null, data.buffer); }
} catch (e) {
console.error('Error loading from cache', id, e);
callback(null, e);
if (callback) { callback(e, null); }
}
}).bind(this));
},
@ -68,16 +62,14 @@ var StorageFileCache = {
remove: function(id, callback) {
this.init((function(err) {
if (err) {
return callback(err);
return callback && callback(err);
}
try {
if (Launcher) {
Launcher.deleteFile(this.getPath(id));
return callback();
}
Launcher.deleteFile(this.getPath(id));
if (callback) { callback(); }
} catch(e) {
console.error('Error removing from cache', id, e);
callback(e);
if (callback) { callback(e); }
}
}).bind(this));
}

View File

@ -9,20 +9,20 @@ var StorageFile = {
load: function(path, callback) {
try {
var data = Launcher.readFile(path);
callback(data.buffer);
if (callback) { callback(null, data.buffer); }
} catch (e) {
console.error('Error reading local file', path, e);
callback(null, e);
if (callback) { callback(e, null); }
}
},
save: function(path, data, callback) {
try {
Launcher.writeFile(path, data);
callback();
if (callback) { callback(); }
} catch (e) {
console.error('Error writing local file', path, e);
callback(e);
if (callback) { callback(e); }
}
}
};

View File

@ -377,7 +377,10 @@ var AppView = Backbone.View.extend({
var firstFile = this.model.files.find(function(file) { return !file.get('demo') && !file.get('created'); });
this.model.closeAllFiles();
if (firstFile) {
this.views.open.showClosedFile(firstFile);
var fileInfo = this.model.fileInfos.getMatch(firstFile.get('storage'), firstFile.get('name'), firstFile.get('path'));
if (fileInfo) {
this.views.open.showOpenFileInfo(fileInfo);
}
}
},

View File

@ -146,20 +146,6 @@ var OpenView = Backbone.View.extend({
}).bind(this));
},
showClosedFile: function(file) {
this.params = {
id: file.get('id'),
name: file.get('name'),
storage: file.get('storage'),
path: file.get('path'),
availOffline: file.get('availOffline'),
keyFileName: null,
keyFileData: null,
fileData: file.data
};
this.displayOpenFile();
},
openFile: function() {
if (!this.busy) {
this.openAny('fileData');
@ -297,8 +283,8 @@ var OpenView = Backbone.View.extend({
filesStat.forEach(function(file) {
if (!file.isFolder && !file.isRemoved) {
var fileName = file.name.replace(/\.kdbx/i, '');
buttons.push({ result: file.name, title: fileName });
allDropboxFiles[file.name] = file;
buttons.push({ result: file.path, title: fileName });
allDropboxFiles[file.path] = file;
}
});
if (!buttons.length) {
@ -322,7 +308,7 @@ var OpenView = Backbone.View.extend({
}
});
that.model.fileInfos.forEach(function(fi) {
if (fi.get('storage') === 'dropbox' && !fi.get('modified') && !allDropboxFiles[fi.get('name')]) {
if (fi.get('storage') === 'dropbox' && !fi.get('modified') && !allDropboxFiles[fi.get('path')]) {
that.model.removeFileInfo(fi.id);
}
});