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: Created April 12, 2013, 10:10 a.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 blob;
45 try
73 { 46 {
74 fileEntry.createWriter(function(writer) 47 blob = new Blob([data.join(this.lineBreak) + this.lineBreak], {type: "text /plain"});
75 { 48 }
76 var executeWriteOperation = function(op, nextOperation) 49 catch (e)
77 { 50 {
78 writer.onwriteend = function() 51 if (!(e instanceof TypeError))
79 { 52 throw e;
80 if (writer.error)
81 callback(writer.error);
82 else
83 nextOperation();
84 }.bind(this);
85 53
86 op(); 54 // Blob wasn't a constructor before Chrome 20
87 }.bind(this); 55 var builder = new (window.BlobBuilder || window.WebKitBlobBuilder);
56 builder.append(data.join(this.lineBreak) + this.lineBreak);
57 blob = builder.getBlob("text/plain");
58 }
88 59
89 executeWriteOperation(writer.truncate.bind(writer, 0), function() 60 _fileSystem.write(file, blob, callback);
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 }, 61 },
111 62
112 copyFile: function(fromFile, toFile, callback) 63 copyFile: function(fromFile, toFile, callback)
113 { 64 {
114 // Simply combine read and write operations 65 // Simply combine read and write operations
115 var data = []; 66 var data = [];
116 this.readFromFile(fromFile, false, { 67 this.readFromFile(fromFile, false, {
117 process: function(line) 68 process: function(line)
118 { 69 {
119 if (line !== null) 70 if (line !== null)
120 data.push(line); 71 data.push(line);
121 } 72 }
122 }, function(e) 73 }, function(e)
123 { 74 {
124 if (e) 75 if (e)
125 callback(e); 76 callback(e);
126 else 77 else
127 this.writeToFile(toFile, false, data, callback); 78 this.writeToFile(toFile, false, data, callback);
128 }.bind(this)); 79 }.bind(this));
129 }, 80 },
130 81
131 renameFile: function(fromFile, newName, callback) 82 renameFile: function(fromFile, newName, callback)
132 { 83 {
133 this._getFileEntry(fromFile, false, function(fs, fileEntry) 84 _fileSystem.move(fromFile, newName, callback);
134 {
135 fileEntry.moveTo(fs.root, newName, function()
136 {
137 callback(null);
138 }, callback);
139 }, callback);
140 }, 85 },
141 86
142 removeFile: function(file, callback) 87 removeFile: function(file, callback)
143 { 88 {
144 this._getFileEntry(file, false, function(fs, fileEntry) 89 _fileSystem.remove(file, callback);
145 {
146 fileEntry.remove(function()
147 {
148 callback(null);
149 }, callback);
150 }, callback);
151 }, 90 },
152 91
153 statFile: function(file, callback) 92 statFile: function(file, callback)
154 { 93 {
155 this._getFileEntry(file, false, function(fs, fileEntry) 94 _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 } 95 }
168 }; 96 };
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