Permalink
Browse files

Firefox addon: does not work, because FileSystem API is polyfilled vi…

…a IndexedDB, which is async and not supported in Web Workers (in Firefox, okay in Chrome). WebSQL has low quota limits, so not worth investigating. The JetPack SDK has a file/IO module? Not sure that would work either.
  • Loading branch information...
danielweck committed Mar 27, 2014
1 parent c89bd24 commit 5c88997a582332723e91abd448fec11bf0c3202c
View
@@ -4,6 +4,20 @@ module.exports = function(grunt) {
var config = {
// top-level task options, if needed.
shell: {
firefoxAddonSDK: {
options: {
stdout: true,
stderr: true,
execOptions: {
cwd: process.cwd()
}
},
command: function(sdkPath, buildPath) {
return 'cd "' + sdkPath + '" && ls -als ./ && source "bin/activate" && ls -als "' + buildPath + '" && cfx --verbose run --pkgdir="' + buildPath + '" -a firefox';
}
}
}
};
grunt.loadNpmTasks('grunt-express');
@@ -17,6 +31,7 @@ module.exports = function(grunt) {
grunt.loadNpmTasks('grunt-concurrent');
grunt.loadNpmTasks('grunt-git-describe');
grunt.loadNpmTasks('load-grunt-config');
grunt.loadNpmTasks('grunt-shell');
var path = require('path');
var configs = require('load-grunt-config')(grunt, {
@@ -28,7 +43,7 @@ module.exports = function(grunt) {
// console.log(JSON.stringify(subConfig));
// console.log('');
//grunt.util._.extend({}, configs)
grunt.util._.merge(config, configs);
View
@@ -0,0 +1 @@
https://github.com/readium/readium-js-viewer/
@@ -0,0 +1,15 @@
require.config({
config: {
'workers/WorkerProxy': {
'workerUrl': './scripts/readium-worker.js'
},
'EpubLibraryManager': {
'canHandleUrl': true,
'canHandleDirectory': false //TODO: see EpubLibraryManager.handleDirectoryImport, only File.mozFullPath is supported => no directory file chooser, no full-folder import :(
},
'EpubReader': {
'annotationCssUrl': self.location.origin + '/css/annotations.css'
}
}
});
@@ -0,0 +1,43 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="images/readium_favicon.png" rel="shortcut icon"/>
<!-- link href="images/readium-touch-icon.png" rel="apple-touch-icon"/ -->
<style>
.library-item{
height: 100px;
}
.library-item img{
height: 100%;
border-radius: 3px;
border: 1px solid rgba(0,0,0,.2);
}
.library-item .no-cover{
width: 300px;
height: 400px;
font-size: 40px;
cursor: pointer;
border-radius: 3px;
border: 1px solid rgba(0,0,0,.1);
}
</style>
<link rel="stylesheet" type="text/css" href="css/readium-all.css">
<script src="scripts/readium-all.js" type="text/javascript"></script>
<title></title>
</head>
<!-- This is all application-specific HTML -->
<body class="chrome-app">
<nav id="app-navbar" class="navbar" role="navigation">
</nav>
<div id="app-container">
</div>
</body>
</html>
@@ -0,0 +1,214 @@
define(['workers/Messages', 'idbFilesystem'], function(Messages, idbFilesystem){
var FileUtils = (function(){
var toArray = function(list) {
return Array.prototype.slice.call(list || [], 0);
}
var makeFile = function(root, filename, contents, callback, error){
root.getFile(filename, {create:true}, function(fileEntry){
fileEntry.createWriter(function(writer){
writer.onwriteend = function(){
// strange piece of the FileWriter api. Writing to an
// existing file just overwrites content in place. Still need to truncate
// which triggers onwritend event...again. o_O
if (writer.position < writer.length){
writer.truncate(writer.position);
}
else if (callback)
callback(fileEntry);
}
writer.onerror = function(e){
throw(e);
}
if (contents instanceof ArrayBuffer){
contents = new Uint8Array(contents);
}
writer.write(new Blob([contents]));
}, error);
}, error);
}
var makeDir = function(root, dirname, callback, error){
root.getDirectory(dirname, {create:true},callback, error);
}
return {
mkdirs : function(root, dirname, callback, error){
var pathParts;
if (dirname instanceof Array){
pathParts = dirname;
}
else{
pathParts = dirname.split('/');
}
var makeDirCallback = function(dir){
if (pathParts.length){
makeDir(dir, pathParts.shift(), makeDirCallback, error);
}
else{
if (callback)
callback(dir);
}
}
makeDirCallback(root);
},
rmDir : function (root, dirname, callback, error){
root.getDirectory(dirname, {create:true}, function(dirEntry){
dirEntry.removeRecursively(callback, error);
});
},
rmFile : function(root, filename, callback, error){
root.getFile(filename, {create:true}, function(fileEntry){
fileEntry.remove(callback, error);
});
},
mkfile : function(root, filename, contents, callback, error){
if (filename.charAt(0) == '/'){
filename = filename.substring(1);
}
var pathParts = filename.split('/');
if (pathParts.length > 1){
FileUtils.mkdirs(root, pathParts.slice(0, pathParts.length - 1), function(dir){
makeFile(dir, pathParts[pathParts.length - 1], contents, callback, error);
}, error);
}
else{
makeFile(root, filename, contents, callback, error);
}
},
ls: function(dir, callback, error){
var reader = dir.createReader();
var entries = [];
var readEntries = function() {
reader.readEntries (function(results) {
if (!results.length) {
callback(entries);
} else {
entries = entries.concat(toArray(results));
readEntries();
}
}, error);
}
readEntries();
},
readBlob: function(blob, dataType, callback){
var reader = new FileReader();
reader.onloadend = function(e){
callback(reader.result);
}
reader["readAs" + dataType](blob);
},
readFileEntry : function(fileEntry, dataType, callback){
fileEntry.file(function(file){
FileUtils.readBlob(file, dataType, callback);
});
},
readFile : function(root, file, dataType, callback, error) {
root.getFile(file, {create:false}, function(fileEntry){
FileUtils.readFileEntry(fileEntry, dataType, callback, error);
}, error);
}
};
}
)();
var rootDir;
var wrapErrorHandler = function(op, path, handler){
return function(err){
var data = {path: path, error: err.name, op: op};
handler(Messages.ERROR_STORAGE, data);
console.error(data);
}
};
var copyDir = function(from, to, success, error){
FileUtils.ls(from, function(entries){
var counter = 0;
var checkFinished = function(){
if (++counter == entries.length){
success();
}
}
entries.forEach(function(entry){
if (entry.isFile){
entry.file(function(file){
FileUtils.mkfile(to, entry.name, file, checkFinished);
});
}
else{
FileUtils.mkdirs(to, entry.name, function(dir){
copyDir(entry, dir, checkFinished);
}, error);
}
});
}, error);
}
self.requestFileSystem = self.requestFileSystem || self.webkitRequestFileSystem;
var StaticStorageManager = {
saveFile : function(path, blob, success, error){
FileUtils.mkfile(rootDir, path, blob, success, wrapErrorHandler('save', path, error));
},
deleteFile : function(path, success, error){
var errorHandler = wrapErrorHandler('delete', path, error);
if (path == '/'){
FileUtils.ls(rootDir, function(entries){
var count = entries.length;
var checkDone = function(){
if (--count == 0){
success();
}
}
entries.forEach(function(entry){
if (entry.isDirectory){
entry.removeRecursively(checkDone, errorHandler)
}
else{
entry.remove(checkDone, errorHandler);
}
});
}, error);
}
else{
FileUtils.rmDir(rootDir, path, success, errorHandler);
}
},
getPathUrl : function(path){
if (path.charAt(0) == '/')
path = path.substring(1);
return rootDir.toURL() + path
},
initStorage : function(success, error){
if (rootDir){
success();
return;
}
requestFileSystem(self.PERSISTENT, 5*1024*1024*1024, function(fs){
rootDir = fs.root;
success();
}, wrapErrorHandler('init', '/', error));
}
}
//$(window).bind('libraryUIReady', function(){
return StaticStorageManager;
});
Oops, something went wrong.

0 comments on commit 5c88997

Please sign in to comment.