-
Notifications
You must be signed in to change notification settings - Fork 0
/
attachment.ts
27 lines (23 loc) · 1.06 KB
/
attachment.ts
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
import * as express from 'express';
import { query, validationResult } from 'express-validator';
import sql from 'sql-template-strings';
import { ApiResponse } from '../apiResponse';
import s3Client from '../aws/s3';
import db from '../db';
import allowedMethods from '../middlewares/allowedMethods';
const router = express.Router();
router.all('/', query('id').isString(), allowedMethods('GET'), async (req, res) => {
const errors = validationResult(req);
if (!errors.isEmpty()) return new ApiResponse(res).validationError(errors);
const id = req.query.id as string;
const query = await db.query(sql`
SELECT attachment FROM messages WHERE id = $1 LIMIT 1;
`, [ id ]);
if (query.rowCount == 0) return new ApiResponse(res).notFound('Message does not exist');
const attachment = query.rows[0].attachment;
if (!attachment) return new ApiResponse(res).badRequest('This message doesn\'t have an attachment');
const url = await s3Client.getSingedUrl(attachment);
res.header('Cache-Control', s3Client.cacheControlHeader);
res.redirect(302, url);
});
export default router;