aboutsummaryrefslogtreecommitdiff
path: root/js/mediators/io-mediator.js
diff options
context:
space:
mode:
Diffstat (limited to 'js/mediators/io-mediator.js')
-rw-r--r--js/mediators/io-mediator.js59
1 files changed, 58 insertions, 1 deletions
diff --git a/js/mediators/io-mediator.js b/js/mediators/io-mediator.js
index c487fef5..c6e7d960 100644
--- a/js/mediators/io-mediator.js
+++ b/js/mediators/io-mediator.js
@@ -205,9 +205,66 @@ exports.IoMediator = Montage.create(Component, {
205 value: function (file, callback) { 205 value: function (file, callback) {
206 //TODO: Implement Delete functionality 206 //TODO: Implement Delete functionality
207 } 207 }
208 } 208 },
209 //////////////////////////////////////////////////////////////////// 209 ////////////////////////////////////////////////////////////////////
210 //////////////////////////////////////////////////////////////////// 210 ////////////////////////////////////////////////////////////////////
211 createFileFromBinary:{
212 enumerable: false,
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);
265
266 }
267 }
211}); 268});
212//////////////////////////////////////////////////////////////////////// 269////////////////////////////////////////////////////////////////////////
213//////////////////////////////////////////////////////////////////////// \ No newline at end of file 270//////////////////////////////////////////////////////////////////////// \ No newline at end of file