alioss-web-uploader
Version:
Upload file or add object to Ali OSS in browser.
3 lines (2 loc) • 6.55 kB
JavaScript
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t=t||self).AliOSSWebUploader=e()}(this,(function(){"use strict";var t=function(){return(t=Object.assign||function(t){for(var e,n=1,r=arguments.length;r>n;n++)for(var o in e=arguments[n])Object.prototype.hasOwnProperty.call(e,o)&&(t[o]=e[o]);return t}).apply(this,arguments)};function e(t){return t.replace(/^\/+/,"")}function n(t){return unescape(encodeURIComponent(t))}function r(t,e){var n=(65535&t)+(65535&e);return(t>>16)+(e>>16)+(n>>16)<<16|65535&n}function o(t,e){return t<<e|t>>>32-e}function s(t,e){for(var n,r=e?"0123456789ABCDEF":"0123456789abcdef",o="",s=0,i=t.length;i>s;s+=1)n=t.charCodeAt(s),o+=r.charAt(n>>>4&15)+r.charAt(15&n);return o}function i(t){var e,n=32*t.length,r="";for(e=0;n>e;e+=8)r+=String.fromCharCode(t[e>>5]>>>24-e%32&255);return r}function a(t){var e,n=8*t.length,r=Array(t.length>>2),o=r.length;for(e=0;o>e;e+=1)r[e]=0;for(e=0;n>e;e+=8)r[e>>5]|=(255&t.charCodeAt(e/8))<<24-e%32;return r}function u(t,e){var n,r,o,s,i,a,u,c,h=e.length,f=[];for(s=(a=Array(Math.ceil(t.length/2))).length,n=0;s>n;n+=1)a[n]=t.charCodeAt(2*n)<<8|t.charCodeAt(2*n+1);for(;a.length>0;){for(i=[],o=0,n=0;a.length>n;n+=1)o=(o<<16)+a[n],o-=(r=Math.floor(o/h))*h,(i.length>0||r>0)&&(i[i.length]=r);f[f.length]=o,a=i}for(u="",n=f.length-1;n>=0;n--)u+=e.charAt(f[n]);for(c=Math.ceil(8*t.length/(Math.log(e.length)/Math.log(2))),n=u.length;c>n;n+=1)u=e[0]+u;return u}function c(t,e){void 0===e&&(e="=");var n,r,o,s="",i=t.length;for(n=0;i>n;n+=3)for(o=t.charCodeAt(n)<<16|(i>n+1?t.charCodeAt(n+1)<<8:0)|(i>n+2?t.charCodeAt(n+2):0),r=0;4>r;r+=1)s+=8*n+6*r>8*t.length?e:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".charAt(o>>>6*(3-r)&63);return s}function h(t){var e=t.responseText||t.response;if(!e)return e;try{return JSON.parse(e)}catch(t){return e}}function f(t,e){var n=new XMLHttpRequest;n.timeout=e.timeout||6e4,e.onProgress&&n.upload&&(n.upload.onprogress=function(t){t.percent=t.loaded/t.total*100,e.onProgress(t)}),n.onerror=function(t){e.onError(t)},n.onload=function(){if(200>n.status||n.status>=300)return e.onError(function(t,e,n){var r=n.response.match(/<Code>(.+)<\/Code>/),o=n.response.match(/<Message>(.+)<\/Message>/),s=e.method||"GET",i="";r&&r[1]&&(i+=r[1]+": "),o&&o[1]&&(i+=o[1]);var a=Error(i);return a.status=n.status,a.method=s,a.code=r&&r[1]||"",a.url=t,a}(t,e,n),h(n));var r={status:n.status,statusText:n.statusText,xhr:n,data:h(n)};e.onSuccess(r,n)},n.ontimeout=function(t){e.onError(Error("Request timeout, limit "+n.timeout+" ms."))},e.onAbort&&(n.onabort=e.onAbort),n.open(e.method||"get",t,!0),e.withCredentials&&"withCredentials"in n&&(n.withCredentials=!0);var r=e.headers||{};for(var o in null!==r["X-Requested-With"]&&n.setRequestHeader("X-Requested-With","XMLHttpRequest"),r)r.hasOwnProperty(o)&&null!==r[o]&&n.setRequestHeader(o,r[o]);return n.send(e.data||null),{abort:function(){n.abort()}}}var p=function(t){var e=!(!t||"boolean"!=typeof t.uppercase)&&t.uppercase,h=t&&"string"==typeof t.pad?t.pad:"=",f=!t||"boolean"!=typeof t.utf8||t.utf8;function p(t,e){var n,s,i,a,u,c,h,f,p=Array(80),l=1732584193,d=-271733879,v=-1732584194,b=271733878,m=-1009589776;for(t[e>>5]|=128<<24-e%32,t[15+(e+64>>9<<4)]=e,n=0;t.length>n;n+=16){for(a=l,u=d,c=v,h=b,f=m,s=0;80>s;s+=1)p[s]=16>s?t[n+s]:o(p[s-3]^p[s-8]^p[s-14]^p[s-16],1),i=r(r(o(l,5),g(s,d,v,b)),r(r(m,p[s]),y(s))),m=b,b=v,v=o(d,30),d=l,l=i;l=r(l,a),d=r(d,u),v=r(v,c),b=r(b,h),m=r(m,f)}return[l,d,v,b,m]}function l(t,e){return i(p(a(t=e?n(t):t),8*t.length))}function d(t,e){var r,o,s,u,c;for(t=f?n(t):t,e=f?n(e):e,(r=a(t)).length>16&&(r=p(r,8*t.length)),o=Array(16),s=Array(16),u=0;16>u;u+=1)o[u]=909522486^r[u],s[u]=1549556828^r[u];return c=p(o.concat(a(e)),512+8*e.length),i(p(s.concat(c),672))}function g(t,e,n,r){return 20>t?e&n|~e&r:40>t?e^n^r:60>t?e&n|e&r|n&r:e^n^r}function y(t){return 20>t?1518500249:40>t?1859775393:60>t?-1894007588:-899497514}this.hex=function(t){return s(l(t,f),e)},this.b64=function(t){return c(l(t,f),h)},this.any=function(t,e){return u(l(t,f),e)},this.raw=function(t){return l(t,f)},this.hex_hmac=function(t,e){return s(d(t,e))},this.b64_hmac=function(t,e){return c(d(t,e),h)},this.any_hmac=function(t,e,n){return u(d(t,e),n)},this.setUpperCase=function(t){return"boolean"==typeof t&&(e=t),this},this.setPad=function(t){return h=t||h,this},this.setUTF8=function(t){return"boolean"==typeof t&&(f=t),this}};return function(){function r(e){if(!(this instanceof r))return new r(e);var n=t(t({endpoint:null,region:"oss-cn-hangzhou",timeout:3e5,internal:!1,secure:void 0},e),{host:""});if(n.endpoint);else{if(!n.region||!n.bucket)throw Error("require endpoint or region/bucket in options");n.endpoint=n.bucket,n.internal&&(n.region+="-internal"),n.endpoint+="."+n.region+".aliyuncs.com"}n.host+="http"+(!0===n.secure||location&&"https:"===location.protocol?"s":"")+"://"+n.endpoint,this.opts=n}return r.prototype.postObject=function(r,o,s){var i;void 0===s&&(s={});var a=new FormData;Object.keys(s.headers||{}).forEach((function(t){a.append(t,""+s.headers[t])}));var u,h,l=e((s.dir||"").replace(/^(.+?)\/*$/,"$1/")+e(r));if(a.append("key",l),this.opts.accessKeyId&&this.opts.accessKeySecret){if("string"==typeof s.policy)i=s.policy;else{var d=t({expiration:new Date(+new Date+864e5).toISOString(),conditions:[{bucket:this.opts.bucket},{key:l},["content-length-range",0,1073741824]]},s.policy);i=c(n(JSON.stringify(d)))}var g=s.signature||(u=this.opts.accessKeySecret,h=i,(new p).b64_hmac(u,h));a.append("OSSAccessKeyId",this.opts.accessKeyId),a.append("policy",i),a.append("Signature",g),this.opts.stsToken&&a.append("x-oss-security-token",this.opts.stsToken)}var y=function(t,e){void 0===e&&(e=[]);var n={};return Object.keys(t).forEach((function(r){var o=r.toLowerCase();(/^x-oss-/.test(o)||e.indexOf(r)>-1)&&(n[o]=t[r])})),n}(s,["success_action_status","success_action_redirect"]);Object.keys(y).forEach((function(t){return a.append(t,y[t])})),a.append("file",o);var v=function(){},b=s.onSuccess||v,m={method:"POST",data:a,timeout:s.timeout||this.opts.timeout,onSuccess:function(t,e){return b(t,e)},onError:s.onError||function(t){console.error(t)},onAbort:s.onAbort||v};return s.onProgress&&(m.onProgress=s.onProgress),f(this.opts.host,m)},r.prototype.generateObjectUrl=function(t,n){return n?"/"!==n[n.length-1]&&(n+="/"):n=this.opts.host+"/",n+function(t){return encodeURIComponent(t).replace(/%2F/g,"/")}(e(t))},r}()}));
//# sourceMappingURL=alioss-web-uploader.min.js.map