This repository has been archived on 2022-08-14. You can view files and clone it, but cannot push or open issues or pull requests.
outline/server/api/user.js

49 lines
1.3 KiB
JavaScript
Raw Normal View History

// @flow
2016-05-20 06:51:22 +00:00
import uuid from 'uuid';
2016-04-29 05:25:37 +00:00
import Router from 'koa-router';
2017-04-27 04:47:03 +00:00
import { makePolicy, signPolicy } from '../utils/s3';
2016-08-27 17:48:56 +00:00
import auth from './middlewares/authentication';
2016-04-29 05:25:37 +00:00
import { presentUser } from '../presenters';
const router = new Router();
2017-04-27 04:47:03 +00:00
router.post('user.info', auth(), async ctx => {
ctx.body = { data: await presentUser(ctx, ctx.state.user) };
2016-05-20 06:51:22 +00:00
});
2017-04-27 04:47:03 +00:00
router.post('user.s3Upload', auth(), async ctx => {
2016-08-14 10:04:47 +00:00
const { filename, kind, size } = ctx.body;
2016-05-20 06:51:22 +00:00
ctx.assertPresent(filename, 'filename is required');
ctx.assertPresent(kind, 'kind is required');
ctx.assertPresent(size, 'size is required');
const s3Key = uuid.v4();
2017-10-31 05:35:30 +00:00
const key = `uploads/${ctx.state.user.id}/${s3Key}/${filename}`;
2016-05-20 06:51:22 +00:00
const policy = makePolicy();
2017-04-27 04:47:03 +00:00
ctx.body = {
data: {
maxUploadSize: process.env.AWS_S3_UPLOAD_MAX_SIZE,
uploadUrl: process.env.AWS_S3_UPLOAD_BUCKET_URL,
form: {
AWSAccessKeyId: process.env.AWS_ACCESS_KEY_ID,
'Cache-Control': 'max-age=31557600',
'Content-Type': kind,
key,
acl: 'public-read',
signature: signPolicy(policy),
policy,
},
asset: {
contentType: kind,
2017-10-30 06:52:09 +00:00
url: `${process.env.AWS_S3_UPLOAD_BUCKET_URL}${key}`,
2017-04-27 04:47:03 +00:00
name: filename,
size,
},
2016-05-20 06:51:22 +00:00
},
2017-04-27 04:47:03 +00:00
};
2016-04-29 05:25:37 +00:00
});
2016-07-22 07:11:54 +00:00
export default router;