diff options
author | Ananya Sen | 2012-06-21 11:02:55 -0700 |
---|---|---|
committer | Ananya Sen | 2012-06-21 11:02:55 -0700 |
commit | 8db9e73ca68c006769af3997034959f6b7008add (patch) | |
tree | c99666706ec0678e523af8826d8297173116bda6 /js/mediators/io-mediator.js | |
parent | 8a47d6934fb2647ec26104935caf57a5cd73cdf2 (diff) | |
download | ninja-8db9e73ca68c006769af3997034959f6b7008add.tar.gz |
- added io api to generate file from binary
- refactoring and cleanup
Signed-off-by: Ananya Sen <Ananya.Sen@motorola.com>
Diffstat (limited to 'js/mediators/io-mediator.js')
-rw-r--r-- | js/mediators/io-mediator.js | 53 |
1 files changed, 52 insertions, 1 deletions
diff --git a/js/mediators/io-mediator.js b/js/mediators/io-mediator.js index c9ea0468..c6e7d960 100644 --- a/js/mediators/io-mediator.js +++ b/js/mediators/io-mediator.js | |||
@@ -210,7 +210,58 @@ exports.IoMediator = Montage.create(Component, { | |||
210 | //////////////////////////////////////////////////////////////////// | 210 | //////////////////////////////////////////////////////////////////// |
211 | createFileFromBinary:{ | 211 | createFileFromBinary:{ |
212 | enumerable: false, | 212 | enumerable: false, |
213 | value: function(blob, filename){ | 213 | value: function(blob, callback){ |
214 | var reader = new FileReader(), file = reader.readAsArrayBuffer(blob), url, uri, dir, save, counter, tempName, element, rules, fileName, fileNameOverride, | ||
215 | rootUrl = this.application.ninja.coreIoApi.rootUrl+escape((this.application.ninja.documentController.documentHackReference.root.split(this.application.ninja.coreIoApi.cloudData.root)[1])), | ||
216 | rootUri = this.application.ninja.documentController.documentHackReference.root; | ||
217 | |||
218 | reader.fileName = blob.name, reader.fileType = blob.type, reader.rootUrl = rootUrl, reader.rootUri = rootUri; | ||
219 | if(callback && callback.position){reader.filePosition = callback.position;} | ||
220 | |||
221 | reader.onload = function (e) { | ||
222 | if(e.currentTarget.fileType.indexOf('image') !== -1 ){ | ||
223 | if (this.application.ninja.coreIoApi.directoryExists({uri: e.currentTarget.rootUri+'images'}).status === 204) { | ||
224 | uri = e.currentTarget.rootUri+'images'; | ||
225 | url = e.currentTarget.rootUrl+'images'; | ||
226 | } else if (this.application.ninja.coreIoApi.directoryExists({uri: e.currentTarget.rootUri+'img'}).status === 204) { | ||
227 | uri = e.currentTarget.rootUri+'img'; | ||
228 | url = e.currentTarget.rootUrl+'img'; | ||
229 | } else { | ||
230 | dir = this.application.ninja.coreIoApi.createDirectory({uri: e.currentTarget.rootUri+'images'}); | ||
231 | if (dir.success && dir.status === 201) { | ||
232 | uri = e.currentTarget.rootUri+'images'; | ||
233 | url = e.currentTarget.rootUrl+'images'; | ||
234 | } else { | ||
235 | //TODO: HANDLE ERROR ON CREATING FOLDER | ||
236 | } | ||
237 | } | ||
238 | //fileName is undefined while pasting image from clipboard | ||
239 | fileNameOverride = e.currentTarget.fileName ? e.currentTarget.fileName : ("image." + e.currentTarget.fileType.substring((e.currentTarget.fileType.indexOf("/")+1), e.currentTarget.fileType.length));//like image.png | ||
240 | |||
241 | if (this.application.ninja.coreIoApi.fileExists({uri: uri+'/'+fileNameOverride}).status === 404) { | ||
242 | save = this.application.ninja.coreIoApi.createFile({uri: uri+'/'+fileNameOverride, contents: e.currentTarget.result, contentType: e.currentTarget.fileType}); | ||
243 | fileName = fileNameOverride; | ||
244 | } else { | ||
245 | counter = 1; | ||
246 | tempName = fileNameOverride.split('.'+(fileNameOverride.split('.')[fileNameOverride.split('.').length-1]))[0]; | ||
247 | tempName += '_'+counter+'.'+(fileNameOverride.split('.')[fileNameOverride.split('.').length-1]); | ||
248 | while (this.application.ninja.coreIoApi.fileExists({uri: uri+'/'+tempName}).status !== 404) { | ||
249 | counter++; | ||
250 | tempName = fileNameOverride.split('.'+(fileNameOverride.split('.')[fileNameOverride.split('.').length-1]))[0]; | ||
251 | tempName += '_'+counter+'.'+(fileNameOverride.split('.')[fileNameOverride.split('.').length-1]); | ||
252 | } | ||
253 | save = this.application.ninja.coreIoApi.createFile({uri: uri+'/'+tempName, contents: e.currentTarget.result, contentType: e.currentTarget.fileType}); | ||
254 | fileName = tempName; | ||
255 | } | ||
256 | |||
257 | if(callback && callback.addFileToStage && (typeof callback.addFileToStage === "function")){ | ||
258 | callback.addFileToStage({"save": save, "url": url, "filename": fileName, "filePosition": e.currentTarget.filePosition, "fileType":e.currentTarget.fileType}); | ||
259 | } | ||
260 | |||
261 | }else{ | ||
262 | //TODO: HANDLE NOT AN IMAGE | ||
263 | } | ||
264 | }.bind(this); | ||
214 | 265 | ||
215 | } | 266 | } |
216 | } | 267 | } |