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 // WARNING: Angular ng-change does not work on input/file. Let's hook our callback through standard JS function
35 var tmpl = '<input type="file" name="{{name}}-input" onchange="angular.element(this).scope().UpLoadFile(this.files)" accept="{{mimetype}}" style="display:none">'+
36 '<div class="upload-file" ng-click="imgClicked()">' +
37 '<img id="{{name}}-img" src="{{thumbnail}}">' +
38 '<range-slider ng-show="!noslider" id="{{name}}-slider" automatic=true inithook="SliderInitCB"></range-slider>' +
42 // Service Create xform insert files in and Post it to url
43 function LoadFileSvc (scope, elem, posturl, files, thumbnailCB) {
44 var xmlReq = new XMLHttpRequest();
45 var xform = new FormData();
47 var OnLoadCB = function (target) {
48 var status = thumbnailCB (target);
49 //if (status) xform.append(scope.name, file, file.name);
51 // Update slider during Upload
52 xmlReq.upload.onprogress = function (event) {
53 var progress = Math.round(event.lengthComputable ? event.loaded * 100 / event.total : 0);
54 if (scope.slider) scope.slider.setValue (progress);
57 // Upload is finish let's notify controler callback
58 xmlReq.onload = function () {
59 elem.addClass ("success");
60 elem.removeClass ("error");
62 status : xmlReq.status,
63 headers: xmlReq.getAllResponseHeaders()
65 scope.callback (response);
68 xmlReq.onerror = function () {
69 elem.addClass ("error");
70 elem.removeClass ("success");
72 status : xmlReq.status,
73 headers: xmlReq.getAllResponseHeaders()
75 scope.callback (response);
78 xmlReq.onabort = function () {
79 elem.addClass ("error");
80 elem.removeClass ("success");
82 status : xmlReq.status,
83 headers: xmlReq.getAllResponseHeaders()
85 scope.callback (response);
88 for (var i = 0; i < files.length; i++) {
90 if (!file.type.match(scope.mimetype)) {
94 console.log ("Selected file=" + file.name + " size="+ file.size/1024 + " Type="+ file.type);
96 // File to upload is too big
97 if (file.size > scope.maxsize*1024) {
98 scope.thumbnail = scope.istoobig; // warning if image path is wrong nothing happen
99 scope.$apply('thumbnail'); // we short-circuit Angular resync Image
103 // This is not an uploadable file
104 if(isNaN(file.size)) {
105 scope.thumbnail = scope.isnotvalid;
106 scope.$apply('thumbnail');
110 scope.Basename= file.name.split('/').reverse()[0];
111 scope.imgElem[0].file = file;
113 // If File is an image let display it now
115 var reader = new FileReader();
116 reader.readAsArrayBuffer(file);
117 reader.onload = OnLoadCB;
119 // if everything is OK let's add file to xform
120 xform.append(scope.name, file, file.name);
124 // everything looks OK let's Post it
125 xmlReq.open("POST", posturl , true);
129 angular.module('UploadFiles',['AppConfig', 'ModalNotification', 'RangeSlider'])
131 .directive('uploadImage', function(AppConfig, JQemu, Notification) {
132 function mymethods(scope, elem, attrs) {
134 // get widget image handle from template
135 scope.imgElem = elem.find('img');
136 scope.inputElem = elem.find('input');
138 // Image was ckick let's simulate an input (file) click
139 scope.imgClicked = function () {
140 scope.inputElem[0].click(); // Warning Angular TriggerEvent does not work!!!
143 // Slider control handle registration after creation
144 scope.SliderInitCB=function (slider) {
145 scope.slider= slider;
148 // Upload is delegated to a shared function
149 scope.UpLoadFile=function (files) {
150 var readerCB = function (upload) {
151 // scope.thumbnail = upload.target.result;
152 scope.imgElem[0].src = window.URL.createObjectURL(new Blob([upload.target.result], {type: "image"}));
153 return true; // true activates post
155 var posturl = attrs.posturl + "?token=" + AppConfig.session.token;
156 new LoadFileSvc (scope, elem, posturl, files, readerCB);
159 // Initiallize default values from attributes values
160 scope.name= attrs.name || 'file';
161 scope.category= attrs.category || 'image';
162 scope.mimetype= (attrs.accept || 'image') + '/*';
163 scope.maxsize= attrs.maxsize || 100; // default max size 100KB
164 scope.regexp = new RegExp (attrs.accept+ '.*','i');
166 if (attrs.thumbnail) scope.thumbnail= AppConfig.paths[scope.category] + attrs.thumbnail;
167 else scope.thumbnail=AppConfig.paths[scope.category] + 'tux-bzh.png';
169 if (attrs.thumbnail) scope.isnotvalid= AppConfig.paths[scope.category] + attrs.isnotvalid;
170 else scope.isnotvalid=AppConfig.paths[scope.category] + 'isnotvalid.png';
172 if (attrs.istoobig) scope.istoobig= AppConfig.paths[scope.category] + attrs.istoobig;
173 else scope.istoobig=AppConfig.paths[scope.category] + 'istoobig.png';
174 scope.noslider = attrs.noslider || false;
176 if (!attrs.posturl) throw new TypeError('file-upload %s posturl=/api/xxxx/xxxx required', scope.attrs);
188 .directive('uploadAudio', function(AppConfig, JQemu, Notification) {
189 function mymethods(scope, elem, attrs) {
191 // get widget image handle from template
192 scope.imgElem = elem.find('img');
193 scope.inputElem = elem.find('input');
195 // Image was ckick let's simulate an input (file) click
196 scope.imgClicked = function () {
197 scope.inputElem[0].click(); // Warning Angular TriggerEvent does not work!!!
200 // Slider control handle registration after creation
201 scope.SliderInitCB=function (slider) {
202 scope.slider= slider;
205 // Upload is delegated to a shared function
206 scope.UpLoadFile=function (files) {
207 var posturl = attrs.posturl + "?token=" + AppConfig.session.token;
208 new LoadFileSvc (scope, elem, posturl, files, false);
211 // Initiallize default values from attributes values
212 scope.name= attrs.name || 'audio';
213 scope.category= attrs.category || 'audio';
214 scope.mimetype= (attrs.accept || 'audio') + '/*';
215 scope.maxsize= attrs.maxsize || 10000; // default max size 10MB
216 scope.regexp = new RegExp (attrs.accept+ '.*','i');
218 if (attrs.thumbnail) scope.thumbnail= AppConfig.paths[scope.category] + attrs.thumbnail;
219 else scope.thumbnail=AppConfig.paths[scope.category] + 'upload-music.png';
221 if (attrs.thumbnail) scope.isnotvalid= AppConfig.paths[scope.category] + attrs.isnotvalid;
222 else scope.isnotvalid=AppConfig.paths[scope.category] + 'isnotvalid.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 .directive('uploadAppli', function(AppConfig, JQemu, Notification) {
242 function mymethods(scope, elem, attrs) {
244 // get widget image handle from template
245 scope.imgElem = elem.find('img');
246 scope.inputElem = elem.find('input');
248 // Image was ckick let's simulate an input (file) click
249 scope.imgClicked = function () {
250 scope.inputElem[0].click(); // Warning Angular TriggerEvent does not work!!!
253 // Slider control handle registration after creation
254 scope.SliderInitCB=function (slider) {
255 scope.slider= slider;
258 // Upload is delegated to a shared function
259 scope.UpLoadFile=function (files) {
261 var readerCB = function (upload) {
262 var zipapp = new JSZip(upload.target.result);
263 var thumbnail = zipapp.file("afa-pkg/thumbnail.jpg");
265 // Check is we have a thumbnail within loaded Zipfile
267 console.log ("This is not a valid Application Framework APP");
268 scope.thumbnail=AppConfig.paths[scope.category] + 'isnotvalid.png';
269 scope.$apply('thumbnail'); // we short-circuit Angular resync Image
270 return false; // do not post zip on binder
272 scope.imgElem[0].src = window.URL.createObjectURL(new Blob([thumbnail.asArrayBuffer()], {type: "image"}));
273 return true; // true activates post
275 var posturl = attrs.posturl + "?token=" + AppConfig.session.token;
276 new LoadFileSvc (scope, elem, posturl, files, readerCB);
279 // Initiallize default values from attributes values
280 scope.name= attrs.name || 'appli';
281 scope.category= attrs.category || 'appli';
282 scope.mimetype= (attrs.accept || '.zip');
283 scope.maxsize= attrs.maxsize || 100000; // default max size 100MB
284 scope.regexp = new RegExp (attrs.accept+ '.*','i');
286 if (attrs.thumbnail) scope.thumbnail= AppConfig.paths[scope.category] + attrs.thumbnail;
287 else scope.thumbnail=AppConfig.paths[scope.category] + 'upload-appli.png';
289 if (attrs.thumbnail) scope.isnotvalid= AppConfig.paths[scope.category] + attrs.isnotvalid;
290 else scope.isnotvalid=AppConfig.paths[scope.category] + 'isnotvalid.png';
292 if (attrs.istoobig) scope.istoobig= AppConfig.paths[scope.category] + attrs.istoobig;
293 else scope.istoobig=AppConfig.paths[scope.category] + 'istoobig.png';
294 scope.noslider = attrs.noslider || false;
296 if (!attrs.posturl) throw new TypeError('file-upload %s posturl=/api/xxxx/xxxx required', scope.attrs);
309 console.log ("UploadFile Loaded");