-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
180 lines (176 loc) · 5.05 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
const app = require("http");
const url = require("url");
const imagemin = require("imagemin");
const imageminJpegtran = require("imagemin-jpegtran");
const imageminPngquant = require("imagemin-pngquant");
const imageminMozjpeg = require("imagemin-mozjpeg");
const imageminGifsicle = require("imagemin-gifsicle");
const got = require("got");
const initServer = (request, response) => {
let requestURL = url.parse(request.url, true);
if (requestURL.search !== null) {
let { file, quality } = requestURL.query;
if (quality === "") {
response.setHeader("Content-Type", "application/json");
response.end(
JSON.stringify(
[
{
error: true,
msg: `Found quality but it need an value [quality = 1-100]`
}
],
null,
3
)
);
} else if (quality === undefined) {
response.setHeader("Content-Type", "application/json");
response.end(
JSON.stringify(
[
{
error: true,
msg: `Require quality [ quality = 1-100 ]`
}
],
null,
3
)
);
} else if (isNaN(quality)) {
response.setHeader("Content-Type", "application/json");
response.end(
JSON.stringify(
[
{
error: true,
msg: `quality should be number type not ${typeof quality} [ quality = 1-100 ]`
}
],
null,
3
)
);
} else if (quality == 0 || quality > 100) {
response.setHeader("Content-Type", "application/json");
response.end(
JSON.stringify(
[
{
error: true,
msg: `quality in between 1 & 100 not ${quality} [ quality = 1-100 ]`
}
],
null,
3
)
);
} else if (file === "") {
response.setHeader("Content-Type", "application/json");
response.end(
JSON.stringify(
[
{
error: true,
msg: `Found file but it need an value [file = 'https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png']`
}
],
null,
3
)
);
} else if (file === undefined) {
response.setHeader("Content-Type", "application/json");
response.end(
JSON.stringify(
[
{
error: true,
msg: `Require file [ file = 'https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png' ]`
}
],
null,
3
)
);
} else if (url.parse(file).hostname === null) {
response.setHeader("Content-Type", "application/json");
response.end(
JSON.stringify(
[
{
error: true,
msg: `Invalid file url [ file = 'https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png' ]`
}
],
null,
3
)
);
} else {
quality = parseInt(quality)
const optimizationLevel = 2
got
.get(file, { encoding: null })
.then(imageResponse => {
imagemin
.buffer(imageResponse.body, {
plugins: [
imageminGifsicle({ optimizationLevel, interlaced: true }),
imageminMozjpeg({ quality}),
imageminJpegtran({ progressive: true }),
imageminPngquant({ quality: [quality/100,quality/100],speed: 5 })
]
})
.then(compressedImage => {
return response.end(compressedImage);
})
.catch(error => {
return response.end(
JSON.stringify(
[
{
error: true,
msg: `Could not compress image from ${file} ${error}`
}
],
null,
3
)
);
});
})
.catch(error => {
return response.end(
JSON.stringify(
[
{
error: true,
msg: `Could not load image from ${file} ${error}`
}
],
null,
3
)
);
});
}
} else {
response.setHeader("Content-Type", "application/json");
response.end(
JSON.stringify(
[
{
error: true,
msg: "Require image [file='https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png'] and quality [ quality=1-100 ]"
}
],
null,
3
)
);
}
};
const server = app.createServer(initServer);
server.listen(process.env.PORT || 3000, "0.0.0.0");