把主题做的差不多能用之后,下步就是处理博客上各式各样的静态资源了,毕竟用来放博客的服务器带宽太少,要是把所有的资源全放上面的话,肯定会拖慢加载速度,于是需要一个能存放文件的地方。

选择

因为我的服务器是买在腾讯云的,于是就直接选择他家的对象存储,毕竟也没几个人会凑巧点到我的博客,前期这段时间免费的资源包已经完全够用了,等到我的博客真的火了(期望)再去看其他更实惠的方案吧。

上传文件

配置存储库的过程就不写了,无非就是设置 CORS ,添加防盗链什么的。

对我来说主要问题是要怎么舒服的把文件上传上去,个人感觉最理想的状态是越轻量越好,最好根本不需要安装,于是,我想到了拿来水群的bot,只需要写一个插件,把我发送给他的文件上传到对象存储,再返回链接就行了,这样既能在qq里保存一份副本,还不需要额外安装或打开其他工具,对我来说是个两全其美的方案。

插件编写

qqbot 用的是自己封装的一个插件加载模块 mirai-xiling 下面是上传到 cos 的代码,其余平台的也都差不多,调用官方的sdk传入文件数据即可

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') // http请求
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;

效果

{A2CB651C-F411-B56C-334F-C0BF202E41B4}.jpg

感觉还不错,而且还可以跨终端使用,那就这样啦

后续

后来还是感觉对象储存有那么一丢丢贵了,so~ 这是适配github的版本

可以直接使用npm安装

1
npm i -S @xiling-bot/github-img-bed@1.0.0

或者根据需求自行修改

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
const axios = require('axios') // http请求
const fs = require('fs');
const { join } = require('path');

let options = {
token: "", // token
userId: "", // 用户名
repositories: "", // 储存库
useJsDelivr: true, // 启用JsDelivr加速
message: "add new image", // commint信息
path: "imgs" // 存储路径
}

const githubUpdate = {
name: "github上传服务",
mounted(xilingOptions) {
devId = xilingOptions.dev;
try {
options = require(join(process.cwd(), "./options/githubUpdate.json"))
} catch (err) {
console.log("[githubUpdate] 需要初始化配置信息");
fs.writeFileSync(join(process.cwd(), "./options/githubUpdate.json"), JSON.stringify(options, null, 4));
}
},
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 && msg.sender.id === devId) {
// 过滤出所有的图片信息
let imgLink = msg.messageChain.filter(_msg => _msg.type === "Image");
if (imgLink.length === 1) {
let axiosOption = {
url: imgLink[0].url,
methods: "GET",
responseType: "arraybuffer"
},
img = await axios(axiosOption),
imgId = imgLink[0].imageId;
axios({
method: "PUT",
headers: { "Authorization": `token ${options.token}` },
url: `https://api.github.com/repos/${options.userId}/${options.repositories}/contents/${options.path}/${imgId}`,
data: {
"message": options.message,
"content": img.data.toString('base64')
}
}).then(res => {
if (options.useJsDelivr) {
msg.quoteReply(`![${imgId}](https://cdn.jsdelivr.net/gh/${options.userId}/${options.repositories}/${res.data.content.path})`);
} else {
msg.quoteReply(`![${imgId}](${res.data.content.download_url})`);
}
}).catch(err => {
console.log(err.response.data)
msg.quoteReply("上传失败");
msg.quoteReply(err.response.data);
})
} else if (imgLink.length) {
msg.reply("一次只能上传一张图片");
}
}
return true;
}
}]
}

module.exports = githubUpdate;

这张是存放在 github 上的图片

{56159EB4-95F0-2BA3-29B3-739AF94E4952}.jpg