Left: | ||
Right: |
OLD | NEW |
---|---|
(Empty) | |
1 /* | |
2 * This file is part of Adblock Plus <https://adblockplus.org/>, | |
3 * Copyright (C) 2006-2017 eyeo GmbH | |
4 * | |
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 | |
7 * published by the Free Software Foundation. | |
8 * | |
9 * Adblock Plus is distributed in the hope that it will be useful, | |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 * GNU General Public License for more details. | |
13 * | |
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/>. | |
16 */ | |
17 | |
18 "use strict"; | |
Wladimir Palant
2017/04/10 11:35:17
Note that the code here is based on io.js from adb
| |
19 | |
20 (function(exports) | |
21 { | |
22 const keyPrefix = "file:"; | |
23 | |
24 function fileToKey(fileName) | |
25 { | |
26 return keyPrefix + fileName; | |
27 } | |
28 | |
29 function loadFile(file) | |
30 { | |
31 let key = fileToKey(file); | |
32 | |
33 return browser.storage.local.get(key).then(items => | |
34 { | |
35 if (items.hasOwnProperty(key)) | |
36 return items[key]; | |
37 | |
38 throw "NoSuchFile"; | |
39 }); | |
40 } | |
41 | |
42 function saveFile(file, data) | |
43 { | |
44 return browser.storage.local.set({ | |
45 [fileToKey(file)]: { | |
46 content: Array.from(data), | |
47 lastModified: Date.now() | |
48 } | |
49 }); | |
50 } | |
51 | |
52 function removeFile(file) | |
53 { | |
54 return browser.storage.local.remove(fileToKey(file)); | |
55 } | |
56 | |
57 exports.IO = | |
58 { | |
59 readFromFile(file) | |
60 { | |
61 return loadFile(file).then(entry => | |
62 { | |
63 return entry.content; | |
64 }); | |
65 }, | |
66 | |
67 writeToFile(file, data) | |
68 { | |
69 return saveFile(file, data); | |
70 }, | |
71 | |
72 copyFile(fromFile, toFile) | |
73 { | |
74 return loadFile(fromFile).then(entry => | |
75 { | |
76 return saveFile(toFile, entry.content); | |
77 }); | |
78 }, | |
79 | |
80 renameFile(fromFile, newName) | |
81 { | |
82 return loadFile(fromFile).then(entry => | |
83 { | |
84 return browser.storage.local.set({ | |
85 [fileToKey(newName)]: entry | |
86 }); | |
87 }).then(() => | |
88 { | |
89 return removeFile(fromFile); | |
90 }); | |
91 }, | |
92 | |
93 removeFile(file) | |
94 { | |
95 return removeFile(file); | |
96 }, | |
97 | |
98 statFile(file) | |
99 { | |
100 return loadFile(file).then(entry => | |
101 { | |
102 return { | |
103 exists: true, | |
104 lastModified: entry.lastModified | |
105 }; | |
106 }); | |
Wladimir Palant
2017/04/10 11:35:17
Note that statFile would normally catch NoSuchFile
| |
107 } | |
108 }; | |
109 })(this); | |
OLD | NEW |