1
+ import request from 'request-promise'
1
2
import * as img2Base64 from './img2base64'
2
3
import db from '../../datastore/index'
3
4
import { Notification , clipboard } from 'electron'
4
- import { Wrapper as OSS } from 'ali-oss '
5
+ import crypto from 'crypto '
5
6
6
- let client
7
+ // generate OSS signature
8
+ const generateSignature = ( fileName ) => {
9
+ const options = db . read ( ) . get ( 'picBed.aliyun' ) . value ( )
10
+ const date = new Date ( ) . toGMTString ( )
11
+ const signString = `PUT\n\n\n${ date } \n/${ options . bucket } /${ options . path } ${ fileName } `
12
+
13
+ const signature = crypto . createHmac ( 'sha1' , options . accessKeySecret ) . update ( signString ) . digest ( 'base64' )
14
+ return `OSS ${ options . accessKeyId } :${ signature } `
15
+ }
7
16
8
- // generate OSS Options
9
- const generateOSSOptions = ( ) => {
17
+ const postOptions = ( fileName , signature , imgBase64 ) => {
10
18
const options = db . read ( ) . get ( 'picBed.aliyun' ) . value ( )
11
- client = new OSS ( {
12
- region : `${ options . area } ` ,
13
- accessKeyId : `${ options . accessKeyId } ` ,
14
- accessKeySecret : `${ options . accessKeySecret } ` ,
15
- bucket : `${ options . bucket } ` ,
16
- secure : true
17
- } )
18
- return client
19
+ return {
20
+ method : 'PUT' ,
21
+ url : `https://${ options . bucket } .${ options . area } .aliyuncs.com/${ encodeURI ( options . path ) } ${ encodeURI ( fileName ) } ` ,
22
+ headers : {
23
+ Host : `${ options . bucket } .${ options . area } .aliyuncs.com` ,
24
+ Authorization : signature ,
25
+ Date : new Date ( ) . toGMTString ( )
26
+ } ,
27
+ body : Buffer . from ( imgBase64 , 'base64' ) ,
28
+ resolveWithFullResponse : true
29
+ }
19
30
}
20
31
21
32
const aliYunUpload = async ( img , type , webContents ) => {
@@ -27,15 +38,17 @@ const aliYunUpload = async (img, type, webContents) => {
27
38
const customUrl = aliYunOptions . customUrl
28
39
const path = aliYunOptions . path
29
40
const length = imgList . length
30
- generateOSSOptions ( )
41
+ generateSignature ( )
31
42
for ( let i in imgList ) {
32
- let body = await client . put ( `${ path } ${ imgList [ i ] . fileName } ` , Buffer . from ( imgList [ i ] . base64Image , 'base64' ) )
33
- if ( body . res . status === 200 ) {
43
+ const signature = generateSignature ( imgList [ i ] . fileName )
44
+ const options = postOptions ( imgList [ i ] . fileName , signature , imgList [ i ] . base64Image )
45
+ let body = await request ( options )
46
+ if ( body . statusCode === 200 ) {
34
47
delete imgList [ i ] . base64Image
35
48
if ( customUrl ) {
36
49
imgList [ i ] [ 'imgUrl' ] = `${ customUrl } /${ path } ${ imgList [ i ] . fileName } `
37
50
} else {
38
- imgList [ i ] [ 'imgUrl' ] = body . url
51
+ imgList [ i ] [ 'imgUrl' ] = `https:// ${ aliYunOptions . bucket } . ${ aliYunOptions . area } .aliyuncs.com/ ${ path } ${ imgList [ 0 ] . fileName } `
39
52
}
40
53
imgList [ i ] [ 'type' ] = 'aliyun'
41
54
if ( i - length === - 1 ) {
0 commit comments