@@ -5,14 +5,17 @@ import axios from "axios";
5
5
import OSS from "ali-oss" ;
6
6
7
7
import AliOSS from "../ImageHosting/AliOSS" ;
8
+ import QiniuOSS from "../ImageHosting/QiniuOSS" ;
8
9
9
- import { toBlob , dateFormat } from "../../utils/helper" ;
10
+ import { toBlob , getOSSName , axiosMdnice } from "../../utils/helper" ;
10
11
import {
11
12
SM_MS_PROXY ,
12
13
ALIOSS_IMAGE_HOSTING ,
14
+ QINIUOSS_IMAGE_HOSTING ,
13
15
IMAGE_HOSTING_TYPE ,
14
16
IMAGE_HOSTING_TYPE_OPTIONS
15
17
} from "../../utils/constant" ;
18
+ import * as qiniu from "qiniu-js" ;
16
19
17
20
const Dragger = Upload . Dragger ;
18
21
const { TabPane } = Tabs ;
@@ -69,10 +72,21 @@ class ImageDialog extends Component {
69
72
}
70
73
// 使用阿里云图床
71
74
if ( this . props . imageHosting . type === "阿里云" ) {
72
- const config = JSON . parse (
75
+ const configAli = JSON . parse (
73
76
window . localStorage . getItem ( ALIOSS_IMAGE_HOSTING )
74
77
) ;
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 ) ;
76
90
}
77
91
// 使用SM.MS图床
78
92
else {
@@ -95,6 +109,7 @@ class ImageDialog extends Component {
95
109
} ;
96
110
} ;
97
111
112
+ // SM.MS存储上传
98
113
smmsUpload = (
99
114
formData ,
100
115
file ,
@@ -137,6 +152,7 @@ class ImageDialog extends Component {
137
152
} ) ;
138
153
} ;
139
154
155
+ // 阿里云对象存储上传
140
156
aliOSSUpload = ( config , file , onSuccess , onError ) => {
141
157
const base64Reader = new FileReader ( ) ;
142
158
base64Reader . readAsDataURL ( file ) ;
@@ -162,27 +178,20 @@ class ImageDialog extends Component {
162
178
} ;
163
179
} ;
164
180
181
+ // 阿里云对象存储上传
165
182
aliOSSPutObject = ( config , file , value , onSuccess , onError ) => {
166
183
let client ;
167
- try {
184
+ try {
168
185
client = new OSS ( config ) ;
169
- } catch ( error ) {
186
+ } catch ( error ) {
170
187
message . error ( "OSS配置错误,请根据文档检查配置项" ) ;
171
188
return ;
172
189
}
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
+
184
193
client
185
- . put ( key , value )
194
+ . put ( OSSName , value )
186
195
. then ( response => {
187
196
const names = file . name . split ( "." ) ;
188
197
names . pop ( ) ;
@@ -200,6 +209,113 @@ class ImageDialog extends Component {
200
209
} ) ;
201
210
} ;
202
211
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
+
203
319
typeChange = type => {
204
320
this . props . imageHosting . setType ( type ) ;
205
321
localStorage . setItem ( IMAGE_HOSTING_TYPE , type ) ;
@@ -243,9 +359,12 @@ class ImageDialog extends Component {
243
359
</ p >
244
360
</ Dragger >
245
361
</ TabPane >
246
- < TabPane tab = "阿里云OSS " key = "2" >
362
+ < TabPane tab = "阿里云 " key = "2" >
247
363
< AliOSS />
248
364
</ TabPane >
365
+ < TabPane tab = "七牛云" key = "3" >
366
+ < QiniuOSS />
367
+ </ TabPane >
249
368
</ Tabs >
250
369
</ Modal >
251
370
) ;
0 commit comments