Skip to content

Instantly share code, notes, and snippets.

@jsmaker
Created June 28, 2012 08:51
Show Gist options
  • Save jsmaker/3009985 to your computer and use it in GitHub Desktop.
Save jsmaker/3009985 to your computer and use it in GitHub Desktop.
Just playing with the fileSystem Api
;var Fs = (function () {
'use strict';
function extend(obj) {
Array.prototype.slice.call(arguments, 1).forEach(function (source) {
var prop;
for (prop in source) {
if (source[prop] !== void 0) {
obj[prop] = source[prop];
}
}
});
return obj;
}
window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem;
var Fs = {
fs : null,
lastFileEntry : null,
size : 5,
testBlockTime : 20,
started : false,
availble : true,
type : window.TEMPORARY,
init : function (type, size, ready) {
this.size = size || this.size;
this.type = type || this.type;
this.started = true;
this.requstFs(ready);
return this;
},
run : function (opt) {
if (!this.started) {
throw new Error('you must run the Fs.init to to use it');
}
var cb, done = opt.done;
if (this.fs && this.availble && opt.action && this[opt.action]) {
if (opt.block) {
this.availble = false;
cb = function () {
this.availble = true;
done.apply(this, arguments);
};
} else {
cb = done;
}
delete opt.done;
this[opt.action].call(this, opt, cb);
} else if (opt.action && this[opt.action]) {
setTimeout(function () {
Fs.run(opt);
}, this.testBlockTime);
} else {
throw new Error('you must run the Fs.init to to use it');
}
return this;
},
requstFs : function (ready) {
window.requestFileSystem(this.type, this.fromMB(this.size), function (fs) {
Fs.fs = fs;
Fs.applyCallback(ready, [fs])
}, this.onErrorFs);
},
requstQuota : function (size, done) {
var Fs = this;
window.webkitStorageInfo.requestQuota(this.type, this.fromMB(this.size), function (grantedBytes) {
Fs.size = (Fs.toMB(grantedBytes));
Fs.requstFs(done);
}, this.onErrorFs);
},
requstUsage : function (done) {
var Fs = this;
window.webkitStorageInfo.queryUsageAndQuota(this.type, function (usage, quota) {
this.usage_quota = {
usage : Fs.toMB(usage),
quota : Fs.toMB(quota)
};
if (typeof done === 'function') {
done.call(Fs, this.usage_quota);
}
}, this.onErrorFs);
},
toMB : function (bytes) {
return bytes / 1024 / 1024;
},
fromMB : function (mb) {
return mb * 1024 * 1024;
},
getFile : function (opt, cb) {
var done = function (fileEntry) {
Fs.lastFileEntry = fileEntry;
Fs.applyCallback(cb, arguments);
};
opt.opt = opt.opt || {
create : true /* , exclusive: true */
};
this.fs.root.getFile(opt.fileName, opt.opt, done, this.onErrorFs);
},
getDir : function (opt, cb) {
var done = function (dirEntry) {
Fs.lastDirEntry = dirEntry;
Fs.applyCallback(cb, arguments);
};
opt.opt = opt.opt || {create : true}; /* , exclusive: true */
console.log(opt.dirName);
this.fs.root.getDirectory(opt.dirName, opt.opt, done, this.onErrorFs);
},
createSubDir : function (opt, done) {
opt.allFolders = opt.allFolders || [];
var rootDirEntry = opt.rootDirEntry;
var folders = opt.folders;
if (typeof rootDirEntry === 'string') {
//TODO: get the folder if its a string
}
if (typeof folders === 'string') {
folders = folders.split('/');
}
if (folders[0] == '.' || folders[0] == '') {
folders = folders.slice(1);
}
rootDirEntry.getDirectory(folders[0], opt.opt || {create: true}, function(dirEntry) {
if (folders.length) {
opt.allFolders.push(dirEntry);
opt.rootDirEntry = dirEntry
opt.folders = folders.slice(1);
Fs.createSubDir(opt, done);
}else{
Fs.applyCallback(done, [opt.allFolders]);
}
}, this.onErrorFs);
},
applyCallback : function (cb, args) {
if (typeof cb === 'function') {
cb.apply(Fs, args);
}
},
readFile : function (opt, done) {
this.getFile(opt, function (fileEntry) {
fileEntry.file(function (file) {
var reader = new FileReader();
reader.onloadend = function (e) {
Fs.applyCallback(done, [this, this.result]);
};
if (opt.as === 'text') {
reader.readAsText(file);
}
}, this.onErrorFs);
});
},
writeFile : function (opt, done) {
this.getFile(opt, function (fileEntry) {
fileEntry.createWriter(function (fileWriter) {
fileWriter.onwriteend = function (e) {
Fs.applyCallback(done, [this, fileEntry]);
};
fileWriter.onerror = Fs.onErrorFs;
if (opt.append) {
// Start write position at EOF.
fileWriter.seek(fileWriter.length);
}
// Create a new Blob and write it to log.txt.
// Note: window.WebKitBlobBuilder in Chrome 12.
var bb = new WebKitBlobBuilder();
bb.append(opt.data);
fileWriter.write(bb.getBlob('text/plain'));
//console.log('writeing');
}, this.onErrorFs);
});
},
removeFile : function (opt, done) {
if (opt.last && this.lastFileEntry) {
var lastFileEntry = this.lastFileEntry;
lastFileEntry.remove(function () {
Fs.applyCallback(done, [lastFileEntry]);
//console.log('File removed.', Fs.lastFileEntry);
}, this.onErrorFs);
} else {
console.log('TODO:delete');
}
},
onFileEntry : function (fileEntry) {
Fs.lastFileEntry = fileEntry;
},
onErrorFs : function (e) {
console.warn('Error: ', e);
// var msg = '';
// switch (e.code) {
// case FileError.QUOTA_EXCEEDED_ERR:
// msg = 'QUOTA_EXCEEDED_ERR';
// break;
// case FileError.NOT_FOUND_ERR:
// msg = 'NOT_FOUND_ERR';
// break;
// case FileError.SECURITY_ERR:
// msg = 'SECURITY_ERR';
// break;
// case FileError.INVALID_MODIFICATION_ERR:
// msg = 'INVALID_MODIFICATION_ERR';
// break;
// case FileError.INVALID_STATE_ERR:
// msg = 'INVALID_STATE_ERR';
// break;
// default:
// msg = 'Unknown Error';
// console.log(e);
// break;
// };
// console.warn('Error: ' + msg);
}
};
return {
init : function (type, size, ready) {
Fs.init.apply(Fs, arguments);
},
run : function (opt) {
Fs.run.apply(Fs, arguments);
}
};
}());
Fs.init(window.TEMPORARY, 1, function (fs) {
console.log('r');
this.requstUsage(function (uq) {
console.log(uq);
});
});
Fs.run({
action:'getDir',
block:true,
dirName : 'banana',
done: function(res) {
Fs.run({
action:'createSubDir',
block:true,
rootDirEntry : res,
folders:'nana/baba/caca',
done: function(res) {
console.log(this,res,'2345234523452');
}
});
}
});
Fs.run({
action : 'writeFile',
fileName : 'banana/log3.txt',
data : 'SHHHHHHAAAAAAAZZZZZZZZZAAAAAAAMMMMMMM2\n',
append : true,
block : true,
done : function (w, res) {
var a = document.createElement('a');
a.innerHTML = 'File: ' + res.name;
document.body.appendChild(a);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment