This repository has been archived by the owner on Aug 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebhook.js
140 lines (125 loc) · 4.27 KB
/
webhook.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
/*
* Function as Service example with Minio Bucket Webhook Notification
* (C) 2017-2019 Minio, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
let express = require('express');
let app = express();
let bodyParser = require('body-parser');
let Minio = require('minio');
let sharp = require('sharp');
let config = require('config');
let mcConfig = config.get('config');
if (mcConfig.endPoint === '<endpoint>') {
console.log('Please configure your endpoint in \"config/webhook.json\".');
process.exit(1);
}
// Allocate a new minio-js client.
var mc = new Minio.Client(mcConfig);
// Generates a webhook config json.
var webhookConfig = function() {
var config = {
"webhook": {}
};
config.webhook["1"] = {};
config.webhook["1"].enable = true;
config.webhook["1"].endpoint = "http://localhost:3000";
return JSON.stringify(config, null, '\t');
}
// Pull sizes from config
const sizes = mcConfig.widths;
var transformer;
// Sharp defaults to jpeg, to use other formats use
// sharp() documentation at https://sharp.pixelplumbing.com/
var thumbnailName;
app.use(bodyParser.json()); // for parsing application/json
app.post('/', function(req, res) {
var bname = req.body.Records[0].s3.bucket.name;
var oname = req.body.Records[0].s3.object.key;
mc.getObject(bname, oname,
function(err, dataStream) {
if (err) {
return console.log(err);
}
console.log("Uploading new images to",
"\"" + mcConfig.destBucket + "\"");
for (var i = 0; i < sizes.length; i++) {
if (mcConfig.webp === true) {
transformer = sharp().resize(sizes[i]).webp();
mc.putObject(mcConfig.destBucket,
oname.split('.')[0] + "-" + sizes[i] + ".webp",
dataStream.pipe(transformer),
"image/webp",
function(err, etag) {
if (err) {
return console.log(err);
}
console.log("Successfully uploaded",
"\"" + etag + "\"");
});
}
transformer = sharp().resize(sizes[i]);
mc.putObject(mcConfig.destBucket,
thumbnailName = oname.split('.')[0] + "-" + sizes[i] + ".jpg",
dataStream.pipe(transformer),
"image/jpeg",
function(err, etag) {
if (err) {
return console.log(err);
}
console.log("Successfully uploaded",
"\"" + etag + "\"");
});
}
});
res.send("");
})
var server = app.listen(3000, function() {
console.log('Webhook listening on all interfaces at port 3000!');
console.log('Please update minio server config as explained in `https://docs.min.io/docs/minio-server-configuration-guide.html` to enable webhook notification target.');
console.log(webhookConfig());
console.log('Once server config is updated, please restart your minio server.');
console.log('');
console.log('Now we proceed to use "mc" to enable receiving events over webhook.');
console.log('');
if ((mcConfig.destBucket) && (mcConfig.bucket)) {
console.log(' $ mc mb myminio/' + mcConfig.bucket);
console.log(' $ mc mb myminio/' + mcConfig.destBucket);
}
var msg = ' $ mc event add myminio/images arn:minio:sqs::_:webhook --event put';
if (mcConfig.prefix) {
msg += ' --prefix ' + mcConfig.prefix;
}
if (mcConfig.suffix) {
msg += ' --suffix ' + mcConfig.suffix;
}
console.log(msg);
})
if (process.platform === "win32") {
var rl = require("readline").createInterface({
input: process.stdin,
output: process.stdout
});
rl.on("SIGINT", function() {
process.emit("SIGINT");
});
}
process.on("SIGINT", function() {
// graceful shutdown
server.close(function() {
console.log("Closed out remaining connections.");
// Close db connections, etc.
});
process.exit();
});