-
-
Save StanShumsky/09cf90c71d589a648611 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/* Check the file content | |
//var filename = FileHandler.getFileName(); | |
// FileHandler.checkRateFile(); | |
// current use: | |
FileHandler.checkRateFile(function(counter) { | |
$scope.counter = counter; | |
console.log('$scope.counter: '+$scope.counter); | |
}); | |
*/ | |
var fileServices = angular.module('App.fileServices', []); | |
fileServices.value('aFileName', '.filename'); | |
fileServices.factory('FileHandler', [ | |
function() { | |
var cb; | |
var onError = function(err) { | |
console.error(err.name+": "+err.message); | |
} | |
var fileInfo = function(file) { | |
console.log(file.name+" - "+file.lastModifiedDate+" - "+file.size); | |
return file.size; | |
} | |
var writeFile = function(fileEntry, content) { | |
fileEntry.createWriter(function(writer) { | |
writer.onwriteend = function(ev) { | |
console.log('written '+content); | |
}; | |
writer.onerror = function(err) { | |
console.err(err.toString()); | |
}; | |
var blob = new Blob([content], {type: 'text/plain'}); | |
writer.write(blob); | |
}, onError); // EO createWriter | |
} | |
var readFile = function(fileEntry) { | |
var reader = new FileReader(); | |
fileEntry.file(function(file) { | |
reader.readAsText(file); | |
reader.onloadend = function(ev) { | |
console.log("file content: '"+ev.target.result+"'"); | |
cb(ev.target.result); | |
} | |
}); | |
} | |
var checkForFile = function(fileSystem) { | |
// fileSystem.root.getFile('.oncosur', {create: true}, gotFileEntry, onError); | |
fileSystem.root.getFile('.filename', {create: true, exclusive: false}, | |
function(fileEntry) { | |
handleFile(fileEntry); | |
}, onError | |
); | |
} | |
var handleFile = function(fileEntry) { | |
fileEntry.file(function(file) { | |
var size = fileInfo(file); | |
if (size == 0) | |
writeFile(fileEntry, '1'); | |
else | |
readFile(fileEntry); | |
}, onError); // EO fileEntry.file | |
} | |
var factory = {}; | |
factory.checkRateFile = function(fn) { | |
cb = fn; | |
var PERSISTENT, TEMPORARY; | |
if (typeof LocalFileSystem === 'undefined') { | |
PERSISTENT = window.PERSISTENT; | |
TEMPORARY = window.TEMPORARY; | |
} else { | |
PERSISTENT = LocalFileSystem.PERSISTENT ; | |
TEMPORARY = LocalFileSystem.TEMPORARY; | |
} | |
window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem; | |
if (window.webkitStorageInfo) { | |
window.webkitStorageInfo.requestQuota(PERSISTENT, 5*1024, function(grantSize){ | |
// navigator.webkitPersistentStorage.requestQuota(PERSISTENT, 5*1024, function(grantSize){ | |
window.requestFileSystem(PERSISTENT, grantSize, checkForFile, onError); | |
}, function(err) { | |
console.log(err.name+": "+err.message); | |
}) | |
} | |
else { | |
// window.requestFileSystem(PERSISTENT, 256, createFile, onError); | |
window.requestFileSystem(PERSISTENT, 256, checkForFile, onError); | |
} | |
}; | |
factory.getFileName = function() { | |
return '.filename'; | |
} | |
return factory; | |
} | |
]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment