diff --git a/src/main/utils/tcYunUpload.js b/src/main/utils/tcYunUpload.js index 1e9267d..eb1cf66 100644 --- a/src/main/utils/tcYunUpload.js +++ b/src/main/utils/tcYunUpload.js @@ -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) { diff --git a/src/renderer/components/SettingView/TcYun.vue b/src/renderer/components/SettingView/TcYun.vue index 77adcf7..0a137c0 100644 --- a/src/renderer/components/SettingView/TcYun.vue +++ b/src/renderer/components/SettingView/TcYun.vue @@ -11,6 +11,19 @@ label-width="120px" :model="form" size="mini"> + + + + \ No newline at end of file