const electron = require('electron') const COS = require('cos-nodejs-sdk-v5') const request = require('request') const crypto = require('crypto') const sha1keyBase64 = function(str, key) { return crypto.createHmac('sha1', key).update(str).digest('base64') } const randomNum = function(minNum, maxNum) { return parseInt(Math.random() * (maxNum - minNum + 1) + minNum, 10) } const generateNonce = function() { return randomNum(10000, 99999) } const SecretId = 'AKIDvmW8mNvCQVt9GEnd3JNH5lHKI8oJnv46' const SecretKey = 'd6QZhgT7alnhR3VghWAg3FF4c2JMG1c2' const cosCDN = new COS({ SecretId, SecretKey }) const generateSign = function(method, data) { let str = `${method}cdn.api.qcloud.com/v2/index.php?` let i = 0 for (const key in data) { if ({}.hasOwnProperty.call(data, key)) { if (i++ > 0) str += '&' str += `${key}=${data[key]}` } } return sha1keyBase64(str, SecretKey) } const cdnTools = { refreshDir(url) { const now = Math.round(new Date() / 1000) const data = { Action: 'RefreshCdnDir', Nonce: generateNonce(), SecretId: SecretId, Timestamp: now, 'dirs.0': url } data.Signature = generateSign('POST', data) return new Promise((resolve, reject) => { const link = 'https://cdn.api.qcloud.com/v2/index.php' const options = { method: 'POST', url: link, headers: { 'Cache-Control': 'no-cache', 'Content-Type': 'application/x-www-form-urlencoded' }, form: data } request(options, (err, response, body) => { if (err) { return reject(err) } resolve(JSON.parse(body)) }) }) }, refreshOneUrl(url) { const now = Math.round(new Date() / 1000) const data = { Action: 'RefreshCdnUrl', Nonce: generateNonce(), SecretId: SecretId, Timestamp: now, 'urls.0': url } data.Signature = generateSign('POST', data) return new Promise((resolve, reject) => { const link = 'https://cdn.api.qcloud.com/v2/index.php' const options = { method: 'POST', url: link, headers: { 'Cache-Control': 'no-cache', 'Content-Type': 'application/x-www-form-urlencoded' }, form: data } request(options, (err, response, body) => { if (err) { return reject(err) } resolve(JSON.parse(body)) }) }) }, uploadToCDN(fileName, path) { return new Promise(function(resolve, reject) { cosCDN.sliceUploadFile({ Bucket: 'client-1256832210', Region: 'ap-beijing', Key: fileName, FilePath: path }, function(err, data) { if (err) { reject(err) } else { resolve(data) } }) }) }, deleteFromCDN(files) { return new Promise(function(resolve, reject) { cosCDN.deleteMultipleObject({ Bucket: 'client-1256832210', Region: 'ap-beijing', Objects: files }, function(err, data) { if (err) { reject(err) } else { resolve(data) } }) }) }, listCDN(subPath) { return new Promise(function(resolve, reject) { cosCDN.getBucket({ Bucket: 'client-1256832210', Region: 'ap-beijing', Prefix: subPath }, function(err, data) { // console.log(err || data.Contents) if (err) { reject(err) } else { resolve(data.Contents) } }) }) } } const main = function() { electron.ipcRenderer.on('ping', (event, message) => { console.log(message) // Prints 'whoooooooh!' }) electron.ipcRenderer.on('save', (event, message) => { console.log('save') const eventMap = new CustomEvent('saveMap', { bubbles: true, detail: {} }) window.dispatchEvent(eventMap) }) electron.ipcRenderer.on('load', (event, message) => { console.log('load') console.log(message) const eventMap = new CustomEvent('loadMap', { bubbles: true, detail: { path: message[0] } }) window.dispatchEvent(eventMap) }) window.refreshCDN = function(gameId) { return cdnTools.refreshDir(`${gameId}/`) } window.listCDN = function(subPath) { return cdnTools.listCDN(subPath) } window.deleteFromCDN = function(files) { return cdnTools.deleteFromCDN(files) } window.uploadFile = function(filePath, showPath) { return cdnTools.uploadToCDN(showPath, filePath) .then((data) => { console.log(data) const urlCdn = `https://resource.kingsome.cn/${data.Key}` return cdnTools.refreshOneUrl(urlCdn) }) // electron.remote.dialog.showOpenDialog({ // title: '选择配表所在文件夹', // properties: ['openDirectory'] // }).then(result => { // console.log(result) // const eventMap = new CustomEvent('loadMap', { // bubbles: true, // detail: { path: result.filePaths[0] } // }) // window.dispatchEvent(eventMap) // }).catch(err => { // console.log(err) // }) } window.upload2FTP = function(filePath, showPath) { // return ftpTools.upload(filePath, showPath) } window.saveMap = function() { const eventMap = new CustomEvent('saveMap', { bubbles: true, detail: {} }) window.dispatchEvent(eventMap) } window.selectGame = function() { electron.remote.dialog.showOpenDialog({ title: '选择配表所在文件夹', properties: ['openDirectory'] }).then(result => { console.log(result) if (result.canceled) { return } const eventMap = new CustomEvent('setGamePath', { bubbles: true, detail: { path: result.filePaths[0] } }) window.dispatchEvent(eventMap) }).catch(err => { console.log(err) }) } // ftpTools.init() } window.onload = function() { main() }