-
Notifications
You must be signed in to change notification settings - Fork 0
/
markdown.ts
50 lines (44 loc) · 1.38 KB
/
markdown.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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import middy from "middy";
import { jsonBodyParser, cors } from "middy/middlewares";
import {
encodeResponse,
jsonErrorHandler,
validateRequestBody
} from "@internote/lib/middlewares";
import { success } from "@internote/lib/responses";
import { CreateHandler } from "@internote/lib/types";
import { CreateExportDTO, ExportResponseDTO } from "./types";
import { serialize } from "./serializers/markdown";
import { required, isString } from "@internote/lib/validator";
import AWS from "aws-sdk";
import md5 from "md5";
const validator = validateRequestBody<CreateExportDTO>({
title: [required, isString],
content: [required]
});
const markdown: CreateHandler<CreateExportDTO> = async (
event,
_ctx,
callback
) => {
const S3 = new AWS.S3();
const { title, content } = event.body;
const output = serialize(content.document);
const hash = md5(output);
const S3UploadPath = `${title} - ${hash}.md`;
await S3.upload({
Bucket: process.env.EXPORT_BUCKET,
Key: S3UploadPath,
Body: output,
ACL: "public-read"
}).promise();
const src = `https://s3-${process.env.REGION}.amazonaws.com/${process.env.EXPORT_BUCKET}/${S3UploadPath}`;
const response: ExportResponseDTO = { src };
return callback(null, success(response));
};
export const handler = middy(markdown)
.use(jsonBodyParser())
.use(validator)
.use(encodeResponse())
.use(jsonErrorHandler())
.use(cors());