Index: lib/io.js |
=================================================================== |
--- a/lib/io.js |
+++ b/lib/io.js |
@@ -15,6 +15,8 @@ |
* along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
*/ |
+"use strict"; |
+ |
const keyPrefix = "file:"; |
function fileToKey(file) |
@@ -22,33 +24,31 @@ |
return keyPrefix + (file instanceof FakeFile ? file.path : file.spec); |
} |
-function loadFile(file, successCallback, errorCallback) |
+function loadFile(file) |
{ |
- let key = fileToKey(file); |
- |
- ext.storage.get([key], function(items) |
+ return new Promise((resolve, reject) => |
{ |
- let entry = items[key]; |
- |
- if (entry) |
- successCallback(entry); |
- else |
- errorCallback(new Error("File doesn't exist")); |
+ let key = fileToKey(file); |
+ ext.storage.get([key], items => |
+ { |
+ let entry = items[key]; |
+ if (!entry) |
+ { |
+ // Check in localStorage |
+ try |
+ { |
+ entry = JSON.parse(window.localStorage.getItem(key)); |
+ } |
+ catch(err) {} |
+ } |
+ if (entry) |
+ resolve(entry); |
+ else |
+ reject(new Error("File doesn't exist")); |
+ }); |
}); |
} |
-function saveFile(file, data, callback) |
-{ |
- ext.storage.set( |
- fileToKey(file), |
- { |
- content: data, |
- lastModified: Date.now() |
- }, |
- callback |
- ); |
-} |
- |
exports.IO = |
{ |
resolveFilePath: function(path) |
@@ -60,47 +60,41 @@ |
{ |
function onLoaded(entry) |
{ |
- for (let line of entry.content) |
- listener.process(line); |
- |
- listener.process(null); |
+ if ("content" in entry) |
+ { |
+ if ("compressed" in entry) |
Sebastian Noack
2016/09/08 15:27:22
Please check for entry["compressed"], otherwise, i
|
+ entry.content = JSON.parse(LZString.decompressFromUTF16(entry.content)); |
+ for (let line of entry.content) |
+ listener.process(line); |
+ } |
callback(null); |
} |
- loadFile(file, onLoaded, callback); |
+ loadFile(file).then(onLoaded, callback); |
}, |
writeToFile: function(file, data, callback) |
{ |
- saveFile(file, data, callback); |
- }, |
+ let entry = {}; |
+ let key = fileToKey(file); |
- copyFile: function(fromFile, toFile, callback) |
- { |
- function onLoaded(entry) |
+ if (typeof browser == "undefined") |
{ |
- saveFile(toFile, entry.content, callback); |
+ entry[key] = {lastModified: Date.now(), content: data}; |
+ ext.storage.set(entry, callback); |
} |
- |
- loadFile(fromFile, onLoaded, callback); |
- }, |
- |
- renameFile: function(fromFile, newName, callback) |
- { |
- function onLoaded() |
+ else |
{ |
- ext.storage.remove(fileToKey(fromFile), function() |
- { |
- ext.storage.set(keyPrefix + newName, entry, callback); |
- }); |
+ // Edge cannot write files larger than 1Mb (500k string in UTF-16) via |
+ // storage API. However it does not always raise an exception when trying |
+ // to do so. The solution is to compress and fallback to localStorage. |
+ let processedData = LZString.compressToUTF16(JSON.stringify(data)); |
Sebastian Noack
2016/09/08 15:27:22
(Why) do we need to call JSON.stringifiy() here? I
Oleksandr
2016/09/08 23:51:57
'data' here is an array of strings, which are line
|
+ ext.storage.remove(key); |
+ entry[key] = {lastModified: Date.now(), content: processedData, |
+ compressed: true}; |
+ window.localStorage.setItem(key, JSON.stringify(entry[key])); |
+ callback(); |
} |
- |
- loadFile(fromFile, onLoaded, callback); |
- }, |
- |
- removeFile: function(file, callback) |
- { |
- ext.storage.remove(fileToKey(file), callback); |
}, |
statFile: function(file, callback) |
@@ -113,6 +107,6 @@ |
}); |
} |
- loadFile(file, onLoaded, callback); |
+ loadFile(file).then(onLoaded, callback); |
} |
}; |