feat: update file api methods

This commit is contained in:
yanglbme 2020-11-12 19:22:52 +08:00
parent 73b0f2ca5a
commit 6645eb3cde
2 changed files with 26 additions and 31 deletions

View File

@ -45,13 +45,13 @@ Markdown 文档自动即时渲染为微信图文,让你不再为微信文章
## 目前支持哪些图床
| # | 图床 | 使用时是否需要配置 | 备注 |
| --- | ----------- | ------------------------------------------------------------------ | ---------------------------------------------------------------------------------------------------------------------- |
| --- | ----------------------------------------------- | ---------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------- |
| 1 | 默认 | 否 | - |
| 2 | Gitee | 配置 `Repo`、`Token` 参数 | 图片超过 1MB 无法正常展示 |
| 3 | GitHub | 配置 `Repo`、`Token` 参数 | [如何获取 GitHub token](https://docs.github.com/en/github/authenticating-to-github/creating-a-personal-access-token) |
| 4 | 阿里云 OSS | 配置 `AccessKey ID`、`AccessKey Secret`、`Bucket`、`Region` 参数 | [如何使用阿里云 OSS](https://help.aliyun.com/document_detail/31883.html) |
| 5 | 腾讯云 COS | 配置 `SecretId`、`SecretKey`、`Bucket`、`Region` 参数 | [如何使用腾讯云 COS](https://cloud.tencent.com/document/product/436/38484) |
| 6 | 七牛云 Kodo | 配置 `AccessKey`、`SecretKey`、`Bucket`、`Domain`、`Region` 参数 | [如何使用七牛云 Kodo](https://developer.qiniu.com/kodo) |
| 2 | [Gitee](https://gitee.com) | 配置 `Repo`、`Token` 参数 | 图片超过 1MB 无法正常展示 |
| 3 | [GitHub](https://github.com) | 配置 `Repo`、`Token` 参数 | [如何获取 GitHub token](https://docs.github.com/en/github/authenticating-to-github/creating-a-personal-access-token) |
| 4 | [阿里云](https://www.aliyun.com/product/oss) | 配置 `AccessKey ID`、`AccessKey Secret`、`Bucket`、`Region` 参数 | [如何使用阿里云 OSS](https://help.aliyun.com/document_detail/31883.html) |
| 5 | [腾讯云](https://cloud.tencent.com/act/pro/cos) | 配置 `SecretId`、`SecretKey`、`Bucket`、`Region` 参数 | [如何使用腾讯云 COS](https://cloud.tencent.com/document/product/436/38484) |
| 6 | [七牛云](https://www.qiniu.com/products/kodo) | 配置 `AccessKey`、`SecretKey`、`Bucket`、`Domain`、`Region` 参数 | [如何使用七牛云 Kodo](https://developer.qiniu.com/kodo) |
![select-and-change-color-theme](./public/assets/images/select-and-change-color-theme.gif)

View File

@ -41,7 +41,7 @@ function fileUpload(content, file) {
}
}
function getGitHubCommonConfig(username, repo, branch, token) {
function getDir() {
const date = new Date();
const dir =
date.getFullYear() +
@ -49,6 +49,17 @@ function getGitHubCommonConfig(username, repo, branch, token) {
(date.getMonth() + 1).toString().padStart(2, "0") +
"/" +
date.getDate().toString().padStart(2, "0");
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();
return {
method: "put",
headers: {
@ -99,8 +110,7 @@ function getQiniuToken(accessKey, secretKey, putPolicy) {
async function ghFileUpload(content, filename) {
const isDefault = localStorage.getItem("imgHost") !== "github";
const config = isDefault ? getDefaultConfig() : getGitHubConfig();
const dateFilename =
new Date().getTime() + "-" + uuidv4() + "." + filename.split(".")[1];
const dateFilename = getDateFilename(filename);
const res = await fetch({
url: config.url + dateFilename,
method: config.method,
@ -127,15 +137,8 @@ async function giteeUpload(content, filename) {
.split("/");
const username = repoUrl[0];
const repo = repoUrl[1];
const date = new Date();
const dir =
date.getFullYear() +
"/" +
(date.getMonth() + 1).toString().padStart(2, "0") +
"/" +
date.getDate().toString().padStart(2, "0");
const dateFilename =
new Date().getTime() + "-" + uuidv4() + "." + filename.split(".")[1];
const dir = getDir();
const dateFilename = getDateFilename(filename);
const res = await fetch({
url: `https://gitee.com/api/v5/repos/${username}/${repo}/contents/${dir}/${dateFilename}`,
method: "POST",
@ -150,8 +153,7 @@ async function giteeUpload(content, filename) {
}
async function aliOSSFileUpload(content, filename) {
const dateFilename =
new Date().getTime() + "-" + uuidv4() + "." + filename.split(".")[1];
const dateFilename = getDateFilename(filename);
const aliOSSConfig = JSON.parse(localStorage.getItem("aliOSSConfig"));
const buffer = Buffer(content, "base64");
try {
@ -174,8 +176,7 @@ async function aliOSSFileUpload(content, filename) {
}
async function txCOSFileUpload(file) {
const dateFilename =
new Date().getTime() + "-" + uuidv4() + "." + file.name.split(".")[1];
const dateFilename = getDateFilename(file.name);
const txCOSConfig = JSON.parse(localStorage.getItem("txCOSConfig"));
const cos = new COS({
SecretId: txCOSConfig.secretId,
@ -224,13 +225,7 @@ async function qiniuUpload(file) {
putPolicy
);
const dir = qiniuConfig.path ? qiniuConfig.path + "/" : "";
const dateFilename =
dir +
new Date().getTime() +
"-" +
uuidv4() +
"." +
file.name.split(".")[1];
const dateFilename = dir + getDateFilename(file.name);
const config = {
region: qiniuConfig.region,
};