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

Unified Diff: lib/io.js

Issue 10296001: Implement File API (Closed)
Patch Set: Addressed the new issues Created April 16, 2013, 1:37 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « lib/compat.js ('k') | libadblockplus.gyp » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/io.js
===================================================================
--- a/lib/io.js
+++ b/lib/io.js
@@ -4,26 +4,6 @@
var IO = exports.IO =
{
- _getFileEntry: function(file, create, successCallback, errorCallback)
- {
- if (file instanceof FakeFile)
- file = file.path;
- else if ("spec" in file)
- file = file.spec;
-
- // Remove directory path - we operate on a single directory in Chrome
- file = file.replace(/^.*[\/\\]/, "");
-
- // We request a gigabyte of space, just in case
- (window.requestFileSystem || window.webkitRequestFileSystem)(window.PERSISTENT, 1024*1024*1024, function(fs)
- {
- fs.root.getFile(file, {create: create}, function(fileEntry)
- {
- successCallback(fs, fileEntry);
- }, errorCallback);
- }, errorCallback);
- },
-
lineBreak: "\n",
resolveFilePath: function(path)
@@ -44,69 +24,25 @@
return;
}
- this._getFileEntry(file, false, function(fs, fileEntry)
+ _fileSystem.read(file, function(result)
{
- fileEntry.file(function(file)
+ if (result.error)
+ callback(result.error);
+ else
{
- var reader = new FileReader();
- reader.onloadend = function()
- {
- if (reader.error)
- callback(reader.error);
- else
- {
- var lines = reader.result.split(/[\r\n]+/);
- for (var i = 0; i < lines.length; i++)
- listener.process(lines[i]);
- listener.process(null);
- callback(null);
- }
- };
- reader.readAsText(file);
- }, callback);
- }, callback);
+ var lines = result.data.split(/[\r\n]+/);
+ for (var i = 0; i < lines.length; i++)
+ listener.process(lines[i]);
+ listener.process(null);
+ callback(null);
+ }
+ });
},
writeToFile: function(file, encode, data, callback, timeLineID)
{
- this._getFileEntry(file, true, function(fs, fileEntry)
- {
- fileEntry.createWriter(function(writer)
- {
- var executeWriteOperation = function(op, nextOperation)
- {
- writer.onwriteend = function()
- {
- if (writer.error)
- callback(writer.error);
- else
- nextOperation();
- }.bind(this);
-
- op();
- }.bind(this);
-
- executeWriteOperation(writer.truncate.bind(writer, 0), function()
- {
- var blob;
- try
- {
- blob = new Blob([data.join(this.lineBreak) + this.lineBreak], {type: "text/plain"});
- }
- catch (e)
- {
- if (!(e instanceof TypeError))
- throw e;
-
- // Blob wasn't a constructor before Chrome 20
- var builder = new (window.BlobBuilder || window.WebKitBlobBuilder);
- builder.append(data.join(this.lineBreak) + this.lineBreak);
- blob = builder.getBlob("text/plain");
- }
- executeWriteOperation(writer.write.bind(writer, blob), callback.bind(null, null));
- }.bind(this));
- }.bind(this), callback);
- }.bind(this), callback);
+ var content = data.join(this.lineBreak) + this.lineBreak;
+ _fileSystem.write(file, content, callback);
},
copyFile: function(fromFile, toFile, callback)
@@ -130,39 +66,16 @@
renameFile: function(fromFile, newName, callback)
{
- this._getFileEntry(fromFile, false, function(fs, fileEntry)
- {
- fileEntry.moveTo(fs.root, newName, function()
- {
- callback(null);
- }, callback);
- }, callback);
+ _fileSystem.move(fromFile, newName, callback);
},
removeFile: function(file, callback)
{
- this._getFileEntry(file, false, function(fs, fileEntry)
- {
- fileEntry.remove(function()
- {
- callback(null);
- }, callback);
- }, callback);
+ _fileSystem.remove(file, callback);
},
statFile: function(file, callback)
{
- this._getFileEntry(file, false, function(fs, fileEntry)
- {
- fileEntry.getMetadata(function(metadata)
- {
- callback(null, {
- exists: true,
- isDirectory: fileEntry.isDirectory,
- isFile: fileEntry.isFile,
- lastModified: metadata.modificationTime.getTime()
- });
- }, callback);
- }, callback);
+ _fileSystem.stat(file, callback);
}
};
« 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