Skip to content

Commit bd27378

Browse files
committed
支持七牛云
1 parent c7c9311 commit bd27378

File tree

11 files changed

+317
-36
lines changed

11 files changed

+317
-36
lines changed

.github/ISSUE_TEMPLATE/bug_report.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,21 @@ assignees: guanpengchn
77

88
---
99

10-
**运行环境: **
10+
**运行环境:**
1111

1212
|操作系统|浏览器|
1313
|---|---|
1414
|macOS|Chrome|
1515

1616
**Markdown文本内容:**
1717

18-
例如:默认文本
18+
例如:
19+
20+
```md
21+
- 默认文本1
22+
- 默认文本2
23+
- 默认文本3
24+
```
1925

2026
**错误详情:**
2127

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
v1.1.5 发布日期:2019-09-01
2+
- 支持自定义「七牛云」做图床
3+
- 提供mdnice自建免费图床,仅支持临时使用,一天后删除
4+
- 示例请重置后查看「第10条」
5+
16
v1.1.4 发布日期:2019-08-31
27
- 解决公式处理bug
38
- 添加移动端提示页

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
"postcss-loader": "3.0.0",
5858
"postcss-preset-env": "6.5.0",
5959
"postcss-safe-parser": "4.0.1",
60+
"qiniu-js": "^2.5.4",
6061
"react": "^16.7.0",
6162
"react-app-polyfill": "^0.2.1",
6263
"react-dev-utils": "^7.0.3",

src/component/Copy.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ import React, { Component } from "react";
22
import juice from "juice";
33
import { observer, inject } from "mobx-react";
44
import { Button, message, ConfigProvider } from "antd";
5-
import axios from "axios";
65

76
import {
87
BASIC_THEME_ID,
98
CODE_THEME_ID,
109
MARKDOWN_THEME_ID
1110
} from "../utils/constant";
1211

12+
import { axiosMdnice } from "../utils/helper";
13+
1314
@inject("content")
1415
@inject("navbar")
1516
@inject("imageHosting")
@@ -55,12 +56,12 @@ class Copy extends Component {
5556
let formula = math.split("$$")[1];
5657
formula = encodeURI(formula.replace(/\s/g, "&space;"));
5758
const ip = window.returnCitySN.cip.replace(/\./g, "-");
58-
const url = `https://math.mdnice.com/${ip}/type/png/scale/${this.scale}/math/${formula}`;
59+
const url = `/${ip}/type/png/scale/${this.scale}/math/${formula}`;
5960
urlArr.push(url);
6061
}
6162

6263
// 使用promise并行发请求,增快公式转换速度
63-
const promiseArr = urlArr.map(url => axios.get(url));
64+
const promiseArr = urlArr.map(url => axiosMdnice.get(url));
6465

6566
const resultArr = await Promise.all(promiseArr);
6667
resultArr.forEach((result, index) => {
@@ -106,11 +107,11 @@ class Copy extends Component {
106107
let formula = math.split("$")[1];
107108
formula = encodeURI(formula.replace(/\s/g, "&space;"));
108109
const ip = window.returnCitySN.cip.replace(/\./g, "-");
109-
const url = `https://math.mdnice.com/${ip}/type/png/scale/${this.scale}/math/${formula}`;
110+
const url = `/${ip}/type/png/scale/${this.scale}/math/${formula}`;
110111
urlArr.push(url);
111112
}
112113
// 使用promise并行发请求,增快公式转换速度
113-
const promiseArr = urlArr.map(url => axios.get(url));
114+
const promiseArr = urlArr.map(url => axiosMdnice.get(url));
114115

115116
const resultArr = await Promise.all(promiseArr);
116117
resultArr.forEach((result, index) => {
@@ -143,8 +144,8 @@ class Copy extends Component {
143144
// 拷贝流程 块级公式 => 行内公式 => 其他
144145
copy = async () => {
145146
try {
146-
axios.get(
147-
`https://math.mdnice.com/${window.returnCitySN.cip}/${window.returnCitySN.cname}`
147+
axiosMdnice.get(
148+
`/${window.returnCitySN.cip}/${window.returnCitySN.cname}`
148149
);
149150
this.setState({ loading: true });
150151
const flagBlock = await this.solveBlockMath();

src/component/Dialog/ImageDialog.js

Lines changed: 137 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,17 @@ import axios from "axios";
55
import OSS from "ali-oss";
66

77
import AliOSS from "../ImageHosting/AliOSS";
8+
import QiniuOSS from "../ImageHosting/QiniuOSS";
89

9-
import { toBlob, dateFormat } from "../../utils/helper";
10+
import { toBlob, getOSSName, axiosMdnice } from "../../utils/helper";
1011
import {
1112
SM_MS_PROXY,
1213
ALIOSS_IMAGE_HOSTING,
14+
QINIUOSS_IMAGE_HOSTING,
1315
IMAGE_HOSTING_TYPE,
1416
IMAGE_HOSTING_TYPE_OPTIONS
1517
} from "../../utils/constant";
18+
import * as qiniu from "qiniu-js";
1619

1720
const Dragger = Upload.Dragger;
1821
const { TabPane } = Tabs;
@@ -69,10 +72,21 @@ class ImageDialog extends Component {
6972
}
7073
// 使用阿里云图床
7174
if (this.props.imageHosting.type === "阿里云") {
72-
const config = JSON.parse(
75+
const configAli = JSON.parse(
7376
window.localStorage.getItem(ALIOSS_IMAGE_HOSTING)
7477
);
75-
this.aliOSSUpload(config, file, onSuccess, onError);
78+
this.aliOSSUpload(configAli, file, onSuccess, onError);
79+
}
80+
// 使用七牛云图床
81+
else if (this.props.imageHosting.type === "七牛云") {
82+
const configQiniu = JSON.parse(
83+
window.localStorage.getItem(QINIUOSS_IMAGE_HOSTING)
84+
);
85+
this.qiniuOSSUpload(configQiniu, file, onSuccess, onError, onProgress);
86+
}
87+
// 使用七牛云免费图床
88+
else if (this.props.imageHosting.type === "mdnice") {
89+
this.qiniuFreeUpload(formData, file, onSuccess, onError);
7690
}
7791
// 使用SM.MS图床
7892
else {
@@ -95,6 +109,7 @@ class ImageDialog extends Component {
95109
};
96110
};
97111

112+
// SM.MS存储上传
98113
smmsUpload = (
99114
formData,
100115
file,
@@ -137,6 +152,7 @@ class ImageDialog extends Component {
137152
});
138153
};
139154

155+
// 阿里云对象存储上传
140156
aliOSSUpload = (config, file, onSuccess, onError) => {
141157
const base64Reader = new FileReader();
142158
base64Reader.readAsDataURL(file);
@@ -162,27 +178,20 @@ class ImageDialog extends Component {
162178
};
163179
};
164180

181+
// 阿里云对象存储上传
165182
aliOSSPutObject = (config, file, value, onSuccess, onError) => {
166183
let client;
167-
try{
184+
try {
168185
client = new OSS(config);
169-
} catch(error) {
186+
} catch (error) {
170187
message.error("OSS配置错误,请根据文档检查配置项");
171188
return;
172189
}
173-
const names = file.name.split(".");
174-
let key = "";
175-
if (names.length > 1) {
176-
const suffix = names.pop();
177-
key = `${names.join(".")}_${dateFormat(
178-
new Date(),
179-
"yyyyMMddhhmmss"
180-
)}.${suffix}`;
181-
} else {
182-
key = file.name + "_" + dateFormat(new Date(), "yyyyMMddhhmmss");
183-
}
190+
191+
const OSSName = getOSSName(file.name);
192+
184193
client
185-
.put(key, value)
194+
.put(OSSName, value)
186195
.then(response => {
187196
const names = file.name.split(".");
188197
names.pop();
@@ -200,6 +209,113 @@ class ImageDialog extends Component {
200209
});
201210
};
202211

212+
// 七牛云对象存储上传
213+
qiniuOSSUpload = async (config, file, onSuccess, onError, onProgress) => {
214+
try {
215+
const result = await axiosMdnice.get(
216+
`/qiniu/${config.bucket}/${config.accessKey}/${config.secretKey}`
217+
);
218+
const token = result.data;
219+
220+
const base64Reader = new FileReader();
221+
base64Reader.readAsDataURL(file);
222+
base64Reader.onload = e => {
223+
const urlData = e.target.result;
224+
const base64 = urlData.split(",").pop();
225+
const fileType = urlData
226+
.split(";")
227+
.shift()
228+
.split(":")
229+
.pop();
230+
231+
// base64转blob
232+
const blob = toBlob(base64, fileType);
233+
234+
const conf = {
235+
useCdnDomain: true,
236+
region: qiniu.region[config.region] // 区域
237+
};
238+
239+
const putExtra = {
240+
fname: "",
241+
params: {},
242+
mimeType: [] || null
243+
};
244+
245+
const OSSName = getOSSName(file.name);
246+
247+
// 这里第一个参数的形式是blob
248+
const observable = qiniu.upload(blob, OSSName, token, putExtra, conf);
249+
250+
// 上传成功后回调
251+
const complete = response => {
252+
console.log(response);
253+
const names = file.name.split(".");
254+
names.pop();
255+
const filename = names.join(".");
256+
const image = {
257+
filename, // 名字不变并且去掉后缀
258+
url: `http://qiniu.mdnice.com/${response.key}`
259+
};
260+
this.images.push(image);
261+
onSuccess(response);
262+
};
263+
264+
// 上传过程回调
265+
const next = response => {
266+
console.log(response);
267+
onProgress(
268+
{
269+
percent: parseInt(Math.round(response.total.percent.toFixed(2)))
270+
},
271+
file
272+
);
273+
};
274+
275+
// 上传错误回调
276+
const error = err => {
277+
onError(err, err.toString());
278+
};
279+
280+
const observer = {
281+
next,
282+
error,
283+
complete
284+
};
285+
// 注册observer 对象
286+
observable.subscribe(observer);
287+
};
288+
} catch (err) {
289+
onError(err, err.toString());
290+
}
291+
};
292+
293+
// 七牛云免费上传图床
294+
qiniuFreeUpload = async (formData, file, onSuccess, onError) => {
295+
try {
296+
formData.append("file", file);
297+
const config = {
298+
headers: { "Content-Type": "multipart/form-data" }
299+
};
300+
const result = await axiosMdnice.post(
301+
`/qiniuFree`,
302+
formData,
303+
config
304+
);
305+
const names = file.name.split(".");
306+
names.pop();
307+
const filename = names.join(".");
308+
const image = {
309+
filename, // 名字不变并且去掉后缀
310+
url: result.data
311+
};
312+
this.images.push(image);
313+
onSuccess(result);
314+
} catch (err) {
315+
onError(err, err.toString());
316+
}
317+
};
318+
203319
typeChange = type => {
204320
this.props.imageHosting.setType(type);
205321
localStorage.setItem(IMAGE_HOSTING_TYPE, type);
@@ -243,9 +359,12 @@ class ImageDialog extends Component {
243359
</p>
244360
</Dragger>
245361
</TabPane>
246-
<TabPane tab="阿里云OSS" key="2">
362+
<TabPane tab="阿里云" key="2">
247363
<AliOSS />
248364
</TabPane>
365+
<TabPane tab="七牛云" key="3">
366+
<QiniuOSS />
367+
</TabPane>
249368
</Tabs>
250369
</Modal>
251370
);

0 commit comments

Comments
 (0)