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,43 @@ |
{ |
function onLoaded(entry) |
{ |
- for (let line of entry.content) |
- listener.process(line); |
- |
- listener.process(null); |
+ if ("content" in entry) |
+ { |
+ if ("compressed" in entry) |
+ 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) |
+ // Edge cannot write files larger than 1Mb (500k string in UTF-16). |
kzar
2016/08/30 14:13:02
I think this comment should be inside the else blo
Oleksandr
2016/08/31 21:54:37
Done.
|
+ // However it does not always raise an exception when trying to do so. |
+ if (typeof browser == "undefined") |
{ |
- saveFile(toFile, entry.content, callback); |
+ entry[key] = {lastModified: Date.now(), |
kzar
2016/08/30 14:13:03
Nit: No need to wrap this into two lines if it wil
Oleksandr
2016/08/31 21:54:37
Done.
|
+ 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); |
- }); |
+ // Compress and fallback to localStorage |
+ let processedData = LZString.compressToUTF16(JSON.stringify(data)); |
+ ext.storage.remove(key); |
+ entry[key] = {lastModified: Date.now(), |
kzar
2016/08/30 14:13:02
Nit: This could fit on two lines instead of three.
Oleksandr
2016/08/31 21:54:37
Done. However it looked better as it was, IMHO.
kzar
2016/09/01 12:57:37
I can't see the change?
|
+ 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 +109,6 @@ |
}); |
} |
- loadFile(file, onLoaded, callback); |
+ loadFile(file).then(onLoaded, callback); |
} |
}; |