Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: lib/io.js

Issue 29796555: Issue 6621 (Closed)
Patch Set: Created June 1, 2018, 1:58 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-present 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";
19
20 const keyPrefix = "file:";
21
22 function fileToKey(fileName)
23 {
24 return keyPrefix + fileName;
25 }
26
27 function loadFile(fileName)
28 {
29 let key = fileToKey(fileName);
30 return browser.storage.local.get(key).then(items =>
31 {
32 let entry = items[key];
33 if (entry)
34 return entry;
35 throw {type: "NoSuchFile"};
36 });
37 }
38
39 function saveFile(fileName, data)
40 {
41 return browser.storage.local.set({
42 [fileToKey(fileName)]: {
43 content: Array.from(data),
44 lastModified: Date.now()
45 }
46 });
47 }
48
49 exports.IO =
50 {
51 /**
52 * Reads text lines from a file.
53 * @param {string} fileName
54 * Name of the file to be read
55 * @param {TextSink} listener
56 * Function that will be called for each line in the file
57 * @return {Promise}
58 * Promise to be resolved or rejected once the operation is completed
59 */
60 readFromFile(fileName, listener)
61 {
62 return loadFile(fileName).then(entry =>
63 {
64 for (let line of entry.content)
65 listener(line);
66 });
67 },
68
69 /**
70 * Writes text lines to a file.
71 * @param {string} fileName
72 * Name of the file to be written
73 * @param {Iterable.<string>} data
74 * An array-like or iterable object containing the lines (without line
75 * endings)
76 * @return {Promise}
77 * Promise to be resolved or rejected once the operation is completed
78 */
79 writeToFile(fileName, data)
80 {
81 return saveFile(fileName, data);
82 },
83
84 /**
85 * Copies a file.
86 * @param {string} fromFile
87 * Name of the file to be copied
88 * @param {string} toFile
89 * Name of the file to be written, will be overwritten if exists
90 * @return {Promise}
91 * Promise to be resolved or rejected once the operation is completed
92 */
93 copyFile(fromFile, toFile)
94 {
95 return loadFile(fromFile).then(entry => saveFile(toFile, entry.content));
96 },
97
98 /**
99 * Renames a file.
100 * @param {string} fromFile
101 * Name of the file to be renamed
102 * @param {string} newName
103 * New file name, will be overwritten if exists
104 * @return {Promise}
105 * Promise to be resolved or rejected once the operation is completed
106 */
107 renameFile(fromFile, newName)
108 {
109 return loadFile(fromFile)
110 .then(entry => browser.storage.local.set({[fileToKey(newName)]: entry}))
111 .then(() => this.removeFile(fromFile));
112 },
113
114 /**
115 * Removes a file.
116 * @param {string} fileName
117 * Name of the file to be removed
118 * @return {Promise}
119 * Promise to be resolved or rejected once the operation is completed
120 */
121 removeFile(fileName)
122 {
123 return browser.storage.local.remove(fileToKey(fileName));
124 },
125
126 /**
127 * Retrieves file metadata.
128 * @param {string} fileName
129 * Name of the file to be looked up
130 * @return {Promise.<StatData>}
131 * Promise to be resolved with file metadata once the operation is
132 * completed
133 */
134 statFile(fileName)
135 {
136 return loadFile(fileName).then(entry =>
137 {
138 return {
139 exists: true,
140 lastModified: entry.lastModified
141 };
142 }).catch(error =>
143 {
144 if (error.type == "NoSuchFile")
145 return {exists: false};
146 throw error;
147 });
148 }
149 };
OLDNEW

Powered by Google App Engine
This is Rietveld