refactor: update file api

This commit is contained in:
yanglbme 2020-11-30 23:48:22 +08:00
parent 7d1cea4280
commit d97b153a58

View File

@ -31,21 +31,22 @@ function fileUpload(content, file) {
function getDir() { function getDir() {
const date = new Date(); const date = new Date();
const dir = const year = date.getFullYear();
date.getFullYear() + const month = (date.getMonth() + 1).toString().padStart(2, "0");
"/" + const day = date.getDate().toString().padStart(2, "0");
(date.getMonth() + 1).toString().padStart(2, "0") + return `${year}/${month}/${day}`;
"/" +
date.getDate().toString().padStart(2, "0");
return dir;
} }
function getDateFilename(filename) { function getDateFilename(filename) {
const dateFilename = const currentTimestamp = new Date().getTime();
new Date().getTime() + "-" + uuidv4() + "." + filename.split(".")[1]; const fileSuffix = filename.split(".")[1];
return dateFilename; return `${currentTimestamp}-${uuidv4()}.${fileSuffix}`;
} }
//-----------------------------------------------------------------------
// GitHub File Upload
//-----------------------------------------------------------------------
function getGitHubCommonConfig(username, repo, branch, token) { function getGitHubCommonConfig(username, repo, branch, token) {
const dir = getDir(); const dir = getDir();
return { return {
@ -77,9 +78,10 @@ function getGitHubDefaultConfig() {
], ],
}; };
const token = defaultConfig.accessToken[ const randIndex = Math.floor(
Math.floor(Math.random() * defaultConfig.accessToken.length) Math.random() * defaultConfig.accessToken.length
].replace("doocsmd", ""); );
const token = defaultConfig.accessToken[randIndex].replace("doocsmd", "");
return getGitHubCommonConfig( return getGitHubCommonConfig(
defaultConfig.username, defaultConfig.username,
defaultConfig.repo, defaultConfig.repo,
@ -105,41 +107,6 @@ function getGitHubConfig() {
); );
} }
function getGiteeDefaultConfig() {
const defaultConfig = {
username: "filesss",
repo: `img${Math.floor(Math.random() * 10)}`,
branch: "main",
accessToken: [
"ed5fc9866bd6c2fdoocsmddd433f806fd2f399c",
"5448ffebbbf1151doocsmdc4e337cf814fc8a62",
"25b05efd2557ca2doocsmd75b5c0835e3395911",
"11628c7a5aef015doocsmd2eeff9fb9566f0458",
"cb2f5145ed938dedoocsmdbd063b4ed244eecf8",
"d8c0b57500672c1doocsmd55f48b866b5ebcd98",
"78c56eadb88e453doocsmd43ddd95753351771a",
"03e1a688003948fdoocsmda16fcf41e6f03f1f0",
],
};
const token = defaultConfig.accessToken[
Math.floor(Math.random() * defaultConfig.accessToken.length)
].replace("doocsmd", "");
return {
repo: `gitee.com/${defaultConfig.username}/${defaultConfig.repo}`,
branch: defaultConfig.branch,
accessToken: token,
};
}
function getQiniuToken(accessKey, secretKey, putPolicy) {
const policy = JSON.stringify(putPolicy);
const encoded = base64encode(utf16to8(policy));
const hash = CryptoJS.HmacSHA1(encoded, secretKey);
const encodedSigned = hash.toString(CryptoJS.enc.Base64);
return `${accessKey}:${safe64(encodedSigned)}:${encoded}`;
}
async function ghFileUpload(content, filename) { async function ghFileUpload(content, filename) {
const isDefault = localStorage.getItem("imgHost") !== "github"; const isDefault = localStorage.getItem("imgHost") !== "github";
const config = isDefault ? getGitHubDefaultConfig() : getGitHubConfig(); const config = isDefault ? getGitHubDefaultConfig() : getGitHubConfig();
@ -164,6 +131,38 @@ async function ghFileUpload(content, filename) {
: res.content.download_url; : res.content.download_url;
} }
//-----------------------------------------------------------------------
// Gitee File Upload
//-----------------------------------------------------------------------
function getGiteeDefaultConfig() {
const defaultConfig = {
username: "filesss",
repo: `img${Math.floor(Math.random() * 10)}`,
branch: "main",
accessToken: [
"ed5fc9866bd6c2fdoocsmddd433f806fd2f399c",
"5448ffebbbf1151doocsmdc4e337cf814fc8a62",
"25b05efd2557ca2doocsmd75b5c0835e3395911",
"11628c7a5aef015doocsmd2eeff9fb9566f0458",
"cb2f5145ed938dedoocsmdbd063b4ed244eecf8",
"d8c0b57500672c1doocsmd55f48b866b5ebcd98",
"78c56eadb88e453doocsmd43ddd95753351771a",
"03e1a688003948fdoocsmda16fcf41e6f03f1f0",
],
};
const randIndex = Math.floor(
Math.random() * defaultConfig.accessToken.length
);
const token = defaultConfig.accessToken[randIndex].replace("doocsmd", "");
return {
repo: `gitee.com/${defaultConfig.username}/${defaultConfig.repo}`,
branch: defaultConfig.branch,
accessToken: token,
};
}
async function giteeUpload(content, filename) { async function giteeUpload(content, filename) {
const isDefault = localStorage.getItem("imgHost") == "default"; const isDefault = localStorage.getItem("imgHost") == "default";
const giteeConfig = isDefault const giteeConfig = isDefault
@ -191,59 +190,16 @@ async function giteeUpload(content, filename) {
return encodeURI(res.content.download_url); return encodeURI(res.content.download_url);
} }
async function aliOSSFileUpload(content, filename) { //-----------------------------------------------------------------------
const dateFilename = getDateFilename(filename); // Qiniu File Upload
const aliOSSConfig = JSON.parse(localStorage.getItem("aliOSSConfig")); //-----------------------------------------------------------------------
const buffer = Buffer(content, "base64");
try {
const dir = `${aliOSSConfig.path}/${dateFilename}`;
const client = new OSS({
region: aliOSSConfig.region,
bucket: aliOSSConfig.bucket,
accessKeyId: aliOSSConfig.accessKeyId,
accessKeySecret: aliOSSConfig.accessKeySecret,
});
const res = await client.put(dir, buffer);
return aliOSSConfig.cdnHost == ""
? res.url
: `${aliOSSConfig.cdnHost}/${
aliOSSConfig.path == "" ? dateFilename : dir
}`;
} catch (e) {
return Promise.reject(e);
}
}
async function txCOSFileUpload(file) { function getQiniuToken(accessKey, secretKey, putPolicy) {
const dateFilename = getDateFilename(file.name); const policy = JSON.stringify(putPolicy);
const txCOSConfig = JSON.parse(localStorage.getItem("txCOSConfig")); const encoded = base64encode(utf16to8(policy));
const cos = new COS({ const hash = CryptoJS.HmacSHA1(encoded, secretKey);
SecretId: txCOSConfig.secretId, const encodedSigned = hash.toString(CryptoJS.enc.Base64);
SecretKey: txCOSConfig.secretKey, return `${accessKey}:${safe64(encodedSigned)}:${encoded}`;
});
return new Promise((resolve, reject) => {
cos.putObject(
{
Bucket: txCOSConfig.bucket,
Region: txCOSConfig.region,
Key: `${txCOSConfig.path}/${dateFilename}`,
Body: file,
},
function (err, data) {
if (err) {
reject(err);
} else if (txCOSConfig.cdnHost) {
resolve(
txCOSConfig.path != ""
? `${txCOSConfig.cdnHost}/${txCOSConfig.path}/${dateFilename}`
: `${txCOSConfig.cdnHost}/${dateFilename}`
);
} else {
resolve(`https://${data.Location}`);
}
}
);
});
} }
async function qiniuUpload(file) { async function qiniuUpload(file) {
@ -278,6 +234,69 @@ async function qiniuUpload(file) {
}); });
} }
//-----------------------------------------------------------------------
// AliOSS File Upload
//-----------------------------------------------------------------------
async function aliOSSFileUpload(content, filename) {
const dateFilename = getDateFilename(filename);
const aliOSSConfig = JSON.parse(localStorage.getItem("aliOSSConfig"));
const buffer = Buffer(content, "base64");
try {
const dir = `${aliOSSConfig.path}/${dateFilename}`;
const client = new OSS({
region: aliOSSConfig.region,
bucket: aliOSSConfig.bucket,
accessKeyId: aliOSSConfig.accessKeyId,
accessKeySecret: aliOSSConfig.accessKeySecret,
});
const res = await client.put(dir, buffer);
return aliOSSConfig.cdnHost == ""
? res.url
: `${aliOSSConfig.cdnHost}/${
aliOSSConfig.path == "" ? dateFilename : dir
}`;
} catch (e) {
return Promise.reject(e);
}
}
//-----------------------------------------------------------------------
// TxCOS File Upload
//-----------------------------------------------------------------------
async function txCOSFileUpload(file) {
const dateFilename = getDateFilename(file.name);
const txCOSConfig = JSON.parse(localStorage.getItem("txCOSConfig"));
const cos = new COS({
SecretId: txCOSConfig.secretId,
SecretKey: txCOSConfig.secretKey,
});
return new Promise((resolve, reject) => {
cos.putObject(
{
Bucket: txCOSConfig.bucket,
Region: txCOSConfig.region,
Key: `${txCOSConfig.path}/${dateFilename}`,
Body: file,
},
function (err, data) {
if (err) {
reject(err);
} else if (txCOSConfig.cdnHost) {
resolve(
txCOSConfig.path != ""
? `${txCOSConfig.cdnHost}/${txCOSConfig.path}/${dateFilename}`
: `${txCOSConfig.cdnHost}/${dateFilename}`
);
} else {
resolve(`https://${data.Location}`);
}
}
);
});
}
export default { export default {
fileUpload, fileUpload,
}; };