From 295d72960cad391850803e8c4e83d5133bbe82f7 Mon Sep 17 00:00:00 2001 From: Jose Antonio Marquez Date: Mon, 6 Feb 2012 11:40:15 -0800 Subject: Setting up library detection for Chrome File System --- js/io/system/chromeapi.js | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) (limited to 'js/io/system/chromeapi.js') diff --git a/js/io/system/chromeapi.js b/js/io/system/chromeapi.js index 6bf6b9fe..f4e04a09 100644 --- a/js/io/system/chromeapi.js +++ b/js/io/system/chromeapi.js @@ -31,6 +31,8 @@ exports.ChromeApi = Montage.create(Object.prototype, { var readyEvent = document.createEvent("CustomEvent"); readyEvent.initEvent('ready', true, true); this.dispatchEvent(readyEvent); + //Building data of local Ninja Library + this._listNinjaChromeLibrary(); }.bind(this), function (e) {return false}); //Returns false on error (not able to init) // return true; @@ -73,10 +75,17 @@ exports.ChromeApi = Montage.create(Object.prototype, { } }, //////////////////////////////////////////////////////////////////// - // + //Returns the directory contents to a callback function directoryContents: { enumerable: true, - value: function() { + value: function(directory, callback) { + //Creating instance of directory reader + this.fileSystem.directoryReader = directory.createReader(); + //Getting directory contents and sending results to callback + this.fileSystem.directoryReader.readEntries(function(results) { + //Calling callback with results (null if invalid directory) + callback(results); + }, function (e) {callback(null)}); } }, //////////////////////////////////////////////////////////////////// @@ -102,11 +111,24 @@ exports.ChromeApi = Montage.create(Object.prototype, { }, //////////////////////////////////////////////////////////////////// // - getLocalLibrary: { + _listNinjaChromeLibrary: { enumerable: false, value: function () { + function parseLibrary (contents) { + // + var lib = []; + // + + + + //Dispatching action ready event + var libraryEvent = document.createEvent("CustomEvent"); + libraryEvent.initEvent('library', true, true); + libraryEvent.ninjaChromeLibrary = lib; + this.dispatchEvent(libraryEvent); + }; // - return {}; + this.directoryContents(this.fileSystem.root, parseLibrary.bind(this)); } } //////////////////////////////////////////////////////////////////// -- cgit v1.2.3 From 0db246b19dc0b50f4f663a147ec92c49e656ae35 Mon Sep 17 00:00:00 2001 From: Jose Antonio Marquez Date: Mon, 6 Feb 2012 16:32:15 -0800 Subject: Adding logic to copy ninja libraries Setting up the logic to handling copying ninja required libraries into the app local file system sandbox to be used by cloud simulator. --- js/io/system/chromeapi.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) (limited to 'js/io/system/chromeapi.js') diff --git a/js/io/system/chromeapi.js b/js/io/system/chromeapi.js index f4e04a09..fb141687 100644 --- a/js/io/system/chromeapi.js +++ b/js/io/system/chromeapi.js @@ -69,9 +69,16 @@ exports.ChromeApi = Montage.create(Object.prototype, { //////////////////////////////////////////////////////////////////// // - directoryDelete: {//TODO: Make sure it uses a force delete + directoryDelete: { enumerable: true, - value: function() { + value: function(directoryPath, callback) { + // + this.fileSystem.getDirectory(directoryPath, {}, function(dirEntry) { + // + dirEntry.removeRecursively(function() { + callback(true); + }); + }, function (e) {callback(false)}); } }, //////////////////////////////////////////////////////////////////// -- cgit v1.2.3 From ad85ff92ba44fb3c8ccf24c2f1b9296804dfa8ca Mon Sep 17 00:00:00 2001 From: Jose Antonio Marquez Date: Mon, 6 Feb 2012 22:33:42 -0800 Subject: Single file library syncing Added the ability to store locally in chrome single file libraries used by Ninja. Working on adding multi-file libraries. --- js/io/system/chromeapi.js | 51 ++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 42 insertions(+), 9 deletions(-) (limited to 'js/io/system/chromeapi.js') diff --git a/js/io/system/chromeapi.js b/js/io/system/chromeapi.js index fb141687..6df41fd3 100644 --- a/js/io/system/chromeapi.js +++ b/js/io/system/chromeapi.js @@ -9,6 +9,9 @@ No rights, expressed or implied, whatsoever to this software are provided by Mot NOTES: The init function starts up the file system API, and a size must be set, no unlimited available as of now. + + Core API reference in NINJA: this.application.ninja.coreIoApi + //////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////// */ // @@ -61,24 +64,51 @@ exports.ChromeApi = Montage.create(Object.prototype, { }, //////////////////////////////////////////////////////////////////// // + fileNew: { + enumerable: true, + value: function(filePath, content, mime, callback) { + // + this.fileSystem.root.getFile(filePath, {create: true}, function(f) { + // + f.createWriter(function(writer) { + // + var b = new window.WebKitBlobBuilder; + b.append(content); + writer.write(b.getBlob(mime)); + // + if (callback) callback(true); + }, function (e) {if (callback) callback(false)}); + }, function (e) {if (callback) callback(false)}); + } + }, + //////////////////////////////////////////////////////////////////// + //Creating directory from path, callback optional directoryNew: { enumerable: true, - value: function() { + value: function(directoryPath, callback) { + //Checking for directory not to already exist + this.fileSystem.root.getDirectory(directoryPath, {}, function(dir) { + if (callback) callback(false); + return; //Directory already exists + }); + //Creating new directory + this.fileSystem.root.getDirectory(directoryPath, {create: true}, function(dir) { + if (callback) callback(true); + }, function (e) {if (callback) callback(false)}); } }, - //////////////////////////////////////////////////////////////////// // directoryDelete: { enumerable: true, value: function(directoryPath, callback) { // - this.fileSystem.getDirectory(directoryPath, {}, function(dirEntry) { + this.fileSystem.root.getDirectory(directoryPath, {}, function(dir) { // - dirEntry.removeRecursively(function() { - callback(true); + dir.removeRecursively(function() { + if (callback) callback(true); }); - }, function (e) {callback(false)}); + }, function (e) {if (callback) callback(false)}); } }, //////////////////////////////////////////////////////////////////// @@ -125,9 +155,12 @@ exports.ChromeApi = Montage.create(Object.prototype, { // var lib = []; // - - - + for(var i=0; contents[i]; i++) { + // + if (contents[i].isDirectory) { + lib.push(contents[i].name); + } + } //Dispatching action ready event var libraryEvent = document.createEvent("CustomEvent"); libraryEvent.initEvent('library', true, true); -- cgit v1.2.3 From 111509d5cbbd54b11afbc49f7c78d0c0548bb617 Mon Sep 17 00:00:00 2001 From: Jose Antonio Marquez Date: Tue, 7 Feb 2012 12:06:21 -0800 Subject: Adding mime-type detection for new file --- js/io/system/chromeapi.js | 288 +++++++--------------------------------------- 1 file changed, 44 insertions(+), 244 deletions(-) (limited to 'js/io/system/chromeapi.js') diff --git a/js/io/system/chromeapi.js b/js/io/system/chromeapi.js index 6df41fd3..2fc8769c 100644 --- a/js/io/system/chromeapi.js +++ b/js/io/system/chromeapi.js @@ -66,13 +66,54 @@ exports.ChromeApi = Montage.create(Object.prototype, { // fileNew: { enumerable: true, - value: function(filePath, content, mime, callback) { + value: function(filePath, content, callback) { // this.fileSystem.root.getFile(filePath, {create: true}, function(f) { // f.createWriter(function(writer) { // - var b = new window.WebKitBlobBuilder; + var b, mime, type = filePath.split('.'); + type = type[type.length-1]; + switch (type) { + case 'bmp': + mime = 'image/bmp'; + break; + case 'gif': + mime = 'image/gif'; + break; + case 'jpeg': + mime = 'image/jpeg'; + break; + case 'jpg': + mime = 'image/jpeg'; + break; + case 'png': + mime = 'image/png'; + break; + case 'rtf': + mime = 'application/rtf'; + break; + case 'tif': + mime = 'image/tiff'; + break; + case 'tiff': + mime = 'image/tiff'; + break; + case 'pdf': + mime = 'application/pdf'; + break; + case 'zip': + mime = 'application/zip'; + break; + case 'svg': + mime = 'image/svg+xml'; + break; + default: + mime = 'text/'+type; + break; + } + // + b = new window.WebKitBlobBuilder; b.append(content); writer.write(b.getBlob(mime)); // @@ -175,245 +216,4 @@ exports.ChromeApi = Montage.create(Object.prototype, { //////////////////////////////////////////////////////////////////// }); //////////////////////////////////////////////////////////////////////// -//////////////////////////////////////////////////////////////////////// - - - -//window.webkitRequestFileSystem(window.PERSISTENT, 10*1024*1024 /*10MB*/, function (fs) { - - - - /* -for (var i=1; i<50; i++) { - fs.root.getDirectory('montage0.0.0.'+i, {}, function(dirEntry) { - // - dirEntry.removeRecursively(function() { - console.log('Directory removed.'); - }); - }); - } -*/ - - - - - - - // - /* -var xhr = new XMLHttpRequest(), dir, mjs; - // - xhr.open("GET", '/ninja-internal/node_modules/descriptor.json', false); - xhr.send(); - // - if (xhr.readyState === 4) { - // - mjs = JSON.parse(xhr.response); - // - if (mjs.version) { - //Checking for version to exist - fs.root.getDirectory('montage'+mjs.version, {}, function(dirEntry) { - //Already copied, nothing - console.log('montage'+mjs.version+' has already been created'); - }, function (e) { - //Not present, should be copied - createFolder(false, {name: 'montage'+mjs.version}); - // - for (var i in mjs.directories) { - createFolder('montage'+mjs.version, mjs.directories[i]); - } - // - - - for (var j in mjs.files) { - - var frqst = new XMLHttpRequest(); - frqst.open("GET", '/ninja-internal/node_modules/montage/'+mjs.files[j], false); - frqst.send(); - - if (frqst.readyState === 4) { - createFile('montage'+mjs.version+'/'+mjs.files[j], frqst.response); - } - } - - // - console.log('montage'+mjs.version+' was created'); - }, folderError); - } - - } - // - function createFile (path, content) { - // - fs.root.getFile(path, {create: true}, function(fileEntry) { - // - fileEntry.createWriter(function(fileWriter) { - // - //console.log(path); - var bb = new window.WebKitBlobBuilder; - bb.append(content); - fileWriter.write(bb.getBlob('text/plain')); - }, fileError); - - }, fileError); - } - // - function createFolder(root, folder) { - if (folder.name) { - if (root) { - dir = root+'/'+folder.name; - } else { - dir = folder.name; - } - // - //console.log(dir); - // - fs.root.getDirectory(dir, {create: true}, function(dirEntry) { - // - }, folderError); - } - // - if (folder.children) { - for (var i in folder.children) { - if (root) { - createFolder(root+'/'+folder.name, folder.children[i]); - } else { - createFolder(folder.name, folder.children[i]); - } - } - } - } - // - function folderError (e) { - console.log(e); - } - function fileError (e) { - console.log(e); - } - - - setTimeout( function () { - for (var m in mjs.files) { - - fs.root.getFile('montage'+mjs.version+'/'+mjs.files[m], {}, function(fileEntry) { - - console.log(mjs.files[m]); - - fileEntry.file(function(file) { - - var reader = new FileReader(); - reader.onloadend = function(e) { - //console.log(e.target.file.name); - var test = this.createFile({uri: '/Users/kgq387/Desktop/Ninja Cloud/Disk/'+e.target.file.name, contents: e.target.result}); - }.bind(this); - - reader.file = file; - reader.readAsText(file); - - }.bind(this)); - - }.bind(this)); - - }}.bind(this), 5000); -*/ - - - - - - //}.bind(this)); - - - - - - - - //////////////////////////////////////////////////////////// - //////////////////////////////////////////////////////////// - //TODO: Remove, this is only for testing - - - /* -window.webkitRequestFileSystem(window.PERSISTENT, 10*1024*1024 , function (fs) { - - - var start = new Date().getTime(); - - for (var i=0; i<250; i++) { - - fs.root.getFile('test'+i+'.txt', {create: true}, function(fileEntry) { - - fileEntry.createWriter(function(fileWriter) { - - - var xhr = new XMLHttpRequest(); - // - xhr.open("GET", '/ninja-internal/js/io/templates/descriptor.json', false); - xhr.send(); - // - if (xhr.readyState === 4) { - var bb = new window.WebKitBlobBuilder; - bb.append(xhr.response); - fileWriter.write(bb.getBlob('text/plain')); - } - - }); - - }); - - } - - var end = new Date().getTime(); - var time = end - start; - console.log('Create execution time: ' + time); - - start = new Date().getTime(); - - for (var j=0; j<250; j++) { - - fs.root.getFile('test'+j+'.txt', {create: true}, function(fileEntry) { - - - - fileEntry.file(function(file) { - - var reader = new FileReader(); - reader.onloadend = function(e) { - //console.log(this, e.target); - var test = this.createFile({uri: '/Users/kgq387/Desktop/Ninja Cloud/Disk/'+e.target.file.name, contents: e.target.result}); - console.log(e.target.file.name); - - }.bind(this); - - reader.file = file; - reader.readAsText(file); - - }.bind(this)); - - }.bind(this)); - - } - - - end = new Date().getTime(); - time = end - start; - console.log('Read execution time: ' + time); - - for (var k=0; k<250; k++) { - - fs.root.getFile('test'+k+'.txt', {create: true}, function(fileEntry) { - - fileEntry.remove(function(fileWriter) { - - - }); - - }); - - } - - - - }.bind(this)); -*/ \ No newline at end of file +//////////////////////////////////////////////////////////////////////// \ No newline at end of file -- cgit v1.2.3 From af58afcffff5ac556d16f050a325ac0406897fcd Mon Sep 17 00:00:00 2001 From: Jose Antonio Marquez Date: Tue, 7 Feb 2012 17:03:44 -0800 Subject: Copy local library to cloud Adding functionality to copy a local library to the cloud. Currently creating directory structure. Need to add ability to copy files and ensure proper mime-type is set. --- js/io/system/chromeapi.js | 43 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 2 deletions(-) (limited to 'js/io/system/chromeapi.js') diff --git a/js/io/system/chromeapi.js b/js/io/system/chromeapi.js index 2fc8769c..fc93b22a 100644 --- a/js/io/system/chromeapi.js +++ b/js/io/system/chromeapi.js @@ -7,11 +7,10 @@ No rights, expressed or implied, whatsoever to this software are provided by Mot /* ///////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////// NOTES: + The init function starts up the file system API, and a size must be set, no unlimited available as of now. - Core API reference in NINJA: this.application.ninja.coreIoApi - //////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////// */ // @@ -123,6 +122,46 @@ exports.ChromeApi = Montage.create(Object.prototype, { } }, //////////////////////////////////////////////////////////////////// + // + fileDelete: { + enumerable: true, + value: function(filePath, callback) { + this.fileSystem.root.getFile(filePath, {create: false}, function(file) { + file.remove(function() { + if (callback) callback(true); + }); + }, function (e) {if (callback) callback(false)}); + } + }, + //////////////////////////////////////////////////////////////////// + // + fileContent: { + enumerable: true, + value: function() { + } + }, + //////////////////////////////////////////////////////////////////// + // + fileCopy: { + enumerable: true, + value: function() { + } + }, + //////////////////////////////////////////////////////////////////// + // + fileRename: { + enumerable: true, + value: function() { + } + }, + //////////////////////////////////////////////////////////////////// + // + fileMove: { + enumerable: true, + value: function() { + } + }, + //////////////////////////////////////////////////////////////////// //Creating directory from path, callback optional directoryNew: { enumerable: true, -- cgit v1.2.3 From 687cfbbae1df2392267e9602f955f6eadd5b339d Mon Sep 17 00:00:00 2001 From: Jose Antonio Marquez Date: Tue, 7 Feb 2012 23:13:15 -0800 Subject: Mime type fixes Trying to fix mime type issues in FileSystem, Chrome does not support reponseType 'blob', so looking at alternatives. --- js/io/system/chromeapi.js | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) (limited to 'js/io/system/chromeapi.js') diff --git a/js/io/system/chromeapi.js b/js/io/system/chromeapi.js index fc93b22a..c19a7d3b 100644 --- a/js/io/system/chromeapi.js +++ b/js/io/system/chromeapi.js @@ -71,7 +71,7 @@ exports.ChromeApi = Montage.create(Object.prototype, { // f.createWriter(function(writer) { // - var b, mime, type = filePath.split('.'); + var mime, blob = new window.WebKitBlobBuilder, type = filePath.split('.'); type = type[type.length-1]; switch (type) { case 'bmp': @@ -112,9 +112,8 @@ exports.ChromeApi = Montage.create(Object.prototype, { break; } // - b = new window.WebKitBlobBuilder; - b.append(content); - writer.write(b.getBlob(mime)); + blob.append(content); + writer.write(blob.getBlob(mime)); // if (callback) callback(true); }, function (e) {if (callback) callback(false)}); @@ -137,7 +136,19 @@ exports.ChromeApi = Montage.create(Object.prototype, { // fileContent: { enumerable: true, - value: function() { + value: function(filePath, callback) { + // + this.fileSystem.root.getFile(filePath, {}, function(f) { + f.file(function(file) { + var reader = new FileReader(); + reader.onloadend = function(e) { + if (callback) { + callback({content: this.result, data: file, file: f, url: f.toURL()}); + } + }; + reader.readAsText(file); + }, function (e) {if (callback) callback(false)}); + }, function (e) {if (callback) callback(false)}); } }, //////////////////////////////////////////////////////////////////// -- cgit v1.2.3 From 4bfac53c9a77a3af35d029757eece53f4b7212ed Mon Sep 17 00:00:00 2001 From: Jose Antonio Marquez Date: Wed, 8 Feb 2012 13:11:48 -0800 Subject: Fixed data type issue on copying library files Fixed the methods to allow for ArrayBuffer data to be sent, fixes saving files that are not plain text. --- js/io/system/chromeapi.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'js/io/system/chromeapi.js') diff --git a/js/io/system/chromeapi.js b/js/io/system/chromeapi.js index c19a7d3b..eee7409d 100644 --- a/js/io/system/chromeapi.js +++ b/js/io/system/chromeapi.js @@ -146,7 +146,7 @@ exports.ChromeApi = Montage.create(Object.prototype, { callback({content: this.result, data: file, file: f, url: f.toURL()}); } }; - reader.readAsText(file); + reader.readAsArrayBuffer(file); }, function (e) {if (callback) callback(false)}); }, function (e) {if (callback) callback(false)}); } -- cgit v1.2.3