vue-cloud-upload
Version:
基于vue+elementui的通用云上传组件,无缝衔接腾讯云,华为云,火山云等桶平台,开箱即用
237 lines (236 loc) • 8.37 kB
JavaScript
var E = Object.defineProperty;
var T = Object.getOwnPropertySymbols;
var O = Object.prototype.hasOwnProperty, $ = Object.prototype.propertyIsEnumerable;
var K = (l, e, t) => e in l ? E(l, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : l[e] = t, g = (l, e) => {
for (var t in e || (e = {}))
O.call(e, t) && K(l, t, e[t]);
if (T)
for (var t of T(e))
$.call(e, t) && K(l, t, e[t]);
return l;
};
var m = (l, e, t) => K(l, typeof e != "symbol" ? e + "" : e, t);
var h = (l, e, t) => new Promise((r, y) => {
var b = (u) => {
try {
w(t.next(u));
} catch (s) {
y(s);
}
}, a = (u) => {
try {
w(t.throw(u));
} catch (s) {
y(s);
}
}, w = (u) => u.done ? r(u.value) : Promise.resolve(u.value).then(b, a);
w((t = t.apply(l, e)).next());
});
const C = class C {
constructor(e) {
// 外部传入的OBS对象
m(this, "obsClient", null);
m(this, "tempCredential", null);
m(this, "accessKeyId", null);
m(this, "secretAccessKey", null);
m(this, "securityToken", null);
m(this, "server", null);
e && e.server && (this.server = e.server), e && e.accessKeyId && e.secretAccessKey && (this.accessKeyId = e.accessKeyId, this.secretAccessKey = e.secretAccessKey), this.initClient(e);
}
// 设置外部OBS对象的静态方法
static setExternalOBS(e) {
this.externalOBS = e;
}
static getInstance(e) {
return this.instance || e && (e.accessKeyId && e.secretAccessKey && !e.getTempCredential || e.getTempCredential && typeof e.getTempCredential == "function") && (this.instance = new C(e)), this.instance;
}
static destroyInstance() {
this.instance = null, this.obsClient = null, this.tempCredential = null, localStorage.removeItem("obsCpDatas"), localStorage.removeItem("obsCredential");
}
initClient(e) {
return h(this, null, function* () {
let t = C.externalOBS;
if (this.accessKeyId && this.secretAccessKey && !e.getTempCredential)
this.obsClient = new t({
access_key_id: this.accessKeyId,
secret_access_key: this.secretAccessKey,
server: this.server
});
else if (e.getTempCredential)
yield this.getTempCredential(e.getTempCredential), this.obsClient = new t({
access_key_id: this.tempCredential.accessKeyId,
secret_access_key: this.tempCredential.secretAccessKey,
security_token: this.tempCredential.securityToken,
server: this.server
});
else
throw new Error(
"缺少必要的认证信息:需要提供永久密钥或getTempCredential函数"
);
this.obsClient.initLog({
level: "warn"
// 配置日志级别
});
});
}
getTempCredential(e) {
return h(this, null, function* () {
let t = localStorage.getItem("obsCredential");
if (t && (t = JSON.parse(t)), t && !this.isCredentialExpired(t)) {
this.tempCredential = t;
return;
}
try {
const r = yield e();
r && typeof r == "object" && (this.tempCredential = {
accessKeyId: r.credential.access,
secretAccessKey: r.credential.secret,
securityToken: r.credential.securitytoken,
expiredTime: r.credential.expires_at
}, localStorage.setItem(
"obsCredential",
JSON.stringify(this.tempCredential)
));
} catch (r) {
throw new Error("获取临时凭证失败: " + r.message);
}
});
}
isCredentialExpired(e = this.tempCredential) {
if (!e) return !0;
const t = Date.now(), r = Date.parse(e.expiredTime);
return t >= r - 60 * 1e3;
}
// 确保使用有效的凭证
ensureValidCredential(e) {
return h(this, null, function* () {
if (e && (!this.tempCredential || this.isCredentialExpired())) {
yield this.getTempCredential(e);
let t = C.externalOBS;
this.obsClient = new t({
access_key_id: this.tempCredential.accessKeyId,
secret_access_key: this.tempCredential.secretAccessKey,
security_token: this.tempCredential.securityToken,
server: this.server
});
}
});
}
// 单文件上传
uploadFile(u) {
return h(this, arguments, function* ({
bucket: e,
key: t,
file: r,
sliceSize: y,
chunkSize: b,
onProgress: a,
getTempCredential: w
}) {
if (yield this.ensureValidCredential(w), r.size < y)
try {
const s = yield this.obsClient.putObject({
Bucket: e,
Key: t,
SourceFile: r,
ProgressCallback: (d, o, I) => {
a && typeof a == "function" && a(d / o);
}
});
if (s.CommonMsg.Status < 300) {
const d = yield this.obsClient.getObject({
Bucket: e,
Key: t,
SaveByType: "file"
});
let o = "";
if (d.CommonMsg.Status < 300 && d.InterfaceResult)
o = d.InterfaceResult.Content.SignedUrl;
else
throw new Error(`附件url获取失败: ${d.CommonMsg.Code}`);
return g({
url: o,
key: t,
name: r.name
}, s);
} else
throw new Error(`上传失败: ${s.CommonMsg.Code}`);
} catch (s) {
throw new Error(`华为云OBS上传失败: ${s.message}`);
}
else
try {
let s = [];
const d = localStorage.getItem("obsCpDatas");
d && (s = JSON.parse(d));
const o = `${r.name}-${r.size}-${r.lastModified}`, I = s.findIndex((n) => n.key == o), v = I >= 0;
let i = v ? s[I].cp : null, S;
if (i ? (i.sourceFile = r, S = yield this.obsClient.uploadFile({
UploadCheckpoint: i,
ProgressCallback: (n, c, x) => {
a && typeof a == "function" && a(n / c);
},
EventCallback: function(n, c, x) {
if (n == "uploadPartSucceed") {
const f = s.findIndex((p) => p.key == o), k = i.parts.findIndex(
(p) => p.partNumber == c.partNumber
);
i.parts[k].isCompleted = !0, s[f].cp = i, localStorage.setItem("obsCpDatas", JSON.stringify(s));
}
}
})) : S = yield this.obsClient.uploadFile({
Bucket: e,
Key: t,
SourceFile: r,
PartSize: b,
ProgressCallback: (n, c, x) => {
a && typeof a == "function" && a(n / c);
},
ResumeCallback: function(n, c) {
console.log("记录断点:", c), i = c, v ? s[I].cp = i : (s.push({
key: o,
cp: i
}), localStorage.setItem("obsCpDatas", JSON.stringify(s)));
},
EventCallback: function(n, c, x) {
if (n == "uploadPartSucceed") {
const f = s.findIndex((p) => p.key == o), k = i.parts.findIndex(
(p) => p.partNumber == c.partNumber
);
i.parts[k].isCompleted = !0, s[f].cp = i, localStorage.setItem("obsCpDatas", JSON.stringify(s));
}
}
}), S.CommonMsg.Status < 300) {
const n = s.findIndex((c) => c.key == o);
return s.splice(n, 1), localStorage.setItem("obsCpDatas", JSON.stringify(s)), g({
key: t,
name: r.name
}, S);
} else
throw new Error(`上传失败: ${S.CommonMsg.Code}`);
} catch (s) {
throw new Error(`华为云OBS上传失败: ${s.message}`);
}
});
}
// 图片加水印(华为云OBS处理方式)
addWatermark(b) {
return h(this, arguments, function* ({ bucket: e, region: t, key: r, watermarkText: y }) {
try {
return {
processedUrl: `https://${e}.obs.${t}.myhuaweicloud.com/${r}?x-image-process=image/watermark,text_${encodeURIComponent(
y
)},color_FFFFFF,size_20,g_se,x_20,y_20`,
originalKey: r
};
} catch (a) {
throw new Error(`添加水印失败: ${a.message}`);
}
});
}
};
m(C, "instance", null), m(C, "externalOBS", null);
let _ = C;
export {
_ as default
};