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-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 console.trace(); |
Sebastian Noack
2017/08/24 17:11:00
Did you forget to remove this?
Oleksandr
2017/08/24 17:17:04
Whoops. Yes, sorry. Some of the refreshes before c
| |
32 | 32 localforage.getItem(fileToKey(fileName), (err, value) => |
33 ext.storage.get(key, items => | |
34 { | 33 { |
35 let entry = items[key]; | 34 if (err || value == null) |
36 | 35 reject({type: "NoSuchFile"}); |
37 if (entry) | |
38 resolve(entry); | |
39 else | 36 else |
40 reject({type: "NoSuchFile"}); | 37 resolve(value); |
41 }); | 38 }); |
42 }); | 39 }); |
43 } | 40 } |
44 | 41 |
45 function saveFile(fileName, data) | 42 function saveFile(fileName, data) |
46 { | 43 { |
47 return new Promise((resolve, reject) => | 44 let key = fileToKey(fileName); |
48 { | 45 return localforage.setItem(key, { |
49 ext.storage.set( | 46 content: Array.from(data), |
50 fileToKey(fileName), | 47 lastModified: Date.now() |
51 { | |
52 content: Array.from(data), | |
53 lastModified: Date.now() | |
54 }, | |
55 resolve | |
56 ); | |
57 }); | 48 }); |
58 } | 49 } |
59 | 50 |
60 function removeFile(fileName) | 51 function removeFile(fileName) |
61 { | 52 { |
62 return new Promise((resolve, reject) => | 53 return new Promise((resolve, reject) => |
63 { | 54 { |
64 ext.storage.remove(fileToKey(fileName), () => | 55 ext.storage.remove(fileToKey(fileName), () => |
65 { | 56 { |
66 if (chrome.runtime.lastError) | 57 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 | 117 * Name of the file to be renamed |
127 * @param {string} newName | 118 * @param {string} newName |
128 * New file name, will be overwritten if exists | 119 * New file name, will be overwritten if exists |
129 * @return {Promise} | 120 * @return {Promise} |
130 * Promise to be resolved or rejected once the operation is completed | 121 * Promise to be resolved or rejected once the operation is completed |
131 */ | 122 */ |
132 renameFile(fromFile, newName) | 123 renameFile(fromFile, newName) |
133 { | 124 { |
134 return loadFile(fromFile).then(entry => | 125 return loadFile(fromFile).then(entry => |
135 { | 126 { |
136 return new Promise((resolve, reject) => | 127 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)); | 128 }).then(() => removeFile(fromFile)); |
147 }, | 129 }, |
148 | 130 |
149 /** | 131 /** |
150 * Removes a file. | 132 * Removes a file. |
151 * @param {string} fileName | 133 * @param {string} fileName |
152 * Name of the file to be removed | 134 * Name of the file to be removed |
153 * @return {Promise} | 135 * @return {Promise} |
154 * Promise to be resolved or rejected once the operation is completed | 136 * Promise to be resolved or rejected once the operation is completed |
155 */ | 137 */ |
(...skipping 19 matching lines...) Expand all Loading... | |
175 lastModified: entry.lastModified | 157 lastModified: entry.lastModified |
176 }; | 158 }; |
177 }).catch(error => | 159 }).catch(error => |
178 { | 160 { |
179 if (error.type == "NoSuchFile") | 161 if (error.type == "NoSuchFile") |
180 return {exists: false}; | 162 return {exists: false}; |
181 throw error; | 163 throw error; |
182 }); | 164 }); |
183 } | 165 } |
184 }; | 166 }; |
OLD | NEW |