mirror of
https://github.com/Kuingsmile/PicList.git
synced 2025-01-23 06:38:13 -05:00
Finished: Tencent COS v5 version
This commit is contained in:
parent
2496c0b467
commit
757a7d1260
@ -12,6 +12,7 @@ const generateSignature = (fileName) => {
|
||||
const appId = options.appId
|
||||
const bucket = options.bucket
|
||||
let signature
|
||||
let signTime
|
||||
if (!options.version || options.version === 'v4') {
|
||||
const random = Math.floor(Math.random() * 10000000000)
|
||||
const current = parseInt(new Date().getTime() / 1000) - 1
|
||||
@ -23,9 +24,10 @@ const generateSignature = (fileName) => {
|
||||
const tempString = Buffer.concat([signHexKey, Buffer.from(multiSignature)])
|
||||
signature = Buffer.from(tempString).toString('base64')
|
||||
} else {
|
||||
// https://cloud.tencent.com/document/product/436/7778#signature
|
||||
const today = Math.floor(new Date().getTime() / 1000)
|
||||
const tomorrow = today + 86400
|
||||
const signTime = `${today};${tomorrow}`
|
||||
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')
|
||||
@ -35,7 +37,8 @@ const generateSignature = (fileName) => {
|
||||
return {
|
||||
signature,
|
||||
appId,
|
||||
bucket
|
||||
bucket,
|
||||
signTime
|
||||
}
|
||||
}
|
||||
|
||||
@ -43,7 +46,6 @@ const postOptions = (fileName, signature, imgBase64) => {
|
||||
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',
|
||||
@ -64,13 +66,10 @@ const postOptions = (fileName, signature, imgBase64) => {
|
||||
url: `http://${options.bucket}.cos.${options.area}.myqcloud.com/${path}${fileName}`,
|
||||
headers: {
|
||||
Host: `${options.bucket}.cos.${options.area}.myqcloud.com`,
|
||||
Authorization: signature.signature,
|
||||
Authorization: `q-sign-algorithm=sha1&q-ak=${options.secretId}&q-sign-time=${signature.signTime}&q-key-time=${signature.signTime}&q-header-list=host&q-url-param-list=&q-signature=${signature.signature}`,
|
||||
contentType: 'multipart/form-data'
|
||||
},
|
||||
formData: {
|
||||
op: 'upload',
|
||||
filecontent: Buffer.from(imgBase64, 'base64')
|
||||
},
|
||||
body: Buffer.from(imgBase64, 'base64'),
|
||||
resolveWithFullResponse: true
|
||||
}
|
||||
}
|
||||
@ -85,15 +84,28 @@ const tcYunUpload = async (img, type, webContents) => {
|
||||
const tcYunOptions = db.read().get('picBed.tcyun').value()
|
||||
const customUrl = tcYunOptions.customUrl
|
||||
const path = tcYunOptions.path
|
||||
const useV4 = !tcYunOptions.version || tcYunOptions.version === 'v4'
|
||||
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 = `${res}`
|
||||
console.log(body)
|
||||
if (body.message === 'SUCCESS') {
|
||||
.then(res => res)
|
||||
.catch(err => {
|
||||
console.log(err.response.body)
|
||||
return {
|
||||
statusCode: 400,
|
||||
body: {
|
||||
msg: '认证失败!'
|
||||
}
|
||||
}
|
||||
})
|
||||
let body
|
||||
if (useV4) {
|
||||
body = JSON.parse(res)
|
||||
} else {
|
||||
body = res
|
||||
}
|
||||
if (useV4 && body.message === 'SUCCESS') {
|
||||
delete imgList[i].base64Image
|
||||
if (customUrl) {
|
||||
imgList[i]['imgUrl'] = `${customUrl}/${path}${imgList[i].fileName}`
|
||||
@ -104,6 +116,17 @@ const tcYunUpload = async (img, type, webContents) => {
|
||||
if (i - length === -1) {
|
||||
webContents.send('uploadProgress', 60)
|
||||
}
|
||||
} else if (!useV4 && body && body.statusCode === 200) {
|
||||
delete imgList[i].base64Image
|
||||
if (customUrl) {
|
||||
imgList[i]['imgUrl'] = `${customUrl}/${path}${imgList[i].fileName}`
|
||||
} else {
|
||||
imgList[i]['imgUrl'] = `https://${tcYunOptions.bucket}.cos.${tcYunOptions.area}.myqcloud.com/${path}${imgList[i].fileName}`
|
||||
}
|
||||
imgList[i]['type'] = 'tcyun'
|
||||
if (i - length === -1) {
|
||||
webContents.send('uploadProgress', 60)
|
||||
}
|
||||
} else {
|
||||
webContents.send('uploadProgress', -1)
|
||||
const notification = new Notification({
|
||||
@ -111,6 +134,7 @@ const tcYunUpload = async (img, type, webContents) => {
|
||||
body: res.body.msg
|
||||
})
|
||||
notification.show()
|
||||
return false
|
||||
}
|
||||
}
|
||||
webContents.send('uploadProgress', 100)
|
||||
|
Loading…
Reference in New Issue
Block a user