UNPKG

vue-cloud-upload

Version:

基于vue+elementui的通用云上传组件,无缝衔接腾讯云,华为云,火山云等桶平台,开箱即用

206 lines (205 loc) 6.55 kB
var g = Object.defineProperty; var w = Object.getOwnPropertySymbols; var x = Object.prototype.hasOwnProperty, K = Object.prototype.propertyIsEnumerable; var S = (n, e, t) => e in n ? g(n, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : n[e] = t, T = (n, e) => { for (var t in e || (e = {})) x.call(e, t) && S(n, t, e[t]); if (w) for (var t of w(e)) K.call(e, t) && S(n, t, e[t]); return n; }; var m = (n, e, t) => S(n, typeof e != "symbol" ? e + "" : e, t); var d = (n, e, t) => new Promise((i, s) => { var l = (r) => { try { a(t.next(r)); } catch (o) { s(o); } }, c = (r) => { try { a(t.throw(r)); } catch (o) { s(o); } }, a = (r) => r.done ? i(r.value) : Promise.resolve(r.value).then(l, c); a((t = t.apply(n, e)).next()); }); const u = class u { constructor(e) { // 外部传入的COS对象 m(this, "cosClient", null); m(this, "tempCredential", null); m(this, "secretId", null); m(this, "secretKey", null); e && e.secretId && e.secretKey && (this.secretId = e.secretId, this.secretKey = e.secretKey), this.initClient(e); } // 设置外部COS对象的静态方法 static setExternalCOS(e) { this.externalCOS = e; } static getInstance(e) { return this.instance || e && (e.secretId && e.secretKey || e.getTempCredential && typeof e.getTempCredential == "function") && (this.instance = new u(e)), this.instance; } static destroyInstance() { this.instance = null, this.cosClient = null, this.tempCredential = null, localStorage.removeItem("cosCredential"); } initClient(e) { return d(this, null, function* () { let t = u.externalCOS; if (this.secretId && this.secretKey) this.cosClient = new t({ SecretId: this.secretId, SecretKey: this.secretKey }); else { const i = e.getTempCredential; yield this.getTempCredential(i), this.cosClient = new t({ getAuthorization: (s, l) => d(this, null, function* () { try { (!this.tempCredential || this.isCredentialExpired()) && (yield this.getTempCredential(i)), l({ TmpSecretId: this.tempCredential.tmpSecretId, TmpSecretKey: this.tempCredential.tmpSecretKey, SecurityToken: this.tempCredential.sessionToken, StartTime: this.tempCredential.startTime, ExpiredTime: this.tempCredential.expiredTime }); } catch (c) { console.error("获取临时凭证失败:", c); } }) }); } }); } getTempCredential(e) { return d(this, null, function* () { let t = localStorage.getItem("cosCredential"); if (t && (t = JSON.parse(t)), t && !this.isCredentialExpired(t)) { this.tempCredential = t; return; } try { const i = yield e(); i && typeof i == "object" && (this.tempCredential = { tmpSecretId: i.credentials.tmpSecretId, tmpSecretKey: i.credentials.tmpSecretKey, sessionToken: i.credentials.sessionToken, startTime: i.startTime, expiredTime: i.expiredTime }, localStorage.setItem( "cosCredential", JSON.stringify(this.tempCredential) )); } catch (i) { throw new Error("获取临时凭证失败: " + i.message); } }); } isCredentialExpired(e = this.tempCredential) { return e ? Math.floor(Date.now() / 1e3) >= e.expiredTime - 60 : !0; } // 单文件上传 uploadFile({ bucket: e, region: t, key: i, file: s, sliceSize: l, chunkSize: c, onProgress: a }) { return new Promise((r, o) => d(this, null, function* () { const y = yield this.isBucketPublicRead({ bucket: e, region: t }); this.cosClient.uploadFile( { Bucket: e, Region: t, Key: i, Body: s, SliceSize: l, // 触发分块上传的阈值,超过5MB使用分块上传,默认 1MB,非必须 ChunkSize: c, // 分块大小,默认 1MB,非必须 onProgress: (p) => { a && typeof a == "function" && a(p.percent); } }, (p, h) => { if (p) o(p); else if (y) { const C = h.Location.startsWith("https://") ? h.Location : "https://" + h.Location; r(T({ url: C, key: i, name: s.name }, h)); } else this.cosClient.getObjectUrl( { Bucket: e, Region: t, Key: i, Sign: !0 }, function(C, f) { C ? console.log(C) : r(T({ url: f.Url, key: i, name: s.name }, h)); } ); } ); })); } // 图片加水印 addWatermark(l) { return d(this, arguments, function* ({ bucket: e, region: t, key: i, watermarkText: s }) { const c = { Bucket: e, Region: t, Key: i, PicOperations: JSON.stringify({ is_pic_info: 1, rules: [ { fileid: `watermark_${i}`, rule: `watermark/2/text/${encodeURIComponent( s )}/fill/IzAwMDAwMA/fontsize/20/dissolve/50/gravity/southeast/dx/20/dy/20` } ] }) }; return new Promise((a, r) => { this.cosClient.ciPutObjectFromLocalFile(c, (o, y) => { o ? r(o) : a(y); }); }); }); } // 获取存储桶ACL(访问控制列表) getBucketAcl(i) { return d(this, arguments, function* ({ bucket: e, region: t }) { return new Promise((s, l) => { this.cosClient.getBucketAcl( { Bucket: e, Region: t }, (c, a) => { c ? l(c) : s(a); } ); }); }); } // 检查存储桶是否为公有读 isBucketPublicRead(i) { return d(this, arguments, function* ({ bucket: e, region: t }) { try { return (yield this.getBucketAcl({ bucket: e, region: t })).Grants.some( (l) => l.Grantee.URI === "http://cam.qcloud.com/groups/global/AllUsers" && l.Permission === "READ" ); } catch (s) { return console.error("检查存储桶权限失败:", s), !0; } }); } }; m(u, "instance", null), m(u, "externalCOS", null); let I = u; export { I as default };