/
index.js
186 lines (146 loc) · 4.38 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
require('./promise-allSettled')
const extCi = require("@cloudbase/extension-ci");
const tcb = require('tcb-admin-node')
const axios = require('axios')
const detectFace = require('./req-iai-face').detectFace
// let env = process.env.TCB_ENV // === 'local' ? 'development-v9y2f' : process.env.TCB_ENV
tcb.init({
env: 'production-topjt'
})
tcb.registerExtension(extCi)
const getBase64 = async (fileID) => {
let { fileContent } = await tcb.downloadFile({
fileID
})
return fileContent.toString('base64')
}
const getImageUrl = async (fileID) => {
const { fileList } = await tcb.getTempFileURL({
fileList: [fileID]
})
return fileList[0].tempFileURL
}
exports.main = async (event) => {
const { fileID = '', width = 600, height = 600 } = event
if (!fileID) {
let errorString = '请设置 fileID'
console.log(errorString)
return {
data: {},
time: new Date(),
status: -10086,
message: errorString
}
}
try {
let originImageUrl = await getImageUrl(fileID)
let rule = `imageMogr2/thumbnail/!${width}x${height}r|imageMogr2/scrop/${width}x${height}/`
console.log('rule :', rule);
let cutImageUrl = originImageUrl + '?' + rule
console.log('cutImageUrl :', cutImageUrl);
let {
fileContent,
base64Main
} = await axios.get(cutImageUrl, {
responseType: 'arraybuffer'
})
.then(response => {
let buffer1 = new Buffer(response.data, 'binary')
return {
fileContent: buffer1,
base64Main: buffer1.toString('base64')
}
})
let imgID = fileID.replace('cloud://', '')
let index = imgID.indexOf('/')
let cloudPath = imgID.substr(index)
console.log('fileContent :', fileContent);
let facePath = 'detect-face'
return Promise.allSettled([
tcb.callFunction({
name: 'image-safe-check',
data: {
fileID: fileID
}
}),
detectFace(base64Main),
tcb.uploadFile({
cloudPath: facePath + cloudPath,
fileContent: fileContent
})
]).then((results) => {
let checkResult = results[0]
let faceResult = results[1]
let fileReult = results[2]
console.log('fileReult :', fileReult);
if (checkResult.result.status) {
return checkResult.result
}
return {
...faceResult,
data: {
...faceResult.data,
faceFileID: fileReult.fileID,
faceImageUrl: cutImageUrl
}
}
})
console.log('arrayBuffer :', arrayBuffer);
} catch (error) {
console.log('error :', error);
return {
data: {},
time: new Date(),
status: -10087,
message: JSON.stringify(error)
}
}
// let imgID = fileID.replace('cloud://', '')
// let index = imgID.indexOf('/')
// let cloudPath = imgID.substr(index)
// let cloudEnvPath = imgID.substr(0, index)
// let facePath1 = '/face-recognition1'
// const opts = {
// rules: [
// {
// // 处理结果的文件路径,如以’/’开头,则存入指定文件夹中,否则,存入原图文件存储的同目录
// fileid: facePath1 + cloudPath,
// rule: "imageMogr2/scrop/800x800" // 处理样式参数,与下载时处理图像在url拼接的参数一致
// }
// ]
// }
// await tcb.invokeExtension("CloudInfinite", {
// action: "ImageProcess",
// cloudPath: cloudPath, // 存储图像的绝对路径,与tcb.uploadFile中一致
// // fileContent, // 该字段可选,文件内容:Uint8Array|Buffer。有值,表示上传时处理图像;为空,则处理已经上传的图像
// operations: opts
// });
// let faceFileID = cloudEnvPath + facePath1 + cloudPath
// // const base64 = await getBase64(faceFileID)
// const imageUrl = await getImageUrl(faceFileID)
// return Promise.allSettled([
// tcb.callFunction({
// name: 'image-safe-check',
// data: {
// fileID: faceFileID
// }
// }),
// detectFace(imageUrl)
// ]).then((results) => {
// let checkResult = results[0]
// let faceResult = results[1]
// if (checkResult.result.status) {
// return checkResult.result
// }
// return {
// ...faceResult,
// data: {
// ...faceResult.data,
// faceFileID,
// faceImageUrl: imageUrl
// }
// }
// }).catch(error => {
// console.log('error :', error);
// })
}