From 6645eb3cdea7a29b7380ebc7048c714a805d8245 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Thu, 12 Nov 2020 19:22:52 +0800 Subject: [PATCH] feat: update file api methods --- README.md | 16 ++++++++-------- src/api/file.js | 41 ++++++++++++++++++----------------------- 2 files changed, 26 insertions(+), 31 deletions(-) diff --git a/README.md b/README.md index df61e76..ab6aeeb 100644 --- a/README.md +++ b/README.md @@ -44,14 +44,14 @@ 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) | +| # | 图床 | 使用时是否需要配置 | 备注 | +| --- | ----------------------------------------------- | ---------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------- | +| 1 | 默认 | 否 | - | +| 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) diff --git a/src/api/file.js b/src/api/file.js index 9798b0f..afe3491 100644 --- a/src/api/file.js +++ b/src/api/file.js @@ -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, };