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 +++++++------------------------------------ js/io/system/ninjalibrary.js | 4 +- 2 files changed, 46 insertions(+), 246 deletions(-) (limited to 'js/io') 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 diff --git a/js/io/system/ninjalibrary.js b/js/io/system/ninjalibrary.js index 09cf7407..658ce589 100644 --- a/js/io/system/ninjalibrary.js +++ b/js/io/system/ninjalibrary.js @@ -83,7 +83,7 @@ exports.NinjaLibrary = Montage.create(Object.prototype, { //Checking for status if (xhr.readyState === 4) { //TODO: add check for mime type //Creating new file from loaded content - this.chromeApi.fileNew('/'+tocopylibs[i].name+'/'+tocopylibs[i].file, xhr.response, 'text/plain'); + this.chromeApi.fileNew('/'+tocopylibs[i].name+'/'+tocopylibs[i].file, xhr.response); } else { //Error creating single file library } @@ -120,7 +120,7 @@ exports.NinjaLibrary = Montage.create(Object.prototype, { xhr.send(); //Checking for status if (xhr.readyState === 4) { - this.api.fileNew(this.local+'/'+this.files[i], xhr.response, 'text/plain', function (status) { + this.api.fileNew(this.local+'/'+this.files[i], xhr.response, function (status) { if (status) { this.filesCreated++; } -- cgit v1.2.3