1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
| const axios = require('axios') const fs = require('fs'); const { join } = require('path');
const COS = require('cos-nodejs-sdk-v5');
let options = { SecretId: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', SecretKey: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', Bucket: 'Bucket-name', Region: 'path' }
let cos = {}, uploadGroupId = null, devId = null;
const oosUpdate = { name: "对象存储上传服务", mounted(xilingOptions) { devId = xilingOptions.dev; try { options = require(join(process.cwd(), "./options/oosUpdate.json")) } catch (err) { console.log("[oosUpdate] 需要初始化配置信息"); fs.writeFileSync(join(process.cwd(), "./options/oosUpdate.json"), JSON.stringify(options, null, 4)); } cos = new COS({ SecretId: options.SecretId, SecretKey: options.SecretKey }); }, command: [{ name: "上传模式", exce(msg, parameter) { if (msg.sender.id === devId) { msg.reply([{ type: "Plain", text: "上传模式已开启" }]); uploadGroupId = msg.sender.group.id; } }, }, { name: "结束", exce(msg, parameter) { if (msg.sender.id === devId) { msg.reply([{ type: "Plain", text: "上传模式已关闭," }]); uploadGroupId = null; } } }], passive: [{ name: "监听方法", async exce(msg) { if (msg.sender.group.id === uploadGroupId) { let imgLink = msg.messageChain.filter(_msg => _msg.type === "Image"); if (imgLink.length === 1) { let axiosOption = { url: imgLink[0].url, methods: "GET", responseType: "stream" }, img = await axios(axiosOption), imgId = imgLink[0].imageId.split("-").slice(1, 4).join(""); cos.putObject({ Bucket: options.Bucket, Region: options.Region, Key: imgId, StorageClass: 'STANDARD', Body: img.data }, function(err, data) { if (!err) { msg.quoteReply(`![${imgId}](https://${data.Location})`); } else { msg.quoteReply("上传失败,请在控制台查看错误"); console.log(err); } }); } else if (imgLink.length) { msg.reply("一次只能上传一张图片"); } } return true; } }] }
module.exports = oosUpdate;
|