-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.ts
More file actions
198 lines (177 loc) · 6.13 KB
/
handler.ts
File metadata and controls
198 lines (177 loc) · 6.13 KB
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
187
188
189
190
191
192
193
194
195
196
197
198
import { Handler, Context, Callback } from 'aws-lambda';
import logger from './logger';
import Resizer from './resizer';
const HttpStatus = require('http-status-codes');
const minSize: number = 100;
const maxSize: number = 1200;
const limitRatio: number = 2;
interface JsonReturn {
statusCode: number;
body: string;
}
const jsonIse: JsonReturn = {
statusCode: HttpStatus.BAD_REQUEST,
body: JSON.stringify({ msg: 'Illegal size Exception' }),
};
const jsonIee: JsonReturn = {
statusCode: HttpStatus.BAD_REQUEST,
body: JSON.stringify({ msg: 'Illegal extension Exception' }),
};
const notSupportUrl: JsonReturn = {
statusCode: HttpStatus.BAD_REQUEST,
body: JSON.stringify({ msg: 'Not Support URL' }),
};
interface ReturnType {
statusCode: number;
headers: {
Location: string;
};
}
interface ErrorType {
statusCode: number;
body: string;
}
const imgExtension: RegExp = /(.*?)\.(jpg|JPG|png|PNG|gif|GIF)$/;
const redirecturl: string | undefined = process.env.redirect;
const resizer: Resizer = new Resizer();
const resolve: Handler = async (event: any, _context: Context, callback: Callback) => {
const req: any = event.pathParameters || {};
const path: string = event.path || '';
logger.info('resolve ' + path);
logger.info('source ip ' + (event.requestContext?.identity?.sourceIp ?? 'unknown'));
logger.info('call userAgent ' + (event.requestContext?.identity?.userAgent ?? 'unknown'));
if (path.startsWith('/resizeImages')) {
const files: string = req.files;
const W: number = parseInt(req.width, 10);
if (isNaN(W) || W < minSize || W > maxSize) {
logger.error('resizeImages Illegal Argument size policy error');
return jsonIse;
} else if (!imgExtension.test(files)) {
logger.error('resizeImages Illegal Argument extension policy error');
return jsonIee;
}
await resizer
.resizeImages(files, W)
.then((data: any) => {
const redirect: string = (redirecturl ?? '') + data.path;
logger.info('http code ' + data.status);
const response: ReturnType = {
statusCode: data.status,
headers: {
Location: redirect,
},
};
logger.info('redirect to ' + redirect);
callback(null, response);
})
.catch((err: any) => {
logger.error('resizeImages error ' + err);
const errorjson: ErrorType = {
statusCode: err,
body: JSON.stringify({ msg: HttpStatus.getStatusText(err) }),
};
callback(null, errorjson);
});
} else if (path.startsWith('/convertSizeImages')) {
const files: string = req.files;
const W: number = parseInt(req.width, 10);
const H: number = parseInt(req.height, 10);
if (isNaN(W) || isNaN(H) || H > W * limitRatio || W > H * limitRatio) {
logger.error('convertSizeImages Illegal Argument size policy error');
return jsonIse;
} else if (!imgExtension.test(files)) {
logger.error('convertSizeImages Illegal Argument extension policy error');
return jsonIee;
}
await resizer
.convertSizeImages(files, W, H)
.then((data: any) => {
const redirect: string = (redirecturl ?? '') + data.path;
logger.info('http code ' + data.status);
const response: ReturnType = {
statusCode: data.status,
headers: {
Location: redirect,
},
};
logger.info('redirect to ' + redirect);
callback(null, response);
})
.catch((err: any) => {
logger.error('convertSizeImages error ' + err);
const errorjson: ErrorType = {
statusCode: err,
body: JSON.stringify({ msg: HttpStatus.getStatusText(err) }),
};
callback(null, errorjson);
});
} else if (path.startsWith('/rotateImages')) {
const files: string = req.files;
const angle: number = parseInt(req.angle, 10);
if (isNaN(angle) || angle <= 0 || angle >= 360 || !imgExtension.test(files)) {
logger.error('rotateImages Illegal Argument size/ext policy error');
return jsonIse;
} else if (!imgExtension.test(files)) {
logger.error('rotateImages Illegal Argument extension policy error');
return jsonIee;
}
await resizer
.rotateImages(files, angle)
.then((data: any) => {
const redirect: string = (redirecturl ?? '') + data.path;
logger.info('http code ' + data.status);
const response: ReturnType = {
statusCode: data.status,
headers: {
Location: redirect,
},
};
logger.info('redirect to ' + redirect);
callback(null, response);
})
.catch((err: any) => {
logger.error('rotateImages error ' + err);
const errorjson: ErrorType = {
statusCode: err,
body: JSON.stringify({ msg: HttpStatus.getStatusText(err) }),
};
callback(null, errorjson);
});
} else if (path.startsWith('/fitSizeImages')) {
const files: string = req.files;
const W: number = parseInt(req.width, 10);
const H: number = parseInt(req.height, 10);
if (isNaN(W) || isNaN(H) || H > W * limitRatio || W > H * limitRatio) {
logger.error('fitSizeImages Illegal Argument size policy error');
return jsonIse;
} else if (!imgExtension.test(files)) {
logger.error('fitSizeImages Illegal Argument extension policy error');
return jsonIee;
}
await resizer
.fitSizeImages(files, W, H)
.then((data: any) => {
const redirect: string = (redirecturl ?? '') + data.path;
logger.info('http code ' + data.status);
const response: ReturnType = {
statusCode: data.status,
headers: {
Location: redirect,
},
};
logger.info('redirect to ' + redirect);
callback(null, response);
})
.catch((err: any) => {
logger.error('fitSizeImages error ' + err);
const errorjson: ErrorType = {
statusCode: err,
body: JSON.stringify({ msg: HttpStatus.getStatusText(err) }),
};
callback(null, errorjson);
});
}
// 위 if/else에 안 걸리면
return notSupportUrl;
};
export { resolve };