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

Side by Side Diff: lib/io.js

Issue 10296001: Implement File API (Closed)
Patch Set: Addressed the new issues Created April 16, 2013, 1:37 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
« no previous file with comments | « lib/compat.js ('k') | libadblockplus.gyp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // 1 //
2 // No direct file system access, using FileSystem API 2 // No direct file system access, using FileSystem API
3 // 3 //
4 4
5 var IO = exports.IO = 5 var IO = exports.IO =
6 { 6 {
7 _getFileEntry: function(file, create, successCallback, errorCallback)
8 {
9 if (file instanceof FakeFile)
10 file = file.path;
11 else if ("spec" in file)
12 file = file.spec;
13
14 // Remove directory path - we operate on a single directory in Chrome
15 file = file.replace(/^.*[\/\\]/, "");
16
17 // We request a gigabyte of space, just in case
18 (window.requestFileSystem || window.webkitRequestFileSystem)(window.PERSISTE NT, 1024*1024*1024, function(fs)
19 {
20 fs.root.getFile(file, {create: create}, function(fileEntry)
21 {
22 successCallback(fs, fileEntry);
23 }, errorCallback);
24 }, errorCallback);
25 },
26
27 lineBreak: "\n", 7 lineBreak: "\n",
28 8
29 resolveFilePath: function(path) 9 resolveFilePath: function(path)
30 { 10 {
31 return new FakeFile(path); 11 return new FakeFile(path);
32 }, 12 },
33 13
34 readFromFile: function(file, decode, listener, callback, timeLineID) 14 readFromFile: function(file, decode, listener, callback, timeLineID)
35 { 15 {
36 if ("spec" in file && /^defaults\b/.test(file.spec)) 16 if ("spec" in file && /^defaults\b/.test(file.spec))
37 { 17 {
38 // Code attempts to read the default patterns.ini, we don't have that. 18 // Code attempts to read the default patterns.ini, we don't have that.
39 // Make sure to execute first-run actions instead. 19 // Make sure to execute first-run actions instead.
40 callback(null); 20 callback(null);
41 if (localStorage.currentVersion) 21 if (localStorage.currentVersion)
42 seenDataCorruption = true; 22 seenDataCorruption = true;
43 delete localStorage.currentVersion; 23 delete localStorage.currentVersion;
44 return; 24 return;
45 } 25 }
46 26
47 this._getFileEntry(file, false, function(fs, fileEntry) 27 _fileSystem.read(file, function(result)
48 { 28 {
49 fileEntry.file(function(file) 29 if (result.error)
30 callback(result.error);
31 else
50 { 32 {
51 var reader = new FileReader(); 33 var lines = result.data.split(/[\r\n]+/);
52 reader.onloadend = function() 34 for (var i = 0; i < lines.length; i++)
53 { 35 listener.process(lines[i]);
54 if (reader.error) 36 listener.process(null);
55 callback(reader.error); 37 callback(null);
56 else 38 }
57 { 39 });
58 var lines = reader.result.split(/[\r\n]+/);
59 for (var i = 0; i < lines.length; i++)
60 listener.process(lines[i]);
61 listener.process(null);
62 callback(null);
63 }
64 };
65 reader.readAsText(file);
66 }, callback);
67 }, callback);
68 }, 40 },
69 41
70 writeToFile: function(file, encode, data, callback, timeLineID) 42 writeToFile: function(file, encode, data, callback, timeLineID)
71 { 43 {
72 this._getFileEntry(file, true, function(fs, fileEntry) 44 var content = data.join(this.lineBreak) + this.lineBreak;
73 { 45 _fileSystem.write(file, content, callback);
74 fileEntry.createWriter(function(writer)
75 {
76 var executeWriteOperation = function(op, nextOperation)
77 {
78 writer.onwriteend = function()
79 {
80 if (writer.error)
81 callback(writer.error);
82 else
83 nextOperation();
84 }.bind(this);
85
86 op();
87 }.bind(this);
88
89 executeWriteOperation(writer.truncate.bind(writer, 0), function()
90 {
91 var blob;
92 try
93 {
94 blob = new Blob([data.join(this.lineBreak) + this.lineBreak], {type: "text/plain"});
95 }
96 catch (e)
97 {
98 if (!(e instanceof TypeError))
99 throw e;
100
101 // Blob wasn't a constructor before Chrome 20
102 var builder = new (window.BlobBuilder || window.WebKitBlobBuilder);
103 builder.append(data.join(this.lineBreak) + this.lineBreak);
104 blob = builder.getBlob("text/plain");
105 }
106 executeWriteOperation(writer.write.bind(writer, blob), callback.bind(n ull, null));
107 }.bind(this));
108 }.bind(this), callback);
109 }.bind(this), callback);
110 }, 46 },
111 47
112 copyFile: function(fromFile, toFile, callback) 48 copyFile: function(fromFile, toFile, callback)
113 { 49 {
114 // Simply combine read and write operations 50 // Simply combine read and write operations
115 var data = []; 51 var data = [];
116 this.readFromFile(fromFile, false, { 52 this.readFromFile(fromFile, false, {
117 process: function(line) 53 process: function(line)
118 { 54 {
119 if (line !== null) 55 if (line !== null)
120 data.push(line); 56 data.push(line);
121 } 57 }
122 }, function(e) 58 }, function(e)
123 { 59 {
124 if (e) 60 if (e)
125 callback(e); 61 callback(e);
126 else 62 else
127 this.writeToFile(toFile, false, data, callback); 63 this.writeToFile(toFile, false, data, callback);
128 }.bind(this)); 64 }.bind(this));
129 }, 65 },
130 66
131 renameFile: function(fromFile, newName, callback) 67 renameFile: function(fromFile, newName, callback)
132 { 68 {
133 this._getFileEntry(fromFile, false, function(fs, fileEntry) 69 _fileSystem.move(fromFile, newName, callback);
134 {
135 fileEntry.moveTo(fs.root, newName, function()
136 {
137 callback(null);
138 }, callback);
139 }, callback);
140 }, 70 },
141 71
142 removeFile: function(file, callback) 72 removeFile: function(file, callback)
143 { 73 {
144 this._getFileEntry(file, false, function(fs, fileEntry) 74 _fileSystem.remove(file, callback);
145 {
146 fileEntry.remove(function()
147 {
148 callback(null);
149 }, callback);
150 }, callback);
151 }, 75 },
152 76
153 statFile: function(file, callback) 77 statFile: function(file, callback)
154 { 78 {
155 this._getFileEntry(file, false, function(fs, fileEntry) 79 _fileSystem.stat(file, callback);
156 {
157 fileEntry.getMetadata(function(metadata)
158 {
159 callback(null, {
160 exists: true,
161 isDirectory: fileEntry.isDirectory,
162 isFile: fileEntry.isFile,
163 lastModified: metadata.modificationTime.getTime()
164 });
165 }, callback);
166 }, callback);
167 } 80 }
168 }; 81 };
OLDNEW
« no previous file with comments | « lib/compat.js ('k') | libadblockplus.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld