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-present eyeo GmbH | 3 * Copyright (C) 2006-present 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 |
(...skipping 10 matching lines...) Expand all Loading... |
21 | 21 |
22 function fileToKey(fileName) | 22 function fileToKey(fileName) |
23 { | 23 { |
24 return keyPrefix + fileName; | 24 return keyPrefix + fileName; |
25 } | 25 } |
26 | 26 |
27 function loadFile(fileName) | 27 function loadFile(fileName) |
28 { | 28 { |
29 return new Promise((resolve, reject) => | 29 return new Promise((resolve, reject) => |
30 { | 30 { |
31 let key = fileToKey(fileName); | 31 localforage.getItem(fileToKey(fileName), (err, value) => |
32 | |
33 ext.storage.get(key, items => | |
34 { | 32 { |
35 let entry = items[key]; | 33 if (err || !value) |
36 | 34 reject({type: "NoSuchFile"}); |
37 if (entry) | |
38 resolve(entry); | |
39 else | 35 else |
40 reject({type: "NoSuchFile"}); | 36 resolve(value); |
41 }); | 37 }); |
42 }); | 38 }); |
43 } | 39 } |
44 | 40 |
45 function saveFile(fileName, data) | 41 function saveFile(fileName, data) |
46 { | 42 { |
47 return new Promise((resolve, reject) => | 43 let key = fileToKey(fileName); |
48 { | 44 return localforage.setItem(key, { |
49 ext.storage.set( | 45 content: Array.from(data), |
50 fileToKey(fileName), | 46 lastModified: Date.now() |
51 { | |
52 content: Array.from(data), | |
53 lastModified: Date.now() | |
54 }, | |
55 resolve | |
56 ); | |
57 }); | 47 }); |
58 } | 48 } |
59 | 49 |
60 function removeFile(fileName) | 50 function removeFile(fileName) |
61 { | 51 { |
62 return new Promise((resolve, reject) => | 52 return new Promise((resolve, reject) => |
63 { | 53 { |
64 ext.storage.remove(fileToKey(fileName), () => | 54 ext.storage.remove(fileToKey(fileName), () => |
65 { | 55 { |
66 if (chrome.runtime.lastError) | 56 if (chrome.runtime.lastError) |
(...skipping 12 matching lines...) Expand all Loading... |
79 * Name of the file to be read | 69 * Name of the file to be read |
80 * @param {TextSink} listener | 70 * @param {TextSink} listener |
81 * Function that will be called for each line in the file | 71 * Function that will be called for each line in the file |
82 * @return {Promise} | 72 * @return {Promise} |
83 * Promise to be resolved or rejected once the operation is completed | 73 * Promise to be resolved or rejected once the operation is completed |
84 */ | 74 */ |
85 readFromFile(fileName, listener) | 75 readFromFile(fileName, listener) |
86 { | 76 { |
87 return loadFile(fileName).then(entry => | 77 return loadFile(fileName).then(entry => |
88 { | 78 { |
89 for (let line of entry.content) | 79 if ("content" in entry) |
90 listener(line); | 80 { |
| 81 for (let line of entry.content) |
| 82 listener(line); |
| 83 } |
91 }); | 84 }); |
92 }, | 85 }, |
93 | 86 |
94 /** | 87 /** |
95 * Writes text lines to a file. | 88 * Writes text lines to a file. |
96 * @param {string} fileName | 89 * @param {string} fileName |
97 * Name of the file to be written | 90 * Name of the file to be written |
98 * @param {Iterable.<string>} data | 91 * @param {Iterable.<string>} data |
99 * An array-like or iterable object containing the lines (without line | 92 * An array-like or iterable object containing the lines (without line |
100 * endings) | 93 * endings) |
(...skipping 25 matching lines...) Expand all Loading... |
126 * Name of the file to be renamed | 119 * Name of the file to be renamed |
127 * @param {string} newName | 120 * @param {string} newName |
128 * New file name, will be overwritten if exists | 121 * New file name, will be overwritten if exists |
129 * @return {Promise} | 122 * @return {Promise} |
130 * Promise to be resolved or rejected once the operation is completed | 123 * Promise to be resolved or rejected once the operation is completed |
131 */ | 124 */ |
132 renameFile(fromFile, newName) | 125 renameFile(fromFile, newName) |
133 { | 126 { |
134 return loadFile(fromFile).then(entry => | 127 return loadFile(fromFile).then(entry => |
135 { | 128 { |
136 return new Promise((resolve, reject) => | 129 return saveFile(fileToKey(newName), entry); |
137 { | |
138 ext.storage.set(fileToKey(newName), entry, () => | |
139 { | |
140 if (chrome.runtime.lastError) | |
141 reject(chrome.runtime.lastError.message); | |
142 else | |
143 resolve(); | |
144 }); | |
145 }); | |
146 }).then(() => removeFile(fromFile)); | 130 }).then(() => removeFile(fromFile)); |
147 }, | 131 }, |
148 | 132 |
149 /** | 133 /** |
150 * Removes a file. | 134 * Removes a file. |
151 * @param {string} fileName | 135 * @param {string} fileName |
152 * Name of the file to be removed | 136 * Name of the file to be removed |
153 * @return {Promise} | 137 * @return {Promise} |
154 * Promise to be resolved or rejected once the operation is completed | 138 * Promise to be resolved or rejected once the operation is completed |
155 */ | 139 */ |
(...skipping 19 matching lines...) Expand all Loading... |
175 lastModified: entry.lastModified | 159 lastModified: entry.lastModified |
176 }; | 160 }; |
177 }).catch(error => | 161 }).catch(error => |
178 { | 162 { |
179 if (error.type == "NoSuchFile") | 163 if (error.type == "NoSuchFile") |
180 return {exists: false}; | 164 return {exists: false}; |
181 throw error; | 165 throw error; |
182 }); | 166 }); |
183 } | 167 } |
184 }; | 168 }; |
OLD | NEW |