aboutsummaryrefslogtreecommitdiff
path: root/js
diff options
context:
space:
mode:
authorJose Antonio Marquez2012-02-04 22:32:18 -0800
committerJose Antonio Marquez2012-02-04 22:32:18 -0800
commit23d958580a785ae265bb0a78f238093cc93cee85 (patch)
treef8aaa2ed9c81b1ba44b821cb2adf3b56e2df7431 /js
parent8e06b63e5eab5558823f4923e20a832c8b36cbe2 (diff)
downloadninja-23d958580a785ae265bb0a78f238093cc93cee85.tar.gz
Setting up Chrome File System API class
Chrome file system API will be used to copy over files from the sandboxed app into the sandboxed file system directory (in Chrome) to then be used by the core API and the cloud.
Diffstat (limited to 'js')
-rw-r--r--js/io/system/chromeapi.js295
-rwxr-xr-xjs/io/system/coreioapi.js161
-rwxr-xr-xjs/io/system/fileio.js2
3 files changed, 351 insertions, 107 deletions
diff --git a/js/io/system/chromeapi.js b/js/io/system/chromeapi.js
new file mode 100644
index 00000000..d1ff6da3
--- /dev/null
+++ b/js/io/system/chromeapi.js
@@ -0,0 +1,295 @@
1/* <copyright>
2This file contains proprietary software owned by Motorola Mobility, Inc.<br/>
3No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.<br/>
4(c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved.
5</copyright> */
6
7/* /////////////////////////////////////////////////////////////////////
8////////////////////////////////////////////////////////////////////////
9NOTES:
10////////////////////////////////////////////////////////////////////////
11///////////////////////////////////////////////////////////////////// */
12//
13var Montage = require("montage/core/core").Montage;
14////////////////////////////////////////////////////////////////////////
15//
16exports.ChromeApi = Montage.create(Object.prototype, {
17 ////////////////////////////////////////////////////////////////////
18 //Needs size in MBs for fileSystem init
19 init: {
20 enumerable: true,
21 value: function(size) {
22 //
23 if (window.webkitRequestFileSystem) {
24 //Current way to init Chrome's fileSystem API
25 window.webkitRequestFileSystem(window.PERSISTENT, size*1024*1024, function (fs) {
26 this.fileSystem = fs;
27 }).bind(this);
28 } else {
29 //No fileSystem API
30 }
31 }
32 },
33 ////////////////////////////////////////////////////////////////////
34 //
35 _fileSystem: {
36 enumerable: false,
37 value: null
38 },
39 ////////////////////////////////////////////////////////////////////
40 //
41 fileSystem: {
42 enumerable: false,
43 get: function() {
44 return this._fileSystem;
45 },
46 set: function(value) {
47 this._fileSystem = value;
48 }
49 }
50 ////////////////////////////////////////////////////////////////////
51 ////////////////////////////////////////////////////////////////////
52});
53////////////////////////////////////////////////////////////////////////
54////////////////////////////////////////////////////////////////////////
55
56
57
58//window.webkitRequestFileSystem(window.PERSISTENT, 10*1024*1024 /*10MB*/, function (fs) {
59
60
61
62 /*
63for (var i=1; i<50; i++) {
64 fs.root.getDirectory('montage0.0.0.'+i, {}, function(dirEntry) {
65 //
66 dirEntry.removeRecursively(function() {
67 console.log('Directory removed.');
68 });
69 });
70 }
71*/
72
73
74
75
76
77
78 //
79 /*
80var xhr = new XMLHttpRequest(), dir, mjs;
81 //
82 xhr.open("GET", '/ninja-internal/node_modules/descriptor.json', false);
83 xhr.send();
84 //
85 if (xhr.readyState === 4) {
86 //
87 mjs = JSON.parse(xhr.response);
88 //
89 if (mjs.version) {
90 //Checking for version to exist
91 fs.root.getDirectory('montage'+mjs.version, {}, function(dirEntry) {
92 //Already copied, nothing
93 console.log('montage'+mjs.version+' has already been created');
94 }, function (e) {
95 //Not present, should be copied
96 createFolder(false, {name: 'montage'+mjs.version});
97 //
98 for (var i in mjs.directories) {
99 createFolder('montage'+mjs.version, mjs.directories[i]);
100 }
101 //
102
103
104 for (var j in mjs.files) {
105
106 var frqst = new XMLHttpRequest();
107 frqst.open("GET", '/ninja-internal/node_modules/montage/'+mjs.files[j], false);
108 frqst.send();
109
110 if (frqst.readyState === 4) {
111 createFile('montage'+mjs.version+'/'+mjs.files[j], frqst.response);
112 }
113 }
114
115 //
116 console.log('montage'+mjs.version+' was created');
117 }, folderError);
118 }
119
120 }
121 //
122 function createFile (path, content) {
123 //
124 fs.root.getFile(path, {create: true}, function(fileEntry) {
125 //
126 fileEntry.createWriter(function(fileWriter) {
127 //
128 //console.log(path);
129 var bb = new window.WebKitBlobBuilder;
130 bb.append(content);
131 fileWriter.write(bb.getBlob('text/plain'));
132 }, fileError);
133
134 }, fileError);
135 }
136 //
137 function createFolder(root, folder) {
138 if (folder.name) {
139 if (root) {
140 dir = root+'/'+folder.name;
141 } else {
142 dir = folder.name;
143 }
144 //
145 //console.log(dir);
146 //
147 fs.root.getDirectory(dir, {create: true}, function(dirEntry) {
148 //
149 }, folderError);
150 }
151 //
152 if (folder.children) {
153 for (var i in folder.children) {
154 if (root) {
155 createFolder(root+'/'+folder.name, folder.children[i]);
156 } else {
157 createFolder(folder.name, folder.children[i]);
158 }
159 }
160 }
161 }
162 //
163 function folderError (e) {
164 console.log(e);
165 }
166 function fileError (e) {
167 console.log(e);
168 }
169
170
171 setTimeout( function () {
172 for (var m in mjs.files) {
173
174 fs.root.getFile('montage'+mjs.version+'/'+mjs.files[m], {}, function(fileEntry) {
175
176 console.log(mjs.files[m]);
177
178 fileEntry.file(function(file) {
179
180 var reader = new FileReader();
181 reader.onloadend = function(e) {
182 //console.log(e.target.file.name);
183 var test = this.createFile({uri: '/Users/kgq387/Desktop/Ninja Cloud/Disk/'+e.target.file.name, contents: e.target.result});
184 }.bind(this);
185
186 reader.file = file;
187 reader.readAsText(file);
188
189 }.bind(this));
190
191 }.bind(this));
192
193 }}.bind(this), 5000);
194*/
195
196
197
198
199
200 //}.bind(this));
201
202
203
204
205
206
207
208 ////////////////////////////////////////////////////////////
209 ////////////////////////////////////////////////////////////
210 //TODO: Remove, this is only for testing
211
212
213 /*
214window.webkitRequestFileSystem(window.PERSISTENT, 10*1024*1024 , function (fs) {
215
216
217 var start = new Date().getTime();
218
219 for (var i=0; i<250; i++) {
220
221 fs.root.getFile('test'+i+'.txt', {create: true}, function(fileEntry) {
222
223 fileEntry.createWriter(function(fileWriter) {