2020-10-20 11:43:11 +00:00
|
|
|
import fetch from "./fetch";
|
2020-11-08 20:24:30 +08:00
|
|
|
import CryptoJS from "crypto-js";
|
2020-10-20 11:43:11 +00:00
|
|
|
import OSS from "ali-oss";
|
|
|
|
import COS from "cos-js-sdk-v5";
|
|
|
|
import Buffer from "buffer-from";
|
|
|
|
import { v4 as uuidv4 } from "uuid";
|
2020-11-08 20:24:30 +08:00
|
|
|
import * as qiniu from "qiniu-js";
|
|
|
|
import { utf16to8, base64encode, safe64 } from "../assets/scripts/tokenTools";
|
2020-08-31 20:48:22 +08:00
|
|
|
|
2020-09-13 21:24:27 +08:00
|
|
|
const defaultConfig = {
|
2020-10-20 11:43:11 +00:00
|
|
|
username: "filess",
|
|
|
|
repo: "images",
|
|
|
|
branch: "master",
|
2020-09-13 21:24:27 +08:00
|
|
|
accessToken: [
|
2020-10-20 11:43:11 +00:00
|
|
|
"7715d7ca67b5d3837cfdoocsmde8c38421815aa423510af",
|
|
|
|
"c411415bf95dbe39625doocsmd5047ba9b7a2a6c9642abe",
|
|
|
|
"2821cd8819fa345c053doocsmdca86ac653f8bc20db1f1b",
|
|
|
|
"445f0dae46ef1f2a4d6doocsmdc797301e94797b4750a4c",
|
|
|
|
"cc1d0c1426d0fd0902bdoocsmdd2d7184b14da61b86ec46",
|
|
|
|
"b67e9d15cb6f910492fdoocsmdac6b44d379c953bb19eff",
|
|
|
|
"618c4dc2244ccbbc088doocsmd125d17fd31b7d06a50cf3",
|
|
|
|
"a4b581732e1c1507458doocsmdc5b223b27dae5e2e16a55",
|
|
|
|
],
|
|
|
|
};
|
2020-09-12 16:53:52 +08:00
|
|
|
|
2020-09-15 21:49:30 +08:00
|
|
|
function fileUpload(content, file) {
|
2020-10-20 11:43:11 +00:00
|
|
|
const imgHost = localStorage.getItem("imgHost");
|
|
|
|
!imgHost && localStorage.setItem("imgHost", "default");
|
2020-09-13 21:24:27 +08:00
|
|
|
switch (imgHost) {
|
2020-10-20 11:43:11 +00:00
|
|
|
case "aliOSS":
|
2020-09-16 20:59:19 +08:00
|
|
|
return aliOSSFileUpload(content, file.name);
|
2020-10-20 11:43:11 +00:00
|
|
|
case "txCOS":
|
2020-10-15 09:22:56 +08:00
|
|
|
return txCOSFileUpload(file);
|
2020-11-08 20:24:30 +08:00
|
|
|
case "qiniu":
|
|
|
|
return qiniuUpload(file);
|
2020-11-12 09:47:06 +08:00
|
|
|
case "gitee":
|
|
|
|
return giteeUpload(content, file.name);
|
2020-10-20 11:43:11 +00:00
|
|
|
case "github":
|
2020-09-12 16:53:52 +08:00
|
|
|
default:
|
2020-09-16 20:59:19 +08:00
|
|
|
return ghFileUpload(content, file.name);
|
2020-09-12 16:53:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-12 19:22:52 +08:00
|
|
|
function getDir() {
|
2020-09-13 21:24:27 +08:00
|
|
|
const date = new Date();
|
2020-10-20 11:43:11 +00:00
|
|
|
const dir =
|
|
|
|
date.getFullYear() +
|
|
|
|
"/" +
|
|
|
|
(date.getMonth() + 1).toString().padStart(2, "0") +
|
|
|
|
"/" +
|
|
|
|
date.getDate().toString().padStart(2, "0");
|
2020-11-12 19:22:52 +08:00
|
|
|
return dir;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getDateFilename(filename) {
|
|
|
|
const dateFilename =
|
|
|
|
new Date().getTime() + "-" + uuidv4() + "." + filename.split(".")[1];
|
|
|
|
return dateFilename;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getGitHubCommonConfig(username, repo, branch, token) {
|
|
|
|
const dir = getDir();
|
2020-09-13 21:24:27 +08:00
|
|
|
return {
|
2020-10-20 11:43:11 +00:00
|
|
|
method: "put",
|
2020-09-13 21:24:27 +08:00
|
|
|
headers: {
|
2020-10-20 11:43:11 +00:00
|
|
|
Authorization: "token " + token,
|
2020-09-13 21:24:27 +08:00
|
|
|
},
|
2020-09-16 20:59:19 +08:00
|
|
|
branch: branch,
|
2020-10-20 11:43:11 +00:00
|
|
|
url: `https://api.github.com/repos/${username}/${repo}/contents/${dir}/`,
|
2020-09-13 21:24:27 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-09-16 20:59:19 +08:00
|
|
|
function getDefaultConfig() {
|
2020-10-20 11:43:11 +00:00
|
|
|
const token = defaultConfig.accessToken[
|
|
|
|
Math.floor(Math.random() * defaultConfig.accessToken.length)
|
|
|
|
].replace("doocsmd", "");
|
|
|
|
return getGitHubCommonConfig(
|
|
|
|
defaultConfig.username,
|
|
|
|
defaultConfig.repo,
|
|
|
|
defaultConfig.branch,
|
|
|
|
token
|
|
|
|
);
|
2020-09-16 20:59:19 +08:00
|
|
|
}
|
|
|
|
|
2020-09-13 21:24:27 +08:00
|
|
|
function getGitHubConfig() {
|
|
|
|
const githubConfig = JSON.parse(localStorage.getItem("githubConfig"));
|
2020-10-20 11:43:11 +00:00
|
|
|
const repoUrl = githubConfig.repo
|
|
|
|
.replace("https://github.com/", "")
|
|
|
|
.replace("http://github.com/", "")
|
|
|
|
.replace("github.com/", "")
|
|
|
|
.split("/");
|
2020-09-13 21:24:27 +08:00
|
|
|
const username = repoUrl[0];
|
|
|
|
const repo = repoUrl[1];
|
2020-10-20 11:43:11 +00:00
|
|
|
return getGitHubCommonConfig(
|
|
|
|
username,
|
|
|
|
repo,
|
|
|
|
githubConfig.branch,
|
|
|
|
githubConfig.accessToken
|
|
|
|
);
|
2020-09-13 21:24:27 +08:00
|
|
|
}
|
|
|
|
|
2020-11-08 20:24:30 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-09-16 20:59:19 +08:00
|
|
|
async function ghFileUpload(content, filename) {
|
2020-10-20 11:43:11 +00:00
|
|
|
const isDefault = localStorage.getItem("imgHost") !== "github";
|
2020-09-13 21:24:27 +08:00
|
|
|
const config = isDefault ? getDefaultConfig() : getGitHubConfig();
|
2020-11-12 19:22:52 +08:00
|
|
|
const dateFilename = getDateFilename(filename);
|
2020-09-16 20:59:19 +08:00
|
|
|
const res = await fetch({
|
2020-09-13 21:24:27 +08:00
|
|
|
url: config.url + dateFilename,
|
|
|
|
method: config.method,
|
|
|
|
headers: config.headers,
|
2020-08-29 11:55:16 +08:00
|
|
|
data: {
|
2020-10-20 11:43:11 +00:00
|
|
|
branch: config.branch || "master",
|
2020-10-02 14:49:20 +08:00
|
|
|
message: `Upload by ${window.location.href}`,
|
2020-10-20 11:43:11 +00:00
|
|
|
content: content,
|
|
|
|
},
|
2020-09-12 16:53:52 +08:00
|
|
|
});
|
2020-10-20 11:43:11 +00:00
|
|
|
const githubResourceUrl = "raw.githubusercontent.com/filess/images/master/";
|
2020-10-20 19:59:47 +08:00
|
|
|
const cdnResourceUrl = "cdn.jsdelivr.net/gh/filess/images@master/";
|
2020-10-20 11:43:11 +00:00
|
|
|
return isDefault
|
|
|
|
? res.content.download_url.replace(githubResourceUrl, cdnResourceUrl)
|
|
|
|
: res.content.download_url;
|
2020-05-01 21:30:25 +08:00
|
|
|
}
|
|
|
|
|
2020-11-12 09:47:06 +08:00
|
|
|
async function giteeUpload(content, filename) {
|
|
|
|
const giteeConfig = JSON.parse(localStorage.getItem("giteeConfig"));
|
|
|
|
const repoUrl = giteeConfig.repo
|
|
|
|
.replace("https://gitee.com/", "")
|
|
|
|
.replace("http://gitee.com/", "")
|
|
|
|
.replace("gitee.com/", "")
|
|
|
|
.split("/");
|
|
|
|
const username = repoUrl[0];
|
|
|
|
const repo = repoUrl[1];
|
2020-11-12 19:22:52 +08:00
|
|
|
const dir = getDir();
|
|
|
|
const dateFilename = getDateFilename(filename);
|
2020-11-12 09:47:06 +08:00
|
|
|
const res = await fetch({
|
|
|
|
url: `https://gitee.com/api/v5/repos/${username}/${repo}/contents/${dir}/${dateFilename}`,
|
|
|
|
method: "POST",
|
|
|
|
data: {
|
|
|
|
access_token: giteeConfig.accessToken,
|
|
|
|
branch: giteeConfig.branch || "master",
|
|
|
|
content: content,
|
|
|
|
message: `Upload by ${window.location.href}`,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
return encodeURI(res.content.download_url);
|
|
|
|
}
|
|
|
|
|
2020-09-16 20:59:19 +08:00
|
|
|
async function aliOSSFileUpload(content, filename) {
|
2020-11-12 19:22:52 +08:00
|
|
|
const dateFilename = getDateFilename(filename);
|
2020-10-20 11:43:11 +00:00
|
|
|
const aliOSSConfig = JSON.parse(localStorage.getItem("aliOSSConfig"));
|
|
|
|
const buffer = Buffer(content, "base64");
|
2020-09-12 16:53:52 +08:00
|
|
|
try {
|
2020-10-20 11:43:11 +00:00
|
|
|
const dir = aliOSSConfig.path + "/" + dateFilename;
|
2020-09-13 21:24:27 +08:00
|
|
|
const client = new OSS({
|
|
|
|
region: aliOSSConfig.region,
|
|
|
|
bucket: aliOSSConfig.bucket,
|
|
|
|
accessKeyId: aliOSSConfig.accessKeyId,
|
2020-10-20 11:43:11 +00:00
|
|
|
accessKeySecret: aliOSSConfig.accessKeySecret,
|
2020-09-13 21:24:27 +08:00
|
|
|
});
|
2020-09-16 20:59:19 +08:00
|
|
|
const res = await client.put(dir, buffer);
|
2020-10-20 11:43:11 +00:00
|
|
|
return aliOSSConfig.cdnHost == ""
|
|
|
|
? res.url
|
|
|
|
: aliOSSConfig.cdnHost +
|
|
|
|
"/" +
|
|
|
|
(aliOSSConfig.path == "" ? dateFilename : dir);
|
2020-09-12 16:53:52 +08:00
|
|
|
} catch (e) {
|
|
|
|
return Promise.reject(e);
|
|
|
|
}
|
|
|
|
}
|
2020-05-01 21:30:25 +08:00
|
|
|
|
2020-09-16 20:59:19 +08:00
|
|
|
async function txCOSFileUpload(file) {
|
2020-11-12 19:22:52 +08:00
|
|
|
const dateFilename = getDateFilename(file.name);
|
2020-10-20 11:43:11 +00:00
|
|
|
const txCOSConfig = JSON.parse(localStorage.getItem("txCOSConfig"));
|
2020-09-15 21:49:30 +08:00
|
|
|
const cos = new COS({
|
|
|
|
SecretId: txCOSConfig.secretId,
|
2020-10-20 11:43:11 +00:00
|
|
|
SecretKey: txCOSConfig.secretKey,
|
2020-09-15 21:49:30 +08:00
|
|
|
});
|
|
|
|
return new Promise((resolve, reject) => {
|
2020-10-20 11:43:11 +00:00
|
|
|
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) {
|
|
|
|
// if cdnHost exists
|
|
|
|
resolve(
|
|
|
|
txCOSConfig.path != ""
|
|
|
|
? txCOSConfig.cdnHost +
|
|
|
|
"/" +
|
|
|
|
txCOSConfig.path +
|
|
|
|
"/" +
|
|
|
|
dateFilename
|
|
|
|
: txCOSConfig.cdnHost + "/" + dateFilename
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
// if cdnHost not exists
|
|
|
|
reject(data.Location);
|
|
|
|
}
|
2020-09-15 21:49:30 +08:00
|
|
|
}
|
2020-10-20 11:43:11 +00:00
|
|
|
);
|
|
|
|
});
|
2020-09-15 21:49:30 +08:00
|
|
|
}
|
|
|
|
|
2020-11-08 20:24:30 +08:00
|
|
|
async function qiniuUpload(file) {
|
|
|
|
const qiniuConfig = JSON.parse(localStorage.getItem("qiniuConfig"));
|
|
|
|
const putPolicy = {
|
|
|
|
scope: qiniuConfig.bucket,
|
|
|
|
deadline: Math.trunc(new Date().getTime() / 1000) + 3600,
|
|
|
|
};
|
|
|
|
const token = getQiniuToken(
|
|
|
|
qiniuConfig.accessKey,
|
|
|
|
qiniuConfig.secretKey,
|
|
|
|
putPolicy
|
|
|
|
);
|
|
|
|
const dir = qiniuConfig.path ? qiniuConfig.path + "/" : "";
|
2020-11-12 19:22:52 +08:00
|
|
|
const dateFilename = dir + getDateFilename(file.name);
|
2020-11-08 20:24:30 +08:00
|
|
|
const config = {
|
|
|
|
region: qiniuConfig.region,
|
|
|
|
};
|
|
|
|
const observable = qiniu.upload(file, dateFilename, token, {}, config);
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
observable.subscribe({
|
|
|
|
next: (result) => {
|
|
|
|
console.log(result);
|
|
|
|
},
|
|
|
|
error: (err) => {
|
|
|
|
reject(err.message);
|
|
|
|
},
|
|
|
|
complete: (result) => {
|
|
|
|
resolve(qiniuConfig.domain + "/" + result.key);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-05-01 21:30:25 +08:00
|
|
|
export default {
|
2020-10-20 11:43:11 +00:00
|
|
|
fileUpload,
|
2020-08-31 20:48:22 +08:00
|
|
|
};
|