-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
198 lines (188 loc) · 5.17 KB
/
index.js
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
const express = require('express')
const request = require('request')
const sharp = require('sharp')
const path = require('path')
const fs = require('fs')
const app = express()
app.use(express.json())
const cosConfig = {
Bucket: process.env.Bucket || '', // 填写云托管对象存储桶名称
Region: process.env.Region || 'ap-shanghai' // 存储桶地域,默认是上海,其他地域环境对应填写
}
app.get('/', function (req, res) {
res.send('success')
})
app.get('/made', async function (req, res) {
const { fileid } = req.query
res.send(await img2webp(fileid))
})
app.listen(80, function () {
initcos()
console.log('图像处理服务启动成功!')
})
async function img2webp (fileid) {
if (fileid != null && fileid.indexOf('cloud://') === 0) { // 判断是否是fileid
const filePath = fileid.replace(/cloud:\/\/.{6,}.[0-9]*-.{6,}-[0-9]*\//, '/') // 将fileid处理一下,COS-SDK只需要目录
const tempPath = new Date().getTime().toString() // 创建一个临时的文件名,便于处理
const info = await getFile(filePath, tempPath + filePath.replace(/.*[.]/, '.')) // 下载文件,文件保存为临时名+后缀
if (info.code === 0) { // 下载成功
const webppath = path.join('./', tempPath + '.webp') // 确定转换后的存储地址
try {
await madewebp(info.file, webppath) // 开始转换,输入原文件和输出路径
const res = await uploadFile(filePath.match(/.*[.]/)[0] + 'webp', webppath) // 上传文件,保存在同目录中,以webp后缀替换
fs.unlinkSync(info.file) // 删除原始临时文件
fs.unlinkSync(webppath) // 删除webp临时文件
console.log('文件处理结果', res.code)
return {
code: 0,
fileid: fileid.match(/cloud:\/\/.*[.]/)[0] + 'webp'
}
} catch (e) {
fs.unlinkSync(info.file) // 删除原始临时文件
return {
code: 1,
msg: '文件转换失败!' + e
}
}
} else {
return {
code: 1,
msg: '文件下载失败!' + info.msg
}
}
} else {
return {
code: -1,
msg: 'fileid缺失或者格式不正确!'
}
}
}
function madewebp (prefile, outfile) {
return new Promise((resolve, reject) => {
sharp(prefile).webp({ lossless: true }).toFile(outfile, function (err, info) {
if (err) {
reject(err.toString())
} else {
resolve(info)
}
})
})
}
async function initcos () {
const COS = require('cos-nodejs-sdk-v5')
const res = await call({
url: 'http://api.weixin.qq.com/_/cos/getauth',
method: 'GET'
})
try {
const info = JSON.parse(res)
const auth = {
TmpSecretId: info.TmpSecretId,
TmpSecretKey: info.TmpSecretKey,
SecurityToken: info.Token,
ExpiredTime: info.ExpiredTime
}
this.cos = new COS({
getAuthorization: async function (options, callback) {
callback(auth)
}
})
console.log('COS初始化成功')
} catch (e) {
console.log('COS初始化失败', res)
}
}
/**
* 封装的文件下载函数
* @param {*} cloudpath 文件路径
* @param {*} filepath 保存本地路径
*/
async function getFile (cloudpath, filepath) {
try {
const res = await this.cos.getObject({
Bucket: cosConfig.Bucket,
Region: cosConfig.Region,
Key: cloudpath,
Output: path.join('./', filepath)
})
if (res.statusCode === 200) {
return {
code: 0,
file: path.join('./', filepath)
}
} else {
return {
code: 1,
msg: JSON.stringify(res)
}
}
} catch (e) {
console.log('下载文件失败', e.toString())
return {
code: -1,
msg: e.toString()
}
}
}
/**
* 封装的上传文件函数
* @param {*} cloudpath 上传的云上路径
* @param {*} filepath 本地文件路径
*/
async function uploadFile (cloudpath, filepath) {
const authres = await call({
url: 'http://api.weixin.qq.com/_/cos/metaid/encode',
method: 'POST',
data: {
openid: '', // 管理端为空
bucket: cosConfig.Bucket,
paths: [cloudpath]
}
})
try {
const auth = JSON.parse(authres)
const res = await this.cos.putObject({
Bucket: cosConfig.Bucket,
Region: cosConfig.Region,
Key: cloudpath,
StorageClass: 'STANDARD',
Body: fs.createReadStream(filepath),
ContentLength: fs.statSync(filepath).size,
Headers: {
'x-cos-meta-fileid': auth.respdata.x_cos_meta_field_strs[0]
}
})
if (res.statusCode === 200) {
return {
code: 0,
file: JSON.stringify(res)
}
} else {
return {
code: 1,
msg: JSON.stringify(res)
}
}
} catch (e) {
console.log('上传文件失败', e.toString())
return {
code: -1,
msg: e.toString()
}
}
}
function call (obj) {
return new Promise((resolve, reject) => {
request({
url: obj.url,
method: obj.method || 'POST',
headers: {
'content-type': 'application/json'
},
body: JSON.stringify(obj.data || {})
}, function (err, response) {
if (err) reject(err)
resolve(response.body)
})
})
}