Last active
January 9, 2020 22:06
-
-
Save zavr-1/b91adefcb18cfa9f121e7b0c9a24a4b0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { app, url, middleware, router } = await idio({ | |
cors: { | |
use: true, | |
origin: PROD && [FRONT_END, HOST], | |
credentials: true, | |
}, | |
compress: { use: true }, | |
form: { | |
dest: 'upload', | |
}, | |
frontend: { use: !PROD }, | |
static: [{ use: CLOSURE, root: 'docs' }, { | |
use: true, | |
root: 'upload', | |
}], | |
session: { keys: [SESSION_KEY] }, | |
}) | |
router.post('/upload', | |
// 1. enable session | |
middleware.session, | |
// 2. validate session existance | |
(ctx, next) => { | |
if (!ctx.session.github_user) throw new Error('!Authorisation required.') | |
return next() | |
}, | |
// 3. read incoming message stream with photo form data | |
(ctx, next) => middleware.form.single('image')(ctx, next), | |
// 4. ensure token from the query | |
middleware.csrf, | |
// 5. process the photo | |
async (ctx) => { | |
const { ext } = parse(ctx.file.originalname) | |
ctx.body = { | |
photoId: sync(18), | |
success: 1, | |
result: `/upload/${ctx.file.filename}${ext}`, | |
} | |
} | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment