3 * Copyright (C) 2015 "IoT.bzh"
4 * Author "Fulup Ar Foll"
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details..
17 * https://developer.mozilla.org/en/docs/Web/API/FileReader
18 * https://developer.mozilla.org/en-US/docs/Using_files_from_web_applications#Using_hidden_file_input_elements_using_the_click%28%29_method
19 * https://uncorkedstudios.com/blog/multipartformdata-file-upload-with-angularjs
20 * https://www.terlici.com/2015/05/16/uploading-files-locally.html
21 * https://github.com/nervgh/angular-file-upload/blob/master/src/services/FileUploader.js
22 * https://stuk.github.io/jszip/documentation/howto/read_zip.html
23 * http://onehungrymind.com/zip-parsing-jszip-angular/
24 * http://stackoverflow.com/questions/15341912/how-to-go-from-blob-to-arraybuffer
26 * Bugs: zip file sent even when flag as invalid
34 var tmplAppli = '<input type="file" name="{{name}}-input" onchange="angular.element(this).scope().UpLoadFile(this.files)" accept="{{mimetype}}" style="display:none">'+
35 '<div class="upload-file" ng-click="imgClicked()">' +
36 '<i class="{{icon}}"></i> <span>{{label}}</span>' +
37 '<range-slider ng-show="!noslider" id="{{name}}-slider" automatic=true inithook="SliderInitCB"></range-slider>' +
40 var tmplModal = '<span class="modal-text">Upload Application <b>{{appname}}</b> ?</span>' +
42 '<img ng-src="{{icon}}">' +
43 '<submit-button icon="fi-x" label="Cancel" clicked="abandon"></submit-button>'+
44 '<submit-button icon="fi-like" label="Install" clicked="success"></submit-button> ' +
47 var tmplError = '<span class="modal-text">Invalid Application <b>{{appname}}</b> ?</span>' +
49 '<img ng-src="{{icon}}">' +
50 '<submit-button icon="fi-x" label="Close" clicked="abandon"></submit-button>'+
54 // Service Create xform insert files in and Post it to url
55 function LoadFileSvc (scope, files, fileCB) {
56 var xmlReq = new XMLHttpRequest();
57 var xform = new FormData();
59 // Update slider during Upload
60 xmlReq.upload.onprogress = function (event) {
61 var progress = Math.round(event.lengthComputable ? event.loaded * 100 / event.total : 0);
62 if (scope.slider) scope.slider.setValue (progress);
65 // Upload is finish let's notify controler callback
66 xmlReq.onload = function () {
67 scope.divElem.addClass ("success");
68 scope.divElem.removeClass ("error");
70 status : xmlReq.status,
71 headers: xmlReq.getAllResponseHeaders()
73 scope.callback (response);
76 xmlReq.onerror = function () {
77 scope.divElem.addClass ("error");
78 scope.divElem.removeClass ("success");
81 xmlReq.onabort = function () {
82 scope.divElem.addClass ("error");
83 scope.divElem.removeClass ("success");
85 status : xmlReq.status,
86 headers: xmlReq.getAllResponseHeaders()
88 scope.callback (response);
91 this.postfile = function(posturl) {
92 // everything looks OK let's Post it
93 xmlReq.open("POST", posturl , true);
97 for (var i = 0; i < files.length; i++) {
99 // Unknow Type !!! if (!this.file.type.match(scope.mimetype)) continue;
101 console.log ("Selected file=" + this.file.name + " size="+ this.file.size/1024 + " Type="+ this.file.type);
103 this.basename= this.file.name.split('/').reverse()[0];
105 // File to upload is too big
106 if (isNaN(this.file.size) || this.file.size > scope.maxsize*1024) {
107 setTimeout (fileCB,100); // On error asynchronous callback without argument
111 // If File is readable let's read it
112 var reader = new FileReader();
113 reader.readAsArrayBuffer(this.file);
114 reader.onload = fileCB;
116 // if everything is OK let's add file to xform
117 xform.append(scope.name, this.file, this.file.name);
122 angular.module('UploadFiles',['AppConfig', 'ModalNotification', 'RangeSlider'])
124 .directive('uploadAppli', function(AppConfig, JQemu, Notification, ModalFactory, $timeout) {
125 function mymethods(scope, elem, attrs) {
127 // get widget image handle from template
128 scope.inputElem = elem.find('input');
129 scope.divElem = elem.find('div');
131 // Image was ckick let's simulate an input (file) click
132 scope.imgClicked = function () {
133 scope.inputElem[0].click(); // Warning Angular TriggerEvent does not work!!!
136 // Slider control handle registration after creation
137 scope.SliderInitCB=function (slider) {
138 scope.slider= slider;
141 // Upload is delegated to a shared function
142 scope.UpLoadFile=function (files) {
147 var success = function() {
148 // This Looks OK let's Post Xform/File
149 handle.postfile(attrs.posturl + "?token=" + AppConfig.session.token);
151 scope.modal.deactivate();
152 $timeout (function() {scope.modal.destroy();}, 1000);
155 var abandon = function() {
156 scope.modal.deactivate();
157 $timeout (function() {scope.modal.destroy();}, 1000);
160 var readerCB = function (upload) {
162 // File upload fail handle error
164 if (handle.file.size > scope.maxsize*1024) {
165 appicon = scope.istoobig;
169 if (isNaN(handle.file.size)) {
170 appicon = scope.isnotvalid;
176 var zipapp = new JSZip (upload.target.result);
177 var thumbnail = zipapp.file("icon_128.png");
179 // Check is we have a thumbnail within loaded Zipfile
181 console.log ("This is not a valid Application Framework APP");
182 appicon = scope.isnotvalid;
186 //scope.imgElem[0].src = window.URL.createObjectURL(new Blob([thumbnail.asArrayBuffer()], {type: "image"}));
187 appicon = window.URL.createObjectURL(new Blob([thumbnail.asArrayBuffer()], {type: "image"}));
188 template = tmplModal;
192 // reference http://foundation.zurb.com/apps/docs/#!/angular-modules
194 animationIn: 'slideInFromTop',
199 appname : handle.basename
200 }, template : template
202 // Popup Modal to render application data
203 scope.modal = new ModalFactory(config);
204 scope.modal.activate ();
207 // Load file within browser and if OK call readerCB
208 handle = new LoadFileSvc (scope, files, readerCB);
209 console.log (handle);
212 // Initiallize default values from attributes values
213 scope.name= attrs.name || 'appli';
214 scope.category= attrs.category || 'appli';
215 scope.mimetype= (attrs.accept || '.wgt');
216 scope.maxsize = attrs.maxsize || 100000; // default max size 100MB
217 scope.regexp = new RegExp (attrs.accept+ '.*','i');
218 scope.icon = attrs.icon || 'fi-upload';
219 scope.label = attrs.label || 'Upload';
221 if (attrs.thumbnail) scope.isnotvalid= AppConfig.paths[scope.category] + attrs.isnotvalid;
222 else scope.isnotvalid=AppConfig.paths[scope.category] + 'w3c-widget.png';
224 if (attrs.istoobig) scope.istoobig= AppConfig.paths[scope.category] + attrs.istoobig;
225 else scope.istoobig=AppConfig.paths[scope.category] + 'istoobig.png';
226 scope.noslider = attrs.noslider || false;
228 if (!attrs.posturl) throw new TypeError('file-upload %s posturl=/api/xxxx/xxxx required', scope.attrs);
241 console.log ("UploadFile Loaded");