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 // Update slider during Upload
48 xmlReq.upload.onprogress = function (event) {
49 var progress = Math.round(event.lengthComputable ? event.loaded * 100 / event.total : 0);
50 if (scope.slider) scope.slider.setValue (progress);
53 // Upload is finish let's notify controler callback
54 xmlReq.onload = function () {
55 elem.addClass ("success");
56 elem.removeClass ("error");
58 status : xmlReq.status,
59 headers: xmlReq.getAllResponseHeaders()
61 scope.callback (response);
64 xmlReq.onerror = function () {
65 elem.addClass ("error");
66 elem.removeClass ("success");
68 status : xmlReq.status,
69 headers: xmlReq.getAllResponseHeaders()
71 scope.callback (response);
74 xmlReq.onabort = function () {
75 elem.addClass ("error");
76 elem.removeClass ("success");
78 status : xmlReq.status,
79 headers: xmlReq.getAllResponseHeaders()
81 scope.callback (response);
84 for (var i = 0; i < files.length; i++) {
86 if (!file.type.match(scope.mimetype)) {
90 console.log ("Selected file=" + file.name + " size="+ file.size/1024 + " Type="+ file.type);
92 // File to upload is too big
93 if (file.size > scope.maxsize*1024) {
94 scope.thumbnail = scope.istoobig; // warning if image path is wrong nothing happen
95 scope.$apply('thumbnail'); // we short-circuit Angular resync Image
99 // This is not an uploadable file
100 if(isNaN(file.size)) {
101 scope.thumbnail = scope.isnotvalid;
102 scope.$apply('thumbnail');
106 scope.Basename= file.name.split('/').reverse()[0];
107 scope.imgElem[0].file = file;
109 // If File is an image let display it now
111 var reader = new FileReader();
112 reader.readAsArrayBuffer(file);
113 reader.onload = function (target) {
114 var status = thumbnailCB (target);
115 //if (status) xform.append(scope.name, file, file.name);
118 // if everything is OK let's add file to xform
119 xform.append(scope.name, file, file.name);
123 // everything looks OK let's Post it
124 xmlReq.open("POST", posturl , true);
128 angular.module('UploadFiles',['ConfigApp', 'ModalNotification', 'RangeSlider'])
130 .directive('uploadImage', function(ConfigApp, JQemu, Notification) {
131 function mymethods(scope, elem, attrs) {
133 // get widget image handle from template
134 scope.imgElem = elem.find('img');
135 scope.inputElem = elem.find('input');
137 // Image was ckick let's simulate an input (file) click
138 scope.imgClicked = function () {
139 scope.inputElem[0].click(); // Warning Angular TriggerEvent does not work!!!
142 // Slider control handle registration after creation
143 scope.SliderInitCB=function (slider) {
144 scope.slider= slider;
147 // Upload is delegated to a shared function
148 scope.UpLoadFile=function (files) {
149 var readerCB = function (upload) {
150 // scope.thumbnail = upload.target.result;
151 scope.imgElem[0].src = window.URL.createObjectURL(new Blob([upload.target.result], {type: "image"}));
152 return true; // true activates post
154 var posturl = attrs.posturl + "?token=" + ConfigApp.session.token;
155 new LoadFileSvc (scope, elem, posturl, files, readerCB);
158 // Initiallize default values from attributes values
159 scope.name= attrs.name || 'avatar';
160 scope.category= attrs.category || 'image';
161 scope.mimetype= (attrs.accept || 'image') + '/*';
162 scope.maxsize= attrs.maxsize || 100; // default max size 100KB
163 scope.regexp = new RegExp (attrs.accept+ '.*','i');
165 if (attrs.thumbnail) scope.thumbnail= ConfigApp.paths[scope.category] + attrs.thumbnail;
166 else scope.thumbnail=ConfigApp.paths[scope.category] + 'tux-bzh.png';
168 if (attrs.thumbnail) scope.isnotvalid= ConfigApp.paths[scope.category] + attrs.isnotvalid;
169 else scope.isnotvalid=ConfigApp.paths[scope.category] + 'isnotvalid.png';
171 if (attrs.istoobig) scope.istoobig= ConfigApp.paths[scope.category] + attrs.istoobig;
172 else scope.istoobig=ConfigApp.paths[scope.category] + 'istoobig.png';
173 scope.noslider = attrs.noslider || false;
175 if (!attrs.posturl) throw new TypeError('file-upload %s posturl=/api/xxxx/xxxx required', scope.attrs);
187 .directive('uploadAudio', function(ConfigApp, JQemu, Notification) {
188 function mymethods(scope, elem, attrs) {
190 // get widget image handle from template
191 scope.imgElem = elem.find('img');
192 scope.inputElem = elem.find('input');
194 // Image was ckick let's simulate an input (file) click
195 scope.imgClicked = function () {
196 scope.inputElem[0].click(); // Warning Angular TriggerEvent does not work!!!
199 // Slider control handle registration after creation
200 scope.SliderInitCB=function (slider) {
201 scope.slider= slider;
204 // Upload is delegated to a shared function
205 scope.UpLoadFile=function (files) {
206 var posturl = attrs.posturl + "?token=" + ConfigApp.session.token;
207 new LoadFileSvc (scope, elem, posturl, files, false);
210 // Initiallize default values from attributes values
211 scope.name= attrs.name || 'audio';
212 scope.category= attrs.category || 'audio';
213 scope.mimetype= (attrs.accept || 'audio') + '/*';
214 scope.maxsize= attrs.maxsize || 10000; // default max size 10MB
215 scope.regexp = new RegExp (attrs.accept+ '.*','i');
217 if (attrs.thumbnail) scope.thumbnail= ConfigApp.paths[scope.category] + attrs.thumbnail;
218 else scope.thumbnail=ConfigApp.paths[scope.category] + 'upload-music.png';
220 if (attrs.thumbnail) scope.isnotvalid= ConfigApp.paths[scope.category] + attrs.isnotvalid;
221 else scope.isnotvalid=ConfigApp.paths[scope.category] + 'isnotvalid.png';
223 if (attrs.istoobig) scope.istoobig= ConfigApp.paths[scope.category] + attrs.istoobig;
224 else scope.istoobig=ConfigApp.paths[scope.category] + 'istoobig.png';
225 scope.noslider = attrs.noslider || false;
227 if (!attrs.posturl) throw new TypeError('file-upload %s posturl=/api/xxxx/xxxx required', scope.attrs);
240 .directive('uploadAppli', function(ConfigApp, JQemu, Notification) {
241 function mymethods(scope, elem, attrs) {
243 // get widget image handle from template
244 scope.imgElem = elem.find('img');
245 scope.inputElem = elem.find('input');
247 // Image was ckick let's simulate an input (file) click
248 scope.imgClicked = function () {
249 scope.inputElem[0].click(); // Warning Angular TriggerEvent does not work!!!
252 // Slider control handle registration after creation
253 scope.SliderInitCB=function (slider) {
254 scope.slider= slider;
257 // Upload is delegated to a shared function
258 scope.UpLoadFile=function (files) {
260 var readerCB = function (upload) {
261 var zipapp = new JSZip(upload.target.result);
262 var thumbnail = zipapp.file("afa-pkg/thumbnail.jpg");
264 // Check is we have a thumbnail within loaded Zipfile
266 console.log ("This is not a valid Application Framework APP");
267 scope.thumbnail=ConfigApp.paths[scope.category] + 'isnotvalid.png';
268 scope.$apply('thumbnail'); // we short-circuit Angular resync Image
269 return false; // do not post zip on binder
271 scope.imgElem[0].src = window.URL.createObjectURL(new Blob([thumbnail.asArrayBuffer()], {type: "image"}));
272 return true; // true activates post
274 var posturl = attrs.posturl + "?token=" + ConfigApp.session.token;
275 new LoadFileSvc (scope, elem, posturl, files, readerCB);
278 // Initiallize default values from attributes values
279 scope.name= attrs.name || 'appli';
280 scope.category= attrs.category || 'appli';
281 scope.mimetype= (attrs.accept || '.zip');
282 scope.maxsize= attrs.maxsize || 100000; // default max size 100MB
283 scope.regexp = new RegExp (attrs.accept+ '.*','i');
285 if (attrs.thumbnail) scope.thumbnail= ConfigApp.paths[scope.category] + attrs.thumbnail;
286 else scope.thumbnail=ConfigApp.paths[scope.category] + 'upload-appli.png';
288 if (attrs.thumbnail) scope.isnotvalid= ConfigApp.paths[scope.category] + attrs.isnotvalid;
289 else scope.isnotvalid=ConfigApp.paths[scope.category] + 'isnotvalid.png';
291 if (attrs.istoobig) scope.istoobig= ConfigApp.paths[scope.category] + attrs.istoobig;
292 else scope.istoobig=ConfigApp.paths[scope.category] + 'istoobig.png';
293 scope.noslider = attrs.noslider || false;
295 if (!attrs.posturl) throw new TypeError('file-upload %s posturl=/api/xxxx/xxxx required', scope.attrs);
308 console.log ("UploadFile Loaded");