Skip to content

Instantly share code, notes, and snippets.

@ebrehault
Created May 12, 2014 09:58
Show Gist options
  • Save ebrehault/dcd397a3bd16b67c140a to your computer and use it in GitHub Desktop.
Save ebrehault/dcd397a3bd16b67c140a to your computer and use it in GitHub Desktop.
"use strict";
define(["module", "app"], function (module, app) {
app.lazy.service("localFS", [
"$q",
function ($q) {
var fstype = LocalFileSystem.PERSISTENT;
var path_prefix = "educlever/";
window.requestFileSystem(fstype, 0, function gotFS(fileSystem) {
var dataDir = fileSystem.root.getDirectory(path_prefix, {create: true});
}, function() {
throw "cannot-create-root-directory";
});
this.save = function(filename, content) {
var defered = $q.defer();
window.requestFileSystem(fstype, 0, function gotFS(fileSystem) {
fileSystem.root.getFile(
path_prefix + filename,
{create: true, exclusive: false},
function gotFileEntry(fileEntry) {
fileEntry.createWriter(function gotFileWriter(writer) {
writer.onwriteend = defered.resolve;
writer.write(content);
}, defered.reject);
}, defered.reject);
}, defered.reject);
return defered.promise;
};
this.read = function(filename) {
var defered = $q.defer();
window.requestFileSystem(fstype, 0, function gotFS(fileSystem) {
fileSystem.root.getFile(path_prefix + filename,
null, function gotFileEntry(fileEntry) {
fileEntry.file(function gotFile(file){
var reader = new FileReader();
reader.onloadend = function(evt) {
defered.resolve(evt.target.result);
};
reader.readAsText(file);
}, defered.reject);
}, defered.reject);
}, defered.reject);
return defered.promise;
};
this.remove = function(filename) {
var defered = $q.defer();
window.requestFileSystem(fstype, 0, function gotFS(fileSystem) {
fileSystem.root.getFile(path_prefix + filename,
null, function gotFileEntry(fileEntry) {
fileEntry.remove(defered.resolve, defered.reject);
}, defered.reject);
}, defered.reject);
return defered.promise;
};
}]);
return app;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment