/* Socket IO File Upload Client-Side Library * Copyright (C) 2015 Shane Carr and others * Released under the X11 License * For more information, visit: https://github.com/vote539/socketio-file-upload */ (function(g,d,f){"function"===typeof define&&define.amd?define([],f):"object"===typeof module&&module.exports?module.exports=f():g[d]=f()})(this,"SocketIOFileUpload",function(){return function(g){var d=this;if(!window.File||!window.FileReader)throw Error("Socket.IO File Upload: Browser Not Supported");window.siofu_global||(window.siofu_global={instances:0,downloads:0});var f={},p={},y={},q={},h={};d.fileInputElementId="siofu_input_"+siofu_global.instances++;d.resetFileInputs=!0;d.useText=!1;d.serializedOctets= !1;d.useBuffer=!0;d.chunkSize=102400;var t=function(a,b){var c=document.createEvent("Event");c.initEvent(a,!1,!1);for(var v in b)b.hasOwnProperty(v)&&(c[v]=b[v]);return d.dispatchEvent(c)},r=[],e=function(a,b,c,d){a.addEventListener(b,c,d);r.push(arguments)},z=function(a,b,c,d){a.removeEventListener&&a.removeEventListener(b,c,d)},B=function(){for(var a=r.length-1;0<=a;a--)z.apply(this,r[a]);r=[]},C=function(a){if(null!==d.maxFileSize&&a.size>d.maxFileSize)t("error",{file:a,message:"Attempt by client to upload file exceeding the maximum file size", code:1});else if(t("start",{file:a})){var b=new FileReader,c=siofu_global.downloads++,f=!1,h=d.useText,w=0,r;b._realReader&&(b=b._realReader);p[c]=a;var u={id:c},x=d.chunkSize;if(x>=a.size||0>=x)x=a.size;var n=function(){if(!u.abort){var c=a.slice(w,Math.min(w+x,a.size));h?b.readAsText(c):b.readAsArrayBuffer(c)}},A=function(e){if(!u.abort){var v=Math.min(w+x,a.size);a:{var p=w;e=e.target.result;var n=!1;if(!h)try{var l=new Uint8Array(e);if(d.serializedOctets)e=l;else if(d.useBuffer)e=l.buffer;else{var n= !0,m,q=l.buffer.byteLength,k="";for(m=0;m<q;m+=3)k+="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[l[m]>>2],k+="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(l[m]&3)<<4|l[m+1]>>4],k+="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(l[m+1]&15)<<2|l[m+2]>>6],k+="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[l[m+2]&63];2===q%3?k=k.substring(0,k.length-1)+"=":1===q%3&&(k=k.substring(0,k.length-2)+"==");e=k}}catch(E){g.emit("siofu_done", {id:c,interrupt:!0});break a}g.emit("siofu_progress",{id:c,size:a.size,start:p,end:v,content:e,base64:n})}t("progress",{file:a,bytesLoaded:v,name:r});w+=x;w>=a.size&&(g.emit("siofu_done",{id:c}),t("load",{file:a,reader:b,name:r}),f=!0)}};e(b,"load",A);e(b,"error",function(){g.emit("siofu_done",{id:c,interrupt:!0});z(b,"load",A)});e(b,"abort",function(){g.emit("siofu_done",{id:c,interrupt:!0});z(b,"load",A)});g.emit("siofu_start",{name:a.name,mtime:a.lastModified,meta:a.meta,size:a.size,encoding:h? "text":"octet",id:c});q[c]=function(a){r=a;n()};y[c]=function(){f||n()};return u}},u=function(a){if(0!==a.length){for(var b=0;b<a.length;b++)a[b].meta||(a[b].meta={});if(t("choose",{files:a}))for(b=0;b<a.length;b++){var c=C(a[b]);h[c.id]=c}}},n=function(a){var b=a.target.files||a.dataTransfer.files;a.preventDefault();u(b);if(d.resetFileInputs){try{a.target.value=""}catch(D){}if(a.target.value){var b=document.createElement("form"),c=a.target.parentNode,e=a.target.nextSibling;b.appendChild(a.target); b.reset();c.insertBefore(a.target,e)}}};this.submitFiles=function(a){a&&u(a)};this.listenOnSubmit=function(a,b){b.files&&e(a,"click",function(){u(b.files)},!1)};this.listenOnArraySubmit=function(a,b){for(var c in b)this.listenOnSubmit(a,b[c])};this.listenOnInput=function(a){a.files&&e(a,"change",n,!1)};this.listenOnDrop=function(a){e(a,"dragover",function(a){a.preventDefault()},!1);e(a,"drop",n)};this.prompt=function(){var a;a=document.getElementById(d.fileInputElementId);a||(a=document.createElement("input"), a.setAttribute("type","file"),a.setAttribute("id",d.fileInputElementId),a.style.display="none",document.body.appendChild(a));e(a,"change",n,!1);var b=document.createEvent("MouseEvents");b.initMouseEvent("click",!0,!0,window,0,0,0,0,0,!1,!1,!1,!1,0,null);a.dispatchEvent(b)};this.destroy=function(){B();var a=document.getElementById(d.fileInputElementId);a&&a.parentNode.removeChild(a);for(var b in h)h.hasOwnProperty(b)&&(h[b].abort=!0);h=q=p=f=null};this.addEventListener=function(a,b){f[a]||(f[a]=[]); f[a].push(b)};this.removeEventListener=function(a,b){if(!f[a])return!1;for(var c=0;c<f[a].length;c++)if(f[a][c]===b)return f[a].splice(c,1),!0;return!1};this.dispatchEvent=function(a){var b=f[a.type];if(!b)return!0;for(var c=!0,d=0;d<b.length;d++)!1===b[d](a)&&(c=!1);return c};e(g,"siofu_chunk",function(a){if(y[a.id])y[a.id]()});e(g,"siofu_ready",function(a){if(q[a.id])q[a.id](a.name)});e(g,"siofu_complete",function(a){p[a.id]&&t("complete",{file:p[a.id],detail:a.detail,success:a.success})});e(g, "siofu_error",function(a){p[a.id]&&(t("error",{file:p[a.id],message:a.message,code:0}),h&&(h[a.id].abort=!0))})}});