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 == null) |
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 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
126 * Name of the file to be renamed | 116 * Name of the file to be renamed |
127 * @param {string} newName | 117 * @param {string} newName |
128 * New file name, will be overwritten if exists | 118 * New file name, will be overwritten if exists |
129 * @return {Promise} | 119 * @return {Promise} |
130 * Promise to be resolved or rejected once the operation is completed | 120 * Promise to be resolved or rejected once the operation is completed |
131 */ | 121 */ |
132 renameFile(fromFile, newName) | 122 renameFile(fromFile, newName) |
133 { | 123 { |
134 return loadFile(fromFile).then(entry => | 124 return loadFile(fromFile).then(entry => |
135 { | 125 { |
136 return new Promise((resolve, reject) => | 126 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)); | 127 }).then(() => removeFile(fromFile)); |
147 }, | 128 }, |
148 | 129 |
149 /** | 130 /** |
150 * Removes a file. | 131 * Removes a file. |
151 * @param {string} fileName | 132 * @param {string} fileName |
152 * Name of the file to be removed | 133 * Name of the file to be removed |
153 * @return {Promise} | 134 * @return {Promise} |
154 * Promise to be resolved or rejected once the operation is completed | 135 * Promise to be resolved or rejected once the operation is completed |
155 */ | 136 */ |
(...skipping 19 matching lines...) Expand all Loading... |
175 lastModified: entry.lastModified | 156 lastModified: entry.lastModified |
176 }; | 157 }; |
177 }).catch(error => | 158 }).catch(error => |
178 { | 159 { |
179 if (error.type == "NoSuchFile") | 160 if (error.type == "NoSuchFile") |
180 return {exists: false}; | 161 return {exists: false}; |
181 throw error; | 162 throw error; |
182 }); | 163 }); |
183 } | 164 } |
184 }; | 165 }; |
OLD | NEW |