Added: v5 options

This commit is contained in:
Molunerfinn 2018-05-07 19:55:33 +08:00
parent 4a685e0474
commit 2496c0b467
2 changed files with 88 additions and 24 deletions

View File

@ -5,21 +5,33 @@ import { Notification, clipboard } from 'electron'
import crypto from 'crypto'
// generate COS signature string
const generateSignature = () => {
const generateSignature = (fileName) => {
const options = db.read().get('picBed.tcyun').value()
const secretId = options.secretId
const secretKey = options.secretKey
const appId = options.appId
const bucket = options.bucket
const random = Math.floor(Math.random() * 10000000000)
const current = parseInt(new Date().getTime() / 1000) - 1
const expired = current + 3600
let signature
if (!options.version || options.version === 'v4') {
const random = Math.floor(Math.random() * 10000000000)
const current = parseInt(new Date().getTime() / 1000) - 1
const expired = current + 3600
const multiSignature = `a=${appId}&b=${bucket}&k=${secretId}&e=${expired}&t=${current}&r=${random}&f=`
const multiSignature = `a=${appId}&b=${bucket}&k=${secretId}&e=${expired}&t=${current}&r=${random}&f=`
const signHexKey = crypto.createHmac('sha1', secretKey).update(multiSignature).digest()
const tempString = Buffer.concat([signHexKey, Buffer.from(multiSignature)])
const signature = Buffer.from(tempString).toString('base64')
const signHexKey = crypto.createHmac('sha1', secretKey).update(multiSignature).digest()
const tempString = Buffer.concat([signHexKey, Buffer.from(multiSignature)])
signature = Buffer.from(tempString).toString('base64')
} else {
const today = Math.floor(new Date().getTime() / 1000)
const tomorrow = today + 86400
const signTime = `${today};${tomorrow}`
const signKey = crypto.createHmac('sha1', secretKey).update(signTime).digest('hex')
const httpString = `put\n/${options.path}${fileName}\n\nhost=${options.bucket}.cos.${options.area}.myqcloud.com\n`
const sha1edHttpString = crypto.createHash('sha1').update(httpString).digest('hex')
const stringToSign = `sha1\n${signTime}\n${sha1edHttpString}\n`
signature = crypto.createHmac('sha1', signKey).update(stringToSign).digest('hex')
}
return {
signature,
appId,
@ -28,19 +40,38 @@ const generateSignature = () => {
}
const postOptions = (fileName, signature, imgBase64) => {
const area = db.read().get('picBed.tcyun.area').value()
const path = db.read().get('picBed.tcyun.path').value()
return {
method: 'POST',
url: `http://${area}.file.myqcloud.com/files/v2/${signature.appId}/${signature.bucket}/${path}${fileName}`,
headers: {
Host: `${area}.file.myqcloud.com`,
Authorization: signature.signature,
contentType: 'multipart/form-data'
},
formData: {
op: 'upload',
filecontent: Buffer.from(imgBase64, 'base64')
const options = db.read().get('picBed.tcyun').value()
const area = options.area
const path = options.path
console.log(options.verison)
if (!options.version || options.version === 'v4') {
return {
method: 'POST',
url: `http://${area}.file.myqcloud.com/files/v2/${signature.appId}/${signature.bucket}/${path}${fileName}`,
headers: {
Host: `${area}.file.myqcloud.com`,
Authorization: signature.signature,
contentType: 'multipart/form-data'
},
formData: {
op: 'upload',
filecontent: Buffer.from(imgBase64, 'base64')
}
}
} else {
return {
method: 'PUT',
url: `http://${options.bucket}.cos.${options.area}.myqcloud.com/${path}${fileName}`,
headers: {
Host: `${options.bucket}.cos.${options.area}.myqcloud.com`,
Authorization: signature.signature,
contentType: 'multipart/form-data'
},
formData: {
op: 'upload',
filecontent: Buffer.from(imgBase64, 'base64')
},
resolveWithFullResponse: true
}
}
}
@ -50,15 +81,18 @@ const tcYunUpload = async (img, type, webContents) => {
webContents.send('uploadProgress', 0)
const imgList = await img2Base64[type](img)
webContents.send('uploadProgress', 30)
const singature = generateSignature()
const length = imgList.length
const tcYunOptions = db.read().get('picBed.tcyun').value()
const customUrl = tcYunOptions.customUrl
const path = tcYunOptions.path
for (let i in imgList) {
const singature = generateSignature(imgList[i].fileName)
const options = postOptions(imgList[i].fileName, singature, imgList[i].base64Image)
console.log(123, options)
const res = await request(options)
const body = JSON.parse(res)
// const body = JSON.parse(res)
const body = `${res}`
console.log(body)
if (body.message === 'SUCCESS') {
delete imgList[i].base64Image
if (customUrl) {

View File

@ -11,6 +11,19 @@
label-width="120px"
:model="form"
size="mini">
<el-form-item
label="COS版本"
>
<el-switch
v-model="form.version"
active-text="v4"
inactive-text="v5"
active-value="v4"
inactive-value="v5"
inactive-color="#67C23A"
></el-switch>
<i class="el-icon-question" @click="openWiki"></i>
</el-form-item>
<el-form-item
label="设定SecretId"
prop="secretId"
@ -86,7 +99,8 @@ export default {
appId: '',
area: '',
path: '',
customUrl: ''
customUrl: '',
version: 'v4'
}
}
},
@ -113,6 +127,9 @@ export default {
return false
}
})
},
openWiki () {
this.$electron.remote.shell.openExternal('https://github.com/Molunerfinn/PicGo/wiki/%E8%AF%A6%E7%BB%86%E7%AA%97%E5%8F%A3%E7%9A%84%E4%BD%BF%E7%94%A8#%E5%BE%AE%E5%8D%9A%E5%9B%BE%E5%BA%8A')
}
}
}
@ -147,4 +164,17 @@ export default {
.el-radio-button__inner
border-left none
border-radius 0 14px 14px 0
.el-switch__label
color #eee
&.is-active
color #409EFF
.el-icon-question
font-size 20px
float right
margin-top 9px
color #eee
cursor pointer
transition .2s color ease-in-out
&:hover
color #409EFF
</style>