-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
160 lines (143 loc) · 3.37 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
var Hapi = require('hapi');
var imgur = require('imgur');
var fs = require('fs');
var wrap = require('wordwrap')(40);
var gm = require('gm').subClass({imageMagick: true});
imgur.setClientId(process.env.IMGUR_CLIENT_ID);
var textHeight = 15;
var borderWidth = 2;
var borderColor = '#ffffff';
var imageWidth = 800;
var imageHeight = 409;
// title and domain data
var data = {
title: {
text: 'Ser Nica es...',
fontSize: 32,
color: '#ffffff',
position: {
x: 20,
y: 38
}
},
domain: {
text: 'http://www.sernicaes.com',
fontSize: 14,
color: '#013d85',
position: {
x: 610,
y: 404
}
},
hashtag: {
text: '#SerNicaEs',
fontSize: 14,
color: '#013d85',
position: {
x: 20,
y: 404
}
},
middle: {
fontSize: 36,
color: '#ffffff',
position: {
x: 20
}
}
};
var internals = {};
internals.get = function (request, reply) {
if (request.query.hasOwnProperty('text')) {
var text = request.query.text;
data.middle.text = wrap(text);
// number of lines
var textLines = data.middle.text.split('\n').length;
// center middle based number of lines
data.middle.position.y = (imageHeight / 2) - (textHeight * textLines);
gm('assets/bg.png')
.border(borderWidth, borderWidth)
.borderColor(borderColor)
// text title
.fill(data.title.color)
.fontSize(data.title.fontSize)
.drawText(data.title.position.x, data.title.position.y, data.title.text)
// text domain
.fill(data.domain.color)
.fontSize(data.domain.fontSize)
.drawText(data.domain.position.x, data.domain.position.y, data.domain.text)
// text hashtag
.fill(data.hashtag.color)
.fontSize(data.hashtag.fontSize)
.drawText(data.hashtag.position.x, data.hashtag.position.y, data.hashtag.text)
// text middle
.fill(data.middle.color)
.fontSize(data.middle.fontSize)
.drawText(data.middle.position.x, data.middle.position.y, data.middle.text)
// write file
.write(__dirname + '/new.png', function (err) {
if (!err) {
imgur.uploadFile(__dirname + '/new.png')
.then(function (json) {
// remove file
fs.unlinkSync(__dirname + '/new.png');
// output
reply({
error: false,
errorMessage: '',
success: true,
url: json.data.link
});
})
.catch(function (error) {
// remove file
fs.unlinkSync(__dirname + '/new.png');
// output
reply({
error: true,
errorMessage: error.message,
success: false,
url: ''
});
});
}
else {
reply({
error: true,
errorMessage: err.message,
success: false,
url: ''
});
}
});
}
else {
reply({
error: true,
errorMessage: 'Empty text.',
success: false,
url: ''
});
}
};
// http server
// Create a server with a host and port
var server = new Hapi.Server();
server.connection({
host: '0.0.0.0',
port: process.env.PORT || 5000,
routes: {
cors: true
}
});
server.route([
{ method: 'GET', path: '/', config: { handler: internals.get } }
]);
server.start(function (err) {
if (err) {
console.log(err);
}
else {
console.log('Ser Nica Es que la app no haga crash.');
}
});