-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
165 lines (147 loc) · 4.62 KB
/
app.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
const coworkers = require('coworkers')
const app = coworkers();
var filepreview = require('filepreview');
var findRemoveSync = require('find-remove');
const uuidv4 = require('uuid/v4');
var ExifImage = require('exif').ExifImage;
var crypto = require('crypto');
var path = require('path');
var fs = require('fs');
var os = require('os');
var child_process = require('child_process');
var mime = require('mime');
function checkOldFiles() {
findRemoveSync('./tmp', {age: {seconds: 60*60*24*7 /* 7 days */}, extensions: ['.jpg']});
}
function sendMsgBack(msgContent,backQueue, correlationId)
{
app.connection.createChannel().then(function(ch){
ch.assertQueue(backQueue);
var content = new Buffer(JSON.stringify(msgContent));
const replyMsg = ch.sendToQueue(backQueue, content, {'correlationId': correlationId})
});
}
// shared middlewares
app.use(function * (next) {
// all consumers will run this logic...
console.log('waiting');
yield next
})
// queue consumers w/ middlewares
app.queue('imagepreview-queue', function * () {
//console.log(this.message);
// consumer specific logic
var messageBody = this.message.content.toString();
var correlationId = this.properties.correlationId;
var messageJson = JSON.parse(messageBody);
var input_original = messageJson.url;
delete messageJson.url;
var input = input_original;
var back = false;
var backQueue = null;
var hostName = null;
if(messageJson.sendMsgBack)
{
back = true;
backQueue = messageJson.backQueue;
if(messageJson.hostName)
{
hostName = messageJson.hostName;
if(hostName.substr(hostName.length - 1) !== '/')
{
hostName += '/';
}
delete messageJson.hostName;
}
delete messageJson.sendMsgBack;
delete messageJson.backQueue
}
if (input_original.indexOf("http://") == 0 || input_original.indexOf("https://") == 0) {
var url = input_original.split("/");
var url_filename = url[url.length - 1];
var hash = crypto.createHash('sha512');
hash.update(Math.random().toString());
hash = hash.digest('hex');
var temp_input = path.join(os.tmpdir(), hash + url_filename);
curlArgs = ['--silent', '-L', input, '-o', temp_input];
child_process.execFileSync("curl", curlArgs);
input = temp_input;
}
var options = {
background: 'white',
alpha: 'remove',
colorspace: 'sRGB'
};
for(var key in messageJson)
{
options[key] = messageJson[key];
}
var mimeType = mime.lookup(input);
var exif = {}
if(mimeType === 'image/jpeg')
{
try {
new ExifImage({ image : input}, function (error, exifData) {
if (error)
console.log('Error: '+error.message);
else {
console.log(exifData); // Do something with your data!
exif = exifData;
}
});
} catch (error) {
console.log('Error: ' + error.message);
}
}
var destName = './tmp/'+uuidv4()+'.jpg';
filepreview.generate(input, destName, options, function(error) {
if (error) {
console.log('error', error);
if(back)
{
sendMsgBack({success: false}, backQueue, correlationId)
}
}
else {
console.log('['+correlationId+'] File preview is '+destName);
if(back)
{
sendMsgBack({success: true, 'file': destName.replace('./',hostName),'exif':exif}, backQueue, correlationId)
}
}
app.context.ack = true;
});
/*
var previewCreated = filepreview.generateSync(input, destName, options);
console.log('previewCreated', previewCreated);
if(previewCreated === true)
{
console.log('File preview is '+destName);
if(back)
{
sendMsgBack({success: true, 'file': destName.replace('./',hostName)}, backQueue, correlationId)
}
}
else {
console.log('error');
if(back)
{
sendMsgBack({success: false}, backQueue, correlationId)
}
}
*/
checkOldFiles();
this.ack = true; // acknowledge message later, see `Context` documentation below
});
// middleware error handler
app.on('error', function (err) {
console.error(err.stack)
})
// connect to rabbitmq and begin consuming
app.connect()
.then(function(){
console.log('connection established');
})
.catch(function(error){
console.log('error', error);
});