UNPKG

vue-cloud-upload

Version:

基于vue+elementui的通用云上传组件,无缝衔接腾讯云,华为云,阿里云等对象存储平台,开箱即用

275 lines (274 loc) 8.78 kB
var x = Object.defineProperty; var D = Object.getOwnPropertySymbols; var K = Object.prototype.hasOwnProperty, k = Object.prototype.propertyIsEnumerable; var g = (l, e, t) => e in l ? x(l, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : l[e] = t, I = (l, e) => { for (var t in e || (e = {})) K.call(e, t) && g(l, t, e[t]); if (D) for (var t of D(e)) k.call(e, t) && g(l, t, e[t]); return l; }; var m = (l, e, t) => g(l, typeof e != "symbol" ? e + "" : e, t); var o = (l, e, t) => new Promise((i, s) => { var a = (c) => { try { n(t.next(c)); } catch (d) { s(d); } }, r = (c) => { try { n(t.throw(c)); } catch (d) { s(d); } }, n = (c) => c.done ? i(c.value) : Promise.resolve(c.value).then(a, r); n((t = t.apply(l, e)).next()); }); const C = class C { constructor(e) { // 外部传入的COS对象 m(this, "cosClient", null); m(this, "tempCredential", null); m(this, "secretId", null); m(this, "secretKey", null); m(this, "initPromise", null); e && e.secretId && e.secretKey && (this.secretId = e.secretId, this.secretKey = e.secretKey), this.initPromise = this.initClient(e); } // 添加初始化Promise // 设置外部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 C(e)), this.instance; } // 添加等待初始化完成的方法 static waitForInitialization() { return o(this, null, function* () { this.instance && this.instance.initPromise && (yield this.instance.initPromise); }); } static destroyInstance() { this.instance = null, this.cosClient = null, this.tempCredential = null, localStorage.removeItem("cosCredential"); try { const e = localStorage.getItem("cosCacheDatas"); if (e) { let t = JSON.parse(e); const i = Date.now() - 14400 * 60 * 1e3; t = t.filter((s) => s.createTime ? s.createTime > i : !1), localStorage.setItem("cosCacheDatas", JSON.stringify(t)); } } catch (e) { console.error("清理过期断点记录失败:", e); } } initClient(e) { return o(this, null, function* () { let t = C.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, a) => o(this, null, function* () { try { (!this.tempCredential || this.isCredentialExpired()) && (yield this.getTempCredential(i)), a({ TmpSecretId: this.tempCredential.tmpSecretId, TmpSecretKey: this.tempCredential.tmpSecretKey, SecurityToken: this.tempCredential.sessionToken, StartTime: this.tempCredential.startTime, ExpiredTime: this.tempCredential.expiredTime }); } catch (r) { console.error("获取临时凭证失败:", r); } }) }); } }); } getTempCredential(e) { return o(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: a, chunkSize: r, onProgress: n }) { return new Promise((c, d) => o(this, null, function* () { const S = yield this.isBucketPublicRead({ bucket: e, region: t }), f = `${s.name}-${s.size}-${s.lastModified}`; let h = ""; if (s.size < a) h = i; else { const u = this.getCosDataByKey(f); u ? h = u.name : (h = i, this.setCosData(f, i)); } this.cosClient.uploadFile( { Bucket: e, Region: t, Key: h, Body: s, SliceSize: a, // 触发分块上传的阈值,超过5MB使用分块上传,默认 1MB,非必须 ChunkSize: r, // 分块大小,默认 1MB,非必须 onProgress: (u) => { n && typeof n == "function" && n(u.percent); } }, (u, p) => { if (u) d(u); else if (this.delCosData(f), S) { const y = p.Location.startsWith("https://") ? p.Location : "https://" + p.Location; c(I({ url: y, key: h, name: s.name }, p)); } else this.cosClient.getObjectUrl( { Bucket: e, Region: t, Key: h, Sign: !0 }, function(y, T) { y ? console.log(y) : c(I({ url: T.Url, key: h, name: s.name }, p)); } ); } ); })); } // 通过文件key获取地址 getFileUrlByKey({ bucket: e, region: t, key: i }) { return new Promise((s, a) => o(this, null, function* () { let r = yield this.isBucketPublicRead({ bucket: e, region: t }); try { let n = { Bucket: e, Region: t, Key: i, Sign: !0 }; r && (n.Sign = !1); const c = yield this.cosClient.getObjectUrl(n); s(c); } catch (n) { a("获取文件地址失败!"); } })); } getCosDataByKey(e) { let t = []; const i = localStorage.getItem("cosCacheDatas"); i && (t = JSON.parse(i)); const s = t.findIndex((a) => a.key == e); return s >= 0 ? t[s] : null; } setCosData(e, t) { let i = []; const s = localStorage.getItem("cosCacheDatas"); s && (i = JSON.parse(s)); const a = i.findIndex((r) => r.key == e); a >= 0 ? i[a].name = t : i.push({ key: e, name: t, createTime: Date.now() }), localStorage.setItem("cosCacheDatas", JSON.stringify(i)); } delCosData(e) { let t = []; const i = localStorage.getItem("cosCacheDatas"); i && (t = JSON.parse(i)); const s = t.findIndex((a) => a.key == e); s >= 0 && (t.splice(s, 1), localStorage.setItem("cosCacheDatas", JSON.stringify(t))); } // 图片加水印 addWatermark(a) { return o(this, arguments, function* ({ bucket: e, region: t, key: i, watermarkText: s }) { const r = { 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((n, c) => { this.cosClient.ciPutObjectFromLocalFile(r, (d, S) => { d ? c(d) : n(S); }); }); }); } // 获取存储桶ACL(访问控制列表) getBucketAcl(i) { return o(this, arguments, function* ({ bucket: e, region: t }) { return new Promise((s, a) => { this.cosClient.getBucketAcl( { Bucket: e, Region: t }, (r, n) => { r ? a(r) : s(n); } ); }); }); } // 检查存储桶是否为公有读 isBucketPublicRead(i) { return o(this, arguments, function* ({ bucket: e, region: t }) { try { return (yield this.getBucketAcl({ bucket: e, region: t })).Grants.some( (a) => a.Grantee.URI === "http://cam.qcloud.com/groups/global/AllUsers" && a.Permission === "READ" ); } catch (s) { return console.error("检查存储桶权限失败:", s), !0; } }); } }; m(C, "instance", null), m(C, "externalCOS", null); let w = C; export { w as default };