Skip to content

Commit 0b546e3

Browse files
authored
Merge pull request #35 from doocs/feat_qiniu-upload_20201107
feat: support qiniu kudo
2 parents 5ae6fd4 + 41b4be1 commit 0b546e3

File tree

5 files changed

+437
-1
lines changed

5 files changed

+437
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ Markdown 文档自动即时渲染为微信图文,让你不再为微信文章
4949
| 2 | GitHub 图床 | 配置 `Repo``Token` 参数 | [如何获取 GitHub token?](https://docs.github.com/en/github/authenticating-to-github/creating-a-personal-access-token) |
5050
| 3 | 阿里云 OSS | 配置 `AccessKey ID``AccessKey Secret``Bucket``Region` 等参数 | [如何使用阿里云 OSS?](https://help.aliyun.com/document_detail/31883.html) |
5151
| 4 | 腾讯云 COS | 配置 `SecretId``SecretKey``Bucket``Region` 等参数 | [如何使用腾讯云 COS?](https://cloud.tencent.com/document/product/436/38484) |
52+
| 5 | 七牛云 Kudo | 配置 `AccessKey``SecretKey``Bucket``Domain``Region` 等参数 | [如何使用七牛云 Kodo?](https://cloud.tencent.com/document/product/436/38484) |
5253

5354
![select-and-change-color-theme](./public/assets/images/select-and-change-color-theme.gif)
5455

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "vue-md",
33
"author": "doocs",
4-
"version": "1.4.1",
4+
"version": "1.4.2",
55
"private": true,
66
"homepage": "https://doocs.gitee.io/md",
77
"scripts": {
@@ -17,12 +17,14 @@
1717
"codemirror": "^5.50.2",
1818
"core-js": "^3.4.4",
1919
"cos-js-sdk-v5": "^0.5.27",
20+
"crypto-js": "^4.0.0",
2021
"element-ui": "^2.13.0",
2122
"jquery": "^3.4.1",
2223
"juice": "^6.0.0",
2324
"marked": "^0.8.0",
2425
"prettier": "^2.0.5",
2526
"prettify": "^0.1.7",
27+
"qiniu-js": "^3.1.2",
2628
"uuid": "^8.3.0",
2729
"vue": "^2.6.10",
2830
"vue-router": "^3.1.3",

src/api/file.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import fetch from "./fetch";
2+
import CryptoJS from "crypto-js";
23
import OSS from "ali-oss";
34
import COS from "cos-js-sdk-v5";
45
import Buffer from "buffer-from";
56
import { v4 as uuidv4 } from "uuid";
7+
import * as qiniu from "qiniu-js";
8+
import { utf16to8, base64encode, safe64 } from "../assets/scripts/tokenTools";
69

710
const defaultConfig = {
811
username: "filess",
@@ -28,6 +31,8 @@ function fileUpload(content, file) {
2831
return aliOSSFileUpload(content, file.name);
2932
case "txCOS":
3033
return txCOSFileUpload(file);
34+
case "qiniu":
35+
return qiniuUpload(file);
3136
case "github":
3237
default:
3338
return ghFileUpload(content, file.name);
@@ -81,6 +86,14 @@ function getGitHubConfig() {
8186
);
8287
}
8388

89+
function getQiniuToken(accessKey, secretKey, putPolicy) {
90+
const policy = JSON.stringify(putPolicy);
91+
const encoded = base64encode(utf16to8(policy));
92+
const hash = CryptoJS.HmacSHA1(encoded, secretKey);
93+
const encodedSigned = hash.toString(CryptoJS.enc.Base64);
94+
return accessKey + ":" + safe64(encodedSigned) + ":" + encoded;
95+
}
96+
8497
async function ghFileUpload(content, filename) {
8598
const isDefault = localStorage.getItem("imgHost") !== "github";
8699
const config = isDefault ? getDefaultConfig() : getGitHubConfig();
@@ -166,6 +179,44 @@ async function txCOSFileUpload(file) {
166179
});
167180
}
168181

182+
async function qiniuUpload(file) {
183+
const qiniuConfig = JSON.parse(localStorage.getItem("qiniuConfig"));
184+
const putPolicy = {
185+
scope: qiniuConfig.bucket,
186+
deadline: Math.trunc(new Date().getTime() / 1000) + 3600,
187+
};
188+
const token = getQiniuToken(
189+
qiniuConfig.accessKey,
190+
qiniuConfig.secretKey,
191+
putPolicy
192+
);
193+
const dir = qiniuConfig.path ? qiniuConfig.path + "/" : "";
194+
const dateFilename =
195+
dir +
196+
new Date().getTime() +
197+
"-" +
198+
uuidv4() +
199+
"." +
200+
file.name.split(".")[1];
201+
const config = {
202+
region: qiniuConfig.region,
203+
};
204+
const observable = qiniu.upload(file, dateFilename, token, {}, config);
205+
return new Promise((resolve, reject) => {
206+
observable.subscribe({
207+
next: (result) => {
208+
console.log(result);
209+
},
210+
error: (err) => {
211+
reject(err.message);
212+
},
213+
complete: (result) => {
214+
resolve(qiniuConfig.domain + "/" + result.key);
215+
},
216+
});
217+
});
218+
}
219+
169220
export default {
170221
fileUpload,
171222
};

0 commit comments

Comments
 (0)