aboutsummaryrefslogtreecommitdiff
path: root/js/io/system/ninjalibrary.js
diff options
context:
space:
mode:
Diffstat (limited to 'js/io/system/ninjalibrary.js')
-rw-r--r--js/io/system/ninjalibrary.js42
1 files changed, 28 insertions, 14 deletions
diff --git a/js/io/system/ninjalibrary.js b/js/io/system/ninjalibrary.js
index 12af5988..0470a166 100644
--- a/js/io/system/ninjalibrary.js
+++ b/js/io/system/ninjalibrary.js
@@ -57,35 +57,38 @@ exports.NinjaLibrary = Montage.create(Object.prototype, {
57 // 57 //
58 copyLibToCloud: { 58 copyLibToCloud: {
59 value: function (path, libName, callback) { 59 value: function (path, libName, callback) {
60 var libCheck = this.coreApi.directoryExists({uri: path+libName});
60 //Checking for library to exists 61 //Checking for library to exists
61 if(this.coreApi.directoryExists({uri: path+libName}).status === 404) { 62 if(libCheck.status === 404) {
62 //Getting contents to begin copying 63 //Getting contents to begin copying
63 this.chromeApi.directoryContents(this.chromeApi.fileSystem.root, function (contents) { 64 this.chromeApi.directoryContents(this.chromeApi.fileSystem.root, function (contents) {
64 for (var i in contents) { 65 for (var i in contents) {
65 if (libName === contents[i].name) { 66 if (libName === contents[i].name) {
66 //Getting contents of library to be copied 67 //Getting contents of library to be copied
67 this.chromeApi.directoryContents(contents[i], function (lib) { 68 this.chromeApi.directoryContents(contents[i], function (lib) {
68 //Copying to cloud, adding blocking if no callback specified 69 //Copying to cloud
69 if (!callback) { 70 if (callback) {
70 //TODO: Add blocking logic here 71 this.copyDirectoryToCloud(path, contents[i], path, {total: 0, copied: 0, callback: callback.bind(this)});
71 this.copyDirectoryToCloud(path, contents[i], path); 72 } else {
72 } else { 73 this.copyDirectoryToCloud(path, contents[i], path, {total: 0, copied: 0});
73 this.copyDirectoryToCloud(path, contents[i], path, callback); 74 }
74 }
75 }.bind(this)); 75 }.bind(this));
76 break; 76 break;
77 } 77 }
78 } 78 }
79 }.bind(this)); 79 }.bind(this));
80 } else if (libCheck.status === 204){
81 //Already present, so sending success
82 if (callback) callback(true);
80 } else { 83 } else {
81 //TODO: Add error handling 84 if (callback) callback(false);
82 } 85 }
83 } 86 }
84 }, 87 },
85 //////////////////////////////////////////////////////////////////// 88 ////////////////////////////////////////////////////////////////////
86 // 89 //
87 copyDirectoryToCloud: { 90 copyDirectoryToCloud: {
88 value: function(root, folder, fileRoot, callback) { 91 value: function(root, folder, fileRoot, tracking) {
89 //Setting up directory name 92 //Setting up directory name
90 if (folder.name) { 93 if (folder.name) {
91 var dir; 94 var dir;
@@ -109,19 +112,30 @@ exports.NinjaLibrary = Montage.create(Object.prototype, {
109 //Checking for file or directory 112 //Checking for file or directory
110 if (contents[i].isDirectory) { 113 if (contents[i].isDirectory) {
111 //Recursive call if directory 114 //Recursive call if directory
112 this.copyDirectoryToCloud(dir, contents[i], fileRoot); 115 this.copyDirectoryToCloud(dir, contents[i], fileRoot, tracking);
113 } else if (contents[i].isFile){ 116 } else if (contents[i].isFile){
117 //
118 tracking.total++;
114 //Copying file 119 //Copying file
115 this.chromeApi.fileContent(contents[i].fullPath, function (result) { 120 this.chromeApi.fileContent(contents[i].fullPath, function (result) {
116 //Using binary when copying files to allow images and such to work 121 //Using binary when copying files to allow images and such to work
117 this.coreApi.createFile({uri: (fileRoot+result.file.fullPath).replace(/\/\//gi, '/'), contents: result.content}); 122 var file = this.coreApi.createFile({uri: (fileRoot+result.file.fullPath).replace(/\/\//gi, '/'), contents: result.content});
123 //Checking for file copy success
124 if (file.status === 201) {
125 tracking.copied++;
126 } else {
127 //Error
128 tracking.callback(false);
129 }
130 //Checking for all files to be copied to make callback
131 if (tracking.copied === tracking.total && tracking.callback) {
132 tracking.callback(true);
133 }
118 }.bind(this)); 134 }.bind(this));
119 } 135 }
120 } 136 }
121 }.bind(this)); 137 }.bind(this));
122 } 138 }
123 //TODO Add logic for proper callback status(es)
124 if (callback) callback(true);
125 } 139 }
126 }, 140 },
127 //////////////////////////////////////////////////////////////////// 141 ////////////////////////////////////////////////////////////////////