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-2017 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"; | 18 "use strict"; |
19 | 19 |
20 const keyPrefix = "file:"; | 20 const keyPrefix = "file:"; |
21 | 21 |
22 function fileToKey(file) | 22 function fileToKey(fileName) |
23 { | 23 { |
24 return keyPrefix + (file instanceof FakeFile ? file.path : file.spec); | 24 return keyPrefix + fileName; |
25 } | 25 } |
26 | 26 |
27 function loadFile(file, successCallback, errorCallback) | 27 function loadFile(fileName) |
28 { | 28 { |
29 let key = fileToKey(file); | 29 return new Promise((resolve, reject) => |
| 30 { |
| 31 let key = fileToKey(fileName); |
30 | 32 |
31 // Make sure we do not have subscriptions in localStorage from older | 33 ext.storage.get(key, items => |
32 // versions first | |
33 let entry = localStorage.getItem(key); | |
34 if (typeof entry == "string") | |
35 { | |
36 try | |
37 { | 34 { |
38 entry = JSON.parse(entry); | 35 let entry = items[key]; |
39 } | 36 |
40 catch(err) | 37 if (entry) |
41 { | 38 resolve(entry); |
42 setTimeout(errorCallback(new Error("File is corrupted"))); | 39 else |
43 return; | 40 reject({type: "NoSuchFile"}); |
44 } | 41 }); |
45 setTimeout(successCallback(entry)); | |
46 return; | |
47 } | |
48 // Now try to read from IndexedDB | |
49 localforage.getItem(key, function(err, value) | |
50 { | |
51 if (err || !value) | |
52 errorCallback(new Error("File doesn't exist")); | |
53 else | |
54 successCallback(value); | |
55 }); | 42 }); |
56 } | 43 } |
57 | 44 |
58 function saveFile(file, data, callback) | 45 function saveFile(fileName, data) |
59 { | 46 { |
60 var key = fileToKey(file); | 47 return new Promise((resolve, reject) => |
61 var entry = { | 48 { |
62 content: Array.from(data), | 49 ext.storage.set( |
63 lastModified: Date.now() | 50 fileToKey(fileName), |
64 }; | 51 { |
| 52 content: Array.from(data), |
| 53 lastModified: Date.now() |
| 54 }, |
| 55 resolve |
| 56 ); |
| 57 }); |
| 58 } |
65 | 59 |
66 localStorage.removeItem(key); | 60 function removeFile(fileName) |
67 localforage.setItem(key, entry, callback); | 61 { |
| 62 return new Promise((resolve, reject) => |
| 63 { |
| 64 ext.storage.remove(fileToKey(fileName), () => |
| 65 { |
| 66 if (chrome.runtime.lastError) |
| 67 reject(chrome.runtime.lastError.message); |
| 68 else |
| 69 resolve(); |
| 70 }); |
| 71 }); |
68 } | 72 } |
69 | 73 |
70 exports.IO = | 74 exports.IO = |
71 { | 75 { |
72 resolveFilePath(path) { return new FakeFile(path); }, | 76 /** |
73 | 77 * Reads text lines from a file. |
74 readFromFile(file, listener, callback) | 78 * @param {string} fileName |
| 79 * Name of the file to be read |
| 80 * @param {TextSink} listener |
| 81 * Function that will be called for each line in the file |
| 82 * @return {Promise} |
| 83 * Promise to be resolved or rejected once the operation is completed |
| 84 */ |
| 85 readFromFile(fileName, listener) |
75 { | 86 { |
76 function onLoaded(entry) | 87 return loadFile(fileName).then(entry => |
77 { | 88 { |
78 if ("content" in entry) | 89 for (let line of entry.content) |
79 { | 90 listener(line); |
80 for (let line of entry.content) | 91 }); |
81 listener.process(line); | |
82 } | |
83 listener.process(null); | |
84 callback(null); | |
85 } | |
86 | |
87 loadFile(file, onLoaded, callback); | |
88 }, | 92 }, |
89 | 93 |
90 writeToFile(file, data, callback) | 94 /** |
| 95 * Writes text lines to a file. |
| 96 * @param {string} fileName |
| 97 * Name of the file to be written |
| 98 * @param {Iterable.<string>} data |
| 99 * An array-like or iterable object containing the lines (without line |
| 100 * endings) |
| 101 * @return {Promise} |
| 102 * Promise to be resolved or rejected once the operation is completed |
| 103 */ |
| 104 writeToFile(fileName, data) |
91 { | 105 { |
92 saveFile(file, data, callback); | 106 return saveFile(fileName, data); |
93 }, | 107 }, |
94 | 108 |
95 copyFile(fromFile, toFile, callback) | 109 /** |
| 110 * Copies a file. |
| 111 * @param {string} fromFile |
| 112 * Name of the file to be copied |
| 113 * @param {string} toFile |
| 114 * Name of the file to be written, will be overwritten if exists |
| 115 * @return {Promise} |
| 116 * Promise to be resolved or rejected once the operation is completed |
| 117 */ |
| 118 copyFile(fromFile, toFile) |
96 { | 119 { |
97 function onLoaded(entry) | 120 return loadFile(fromFile).then(entry => saveFile(toFile, entry.content)); |
98 { | |
99 saveFile(toFile, entry.content, callback); | |
100 } | |
101 | |
102 loadFile(fromFile, onLoaded, callback); | |
103 }, | 121 }, |
104 | 122 |
105 renameFile(fromFile, newName, callback) | 123 /** |
| 124 * Renames a file. |
| 125 * @param {string} fromFile |
| 126 * Name of the file to be renamed |
| 127 * @param {string} newName |
| 128 * New file name, will be overwritten if exists |
| 129 * @return {Promise} |
| 130 * Promise to be resolved or rejected once the operation is completed |
| 131 */ |
| 132 renameFile(fromFile, newName) |
106 { | 133 { |
107 function onLoaded(entry) | 134 return loadFile(fromFile).then(entry => |
108 { | 135 { |
109 ext.storage.remove(fileToKey(fromFile), () => | 136 return new Promise((resolve, reject) => |
110 { | 137 { |
111 ext.storage.set(keyPrefix + newName, entry, callback); | 138 ext.storage.set(fileToKey(newName), entry, () => |
| 139 { |
| 140 if (chrome.runtime.lastError) |
| 141 reject(chrome.runtime.lastError.message); |
| 142 else |
| 143 resolve(); |
| 144 }); |
112 }); | 145 }); |
113 } | 146 }).then(() => removeFile(fromFile)); |
114 | |
115 loadFile(fromFile, onLoaded, callback); | |
116 }, | 147 }, |
117 | 148 |
118 removeFile(file, callback) | 149 /** |
| 150 * Removes a file. |
| 151 * @param {string} fileName |
| 152 * Name of the file to be removed |
| 153 * @return {Promise} |
| 154 * Promise to be resolved or rejected once the operation is completed |
| 155 */ |
| 156 removeFile(fileName) |
119 { | 157 { |
120 ext.storage.remove(fileToKey(file), callback); | 158 return removeFile(fileName); |
121 }, | 159 }, |
122 | 160 |
123 statFile(file, callback) | 161 /** |
| 162 * Retrieves file metadata. |
| 163 * @param {string} fileName |
| 164 * Name of the file to be looked up |
| 165 * @return {Promise.<StatData>} |
| 166 * Promise to be resolved with file metadata once the operation is |
| 167 * completed |
| 168 */ |
| 169 statFile(fileName) |
124 { | 170 { |
125 function onLoaded(entry) | 171 return loadFile(fileName).then(entry => |
126 { | 172 { |
127 callback(null, { | 173 return { |
128 exists: true, | 174 exists: true, |
129 lastModified: entry.lastModified | 175 lastModified: entry.lastModified |
130 }); | 176 }; |
131 } | 177 }).catch(error => |
132 | 178 { |
133 loadFile(file, onLoaded, callback); | 179 if (error.type == "NoSuchFile") |
| 180 return {exists: false}; |
| 181 throw error; |
| 182 }); |
134 } | 183 } |
135 }; | 184 }; |
OLD | NEW |