-
-
Save panda4man/df9d63283aa3e3262b4e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Filesystem (checkDir, createDir, checkFile, creatFile, removeFile, writeFile, readeFile) | |
.factory('FileService', function($q) { | |
return { | |
checkDir: function (dir) { | |
var deferred = $q.defer(); | |
getFilesystem().then( | |
function(filesystem) { | |
filesystem.root.getDirectory(dir, {create: false}, | |
function() { | |
//Dir exist | |
deferred.resolve(); | |
}, | |
function() { | |
//Dir dont exist | |
deferred.reject(); | |
} | |
); | |
} | |
); | |
return deferred.promise; | |
}, | |
createDir: function (dir) { | |
getFilesystem().then( | |
function(filesystem) { | |
filesystem.root.getDirectory(dir, {create: true}); | |
} | |
); | |
}, | |
checkFile: function (dir, file) { | |
var deferred = $q.defer(); | |
getFilesystem().then( | |
function(filesystem) { | |
filesystem.root.getFile('/'+ dir +'/'+ file, {create: false}, | |
function() { | |
//File exist | |
deferred.resolve(); | |
}, | |
function() { | |
//File dont exist | |
deferred.reject(); | |
} | |
); | |
} | |
); | |
return deferred.promise; | |
}, | |
createFile: function (dir, file) { | |
getFilesystem().then( | |
function(filesystem) { | |
filesystem.root.getFile('/'+ dir +'/'+ file, {create: true}); | |
} | |
); | |
}, | |
removeFile: function (dir, file) { | |
var deferred = $q.defer(); | |
getFilesystem().then( | |
function(filesystem) { | |
filesystem.root.getFile('/'+ dir +'/'+ file, {create: false}, function(fileEntry){ | |
fileEntry.remove(function() { | |
deferred.resolve(); | |
}); | |
}); | |
} | |
); | |
return deferred.promise; | |
}, | |
writeFile: function (dir, file) { | |
var deferred = $q.defer(); | |
getFilesystem().then( | |
function(filesystem) { | |
filesystem.root.getFile('/'+ dir +'/'+ file, {create: false}, | |
function(fileEntry) { | |
fileEntry.createWriter(function(fileWriter) { | |
deferred.resolve(fileWriter); | |
}); | |
} | |
); | |
} | |
); | |
return deferred.promise; | |
}, | |
readeFile: function (dir, file) { | |
var deferred = $q.defer(); | |
getFilesystem().then( | |
function(filesystem) { | |
filesystem.root.getFile('/'+ dir +'/'+ file, {create: false}, | |
function(fileEntry) { | |
fileEntry.file(function(file) { | |
var reader = new FileReader(); | |
reader.onloadend = function() { | |
deferred.resolve(this.result); | |
}; | |
reader.readAsText(file); | |
}); | |
} | |
); | |
} | |
); | |
return deferred.promise; | |
} | |
}; | |
function getFilesystem() { | |
var deferred = $q.defer(); | |
window.requestFileSystem(window.PERSISTENT, 1024*1024, function(filesystem) { | |
deferred.resolve(filesystem); | |
}); | |
return deferred.promise; | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment