Left: | ||
Right: |
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 ("compressed" in entry) |
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 // 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.
| |
79 { | 82 // However it does not always raise an exception when trying to do so. |
80 function onLoaded(entry) | 83 if (typeof browser == "undefined") |
81 { | 84 { |
82 saveFile(toFile, entry.content, callback); | 85 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.
| |
86 content: data}; | |
87 ext.storage.set(entry, callback); | |
83 } | 88 } |
84 | 89 else |
85 loadFile(fromFile, onLoaded, callback); | |
86 }, | |
87 | |
88 renameFile: function(fromFile, newName, callback) | |
89 { | |
90 function onLoaded() | |
91 { | 90 { |
92 ext.storage.remove(fileToKey(fromFile), function() | 91 // Compress and fallback to localStorage |
93 { | 92 let processedData = LZString.compressToUTF16(JSON.stringify(data)); |
94 ext.storage.set(keyPrefix + newName, entry, callback); | 93 ext.storage.remove(key); |
95 }); | 94 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?
| |
95 content: processedData, | |
96 compressed: true}; | |
97 window.localStorage.setItem(key, JSON.stringify(entry[key])); | |
98 callback(); | |
96 } | 99 } |
97 | |
98 loadFile(fromFile, onLoaded, callback); | |
99 }, | |
100 | |
101 removeFile: function(file, callback) | |
102 { | |
103 ext.storage.remove(fileToKey(file), callback); | |
104 }, | 100 }, |
105 | 101 |
106 statFile: function(file, callback) | 102 statFile: function(file, callback) |
107 { | 103 { |
108 function onLoaded(entry) | 104 function onLoaded(entry) |
109 { | 105 { |
110 callback(null, { | 106 callback(null, { |
111 exists: true, | 107 exists: true, |
112 lastModified: entry.lastModified | 108 lastModified: entry.lastModified |
113 }); | 109 }); |
114 } | 110 } |
115 | 111 |
116 loadFile(file, onLoaded, callback); | 112 loadFile(file).then(onLoaded, callback); |
117 } | 113 } |
118 }; | 114 }; |
OLD | NEW |