Skip to content

Commit 5a25aa0

Browse files
committed
Fuck: remove ali-oss
1 parent 671c011 commit 5a25aa0

File tree

4 files changed

+134
-465
lines changed

4 files changed

+134
-465
lines changed

.eslintignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
test/unit/coverage/**
22
test/unit/*.js
33
test/e2e/*.js
4-
dist/
4+
dist/

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@
7171
}
7272
},
7373
"dependencies": {
74-
"ali-oss": "^5.3.0",
7574
"axios": "^0.16.1",
75+
"dateformat": "^3.0.3",
7676
"element-ui": "^2.0.5",
7777
"fecha": "^2.3.3",
7878
"fs-extra": "^4.0.2",

src/main/utils/aliYunUpload.js

+29-16
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,32 @@
1+
import request from 'request-promise'
12
import * as img2Base64 from './img2base64'
23
import db from '../../datastore/index'
34
import { Notification, clipboard } from 'electron'
4-
import { Wrapper as OSS } from 'ali-oss'
5+
import crypto from 'crypto'
56

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+
}
716

8-
// generate OSS Options
9-
const generateOSSOptions = () => {
17+
const postOptions = (fileName, signature, imgBase64) => {
1018
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+
}
1930
}
2031

2132
const aliYunUpload = async (img, type, webContents) => {
@@ -27,15 +38,17 @@ const aliYunUpload = async (img, type, webContents) => {
2738
const customUrl = aliYunOptions.customUrl
2839
const path = aliYunOptions.path
2940
const length = imgList.length
30-
generateOSSOptions()
41+
generateSignature()
3142
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) {
3447
delete imgList[i].base64Image
3548
if (customUrl) {
3649
imgList[i]['imgUrl'] = `${customUrl}/${path}${imgList[i].fileName}`
3750
} else {
38-
imgList[i]['imgUrl'] = body.url
51+
imgList[i]['imgUrl'] = `https://${aliYunOptions.bucket}.${aliYunOptions.area}.aliyuncs.com/${path}${imgList[0].fileName}`
3952
}
4053
imgList[i]['type'] = 'aliyun'
4154
if (i - length === -1) {

0 commit comments

Comments
 (0)