| OLD | NEW |
| 1 /* | 1 /* |
| 2 * This file is part of Adblock Plus <https://adblockplus.org/>, | 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
| 3 * Copyright (C) 2006-2016 Eyeo GmbH | 3 * Copyright (C) 2006-2016 Eyeo GmbH |
| 4 * | 4 * |
| 5 * Adblock Plus is free software: you can redistribute it and/or modify | 5 * Adblock Plus is free software: you can redistribute it and/or modify |
| 6 * it under the terms of the GNU General Public License version 3 as | 6 * it under the terms of the GNU General Public License version 3 as |
| 7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. |
| 8 * | 8 * |
| 9 * Adblock Plus is distributed in the hope that it will be useful, | 9 * Adblock Plus is distributed in the hope that it will be useful, |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 * GNU General Public License for more details. | 12 * GNU General Public License for more details. |
| 13 * | 13 * |
| 14 * You should have received a copy of the GNU General Public License | 14 * You should have received a copy of the GNU General Public License |
| 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
| 16 */ | 16 */ |
| 17 | 17 |
| 18 "use strict"; |
| 19 |
| 18 const keyPrefix = "file:"; | 20 const keyPrefix = "file:"; |
| 19 | 21 |
| 20 function fileToKey(file) | 22 function fileToKey(file) |
| 21 { | 23 { |
| 22 return keyPrefix + (file instanceof FakeFile ? file.path : file.spec); | 24 return keyPrefix + (file instanceof FakeFile ? file.path : file.spec); |
| 23 } | 25 } |
| 24 | 26 |
| 25 function loadFile(file, successCallback, errorCallback) | 27 function loadFile(file) |
| 26 { | 28 { |
| 27 let key = fileToKey(file); | 29 return new Promise((resolve, reject) => |
| 28 | |
| 29 ext.storage.get([key], function(items) | |
| 30 { | 30 { |
| 31 let entry = items[key]; | 31 let key = fileToKey(file); |
| 32 | 32 ext.storage.get([key], items => |
| 33 if (entry) | 33 { |
| 34 successCallback(entry); | 34 let entry = items[key]; |
| 35 else | 35 if (!entry) |
| 36 errorCallback(new Error("File doesn't exist")); | 36 { |
| 37 // Check in localStorage |
| 38 try |
| 39 { |
| 40 entry = JSON.parse(window.localStorage.getItem(key)); |
| 41 } |
| 42 catch(err) {} |
| 43 } |
| 44 if (entry) |
| 45 resolve(entry); |
| 46 else |
| 47 reject(new Error("File doesn't exist")); |
| 48 }); |
| 37 }); | 49 }); |
| 38 } | 50 } |
| 39 | 51 |
| 40 function saveFile(file, data, callback) | |
| 41 { | |
| 42 ext.storage.set( | |
| 43 fileToKey(file), | |
| 44 { | |
| 45 content: data, | |
| 46 lastModified: Date.now() | |
| 47 }, | |
| 48 callback | |
| 49 ); | |
| 50 } | |
| 51 | |
| 52 exports.IO = | 52 exports.IO = |
| 53 { | 53 { |
| 54 resolveFilePath: function(path) | 54 resolveFilePath: function(path) |
| 55 { | 55 { |
| 56 return new FakeFile(path); | 56 return new FakeFile(path); |
| 57 }, | 57 }, |
| 58 | 58 |
| 59 readFromFile: function(file, listener, callback) | 59 readFromFile: function(file, listener, callback) |
| 60 { | 60 { |
| 61 function onLoaded(entry) | 61 function onLoaded(entry) |
| 62 { | 62 { |
| 63 for (let line of entry.content) | 63 if ("content" in entry) |
| 64 listener.process(line); | 64 { |
| 65 | 65 if (entry["compressed"]) |
| 66 listener.process(null); | 66 entry.content = JSON.parse(LZString.decompressFromUTF16(entry.content)
); |
| 67 for (let line of entry.content) |
| 68 listener.process(line); |
| 69 } |
| 67 callback(null); | 70 callback(null); |
| 68 } | 71 } |
| 69 | 72 |
| 70 loadFile(file, onLoaded, callback); | 73 loadFile(file).then(onLoaded, callback); |
| 71 }, | 74 }, |
| 72 | 75 |
| 73 writeToFile: function(file, data, callback) | 76 writeToFile: function(file, data, callback) |
| 74 { | 77 { |
| 75 saveFile(file, data, callback); | 78 let entry = {}; |
| 76 }, | 79 let key = fileToKey(file); |
| 77 | 80 |
| 78 copyFile: function(fromFile, toFile, callback) | 81 if (typeof browser == "undefined") |
| 79 { | |
| 80 function onLoaded(entry) | |
| 81 { | 82 { |
| 82 saveFile(toFile, entry.content, callback); | 83 entry[key] = {lastModified: Date.now(), content: data}; |
| 84 ext.storage.set(entry, callback); |
| 83 } | 85 } |
| 84 | 86 else |
| 85 loadFile(fromFile, onLoaded, callback); | |
| 86 }, | |
| 87 | |
| 88 renameFile: function(fromFile, newName, callback) | |
| 89 { | |
| 90 function onLoaded() | |
| 91 { | 87 { |
| 92 ext.storage.remove(fileToKey(fromFile), function() | 88 // Edge cannot write files larger than 1Mb (500k string in UTF-16) via |
| 93 { | 89 // storage API. However it does not always raise an exception when trying |
| 94 ext.storage.set(keyPrefix + newName, entry, callback); | 90 // to do so. The solution is to compress and fallback to localStorage. |
| 95 }); | 91 let processedData = LZString.compressToUTF16(JSON.stringify(data)); |
| 92 ext.storage.remove(key); |
| 93 entry[key] = {lastModified: Date.now(), content: processedData, |
| 94 compressed: true}; |
| 95 window.localStorage.setItem(key, JSON.stringify(entry[key])); |
| 96 callback(); |
| 96 } | 97 } |
| 97 | |
| 98 loadFile(fromFile, onLoaded, callback); | |
| 99 }, | |
| 100 | |
| 101 removeFile: function(file, callback) | |
| 102 { | |
| 103 ext.storage.remove(fileToKey(file), callback); | |
| 104 }, | 98 }, |
| 105 | 99 |
| 106 statFile: function(file, callback) | 100 statFile: function(file, callback) |
| 107 { | 101 { |
| 108 function onLoaded(entry) | 102 function onLoaded(entry) |
| 109 { | 103 { |
| 110 callback(null, { | 104 callback(null, { |
| 111 exists: true, | 105 exists: true, |
| 112 lastModified: entry.lastModified | 106 lastModified: entry.lastModified |
| 113 }); | 107 }); |
| 114 } | 108 } |
| 115 | 109 |
| 116 loadFile(file, onLoaded, callback); | 110 loadFile(file).then(onLoaded, callback); |
| 117 } | 111 } |
| 118 }; | 112 }; |
| OLD | NEW |