From e2e7babee0de35385eb74830c82eaccdb28f013a Mon Sep 17 00:00:00 2001
From: syuilo <syuilotan@yahoo.co.jp>
Date: Fri, 13 Apr 2018 11:44:39 +0900
Subject: [PATCH] wip

---
 src/server/api/api-handler.ts    | 6 ++++--
 src/server/api/call.ts           | 7 +++----
 src/server/api/common/signin.ts  | 4 +++-
 src/server/api/index.ts          | 3 ++-
 src/server/api/private/signin.ts | 4 ++--
 5 files changed, 14 insertions(+), 10 deletions(-)

diff --git a/src/server/api/api-handler.ts b/src/server/api/api-handler.ts
index 947794a20e..e716dcdc01 100644
--- a/src/server/api/api-handler.ts
+++ b/src/server/api/api-handler.ts
@@ -7,6 +7,8 @@ import { IUser } from '../../models/user';
 import { IApp } from '../../models/app';
 
 export default async (endpoint: Endpoint, ctx: Koa.Context) => {
+	const body = ctx.is('multipart/form-data') ? (ctx.req as any).body : ctx.request.body;
+
 	const reply = (x?: any, y?: any) => {
 		if (x === undefined) {
 			ctx.status = 204;
@@ -25,7 +27,7 @@ export default async (endpoint: Endpoint, ctx: Koa.Context) => {
 
 	// Authentication
 	try {
-		[user, app] = await authenticate(ctx.request.body['i']);
+		[user, app] = await authenticate(body['i']);
 	} catch (e) {
 		reply(403, 'AUTHENTICATION_FAILED');
 		return;
@@ -35,7 +37,7 @@ export default async (endpoint: Endpoint, ctx: Koa.Context) => {
 
 	// API invoking
 	try {
-		res = await call(endpoint, user, app, ctx.request.body, ctx.req);
+		res = await call(endpoint, user, app, body, (ctx.req as any).file);
 	} catch (e) {
 		reply(400, e);
 		return;
diff --git a/src/server/api/call.ts b/src/server/api/call.ts
index cc40294657..713add566a 100644
--- a/src/server/api/call.ts
+++ b/src/server/api/call.ts
@@ -1,4 +1,3 @@
-import * as http from 'http';
 import * as multer from 'koa-multer';
 
 import endpoints, { Endpoint } from './endpoints';
@@ -6,7 +5,7 @@ import limitter from './limitter';
 import { IUser } from '../../models/user';
 import { IApp } from '../../models/app';
 
-export default (endpoint: string | Endpoint, user: IUser, app: IApp, data: any, req?: http.IncomingMessage) => new Promise<any>(async (ok, rej) => {
+export default (endpoint: string | Endpoint, user: IUser, app: IApp, data: any, file?: any) => new Promise<any>(async (ok, rej) => {
 	const isSecure = user != null && app == null;
 
 	const ep = typeof endpoint == 'string' ? endpoints.find(e => e.name == endpoint) : endpoint;
@@ -36,8 +35,8 @@ export default (endpoint: string | Endpoint, user: IUser, app: IApp, data: any,
 
 	let exec = require(`${__dirname}/endpoints/${ep.name}`);
 
-	if (ep.withFile && req) {
-		exec = exec.bind(null, (req as multer.MulterIncomingMessage).file);
+	if (ep.withFile && file) {
+		exec = exec.bind(null, file);
 	}
 
 	let res;
diff --git a/src/server/api/common/signin.ts b/src/server/api/common/signin.ts
index f57c38414c..44e1336f27 100644
--- a/src/server/api/common/signin.ts
+++ b/src/server/api/common/signin.ts
@@ -3,7 +3,7 @@ import * as Koa from 'koa';
 import config from '../../../config';
 import { ILocalUser } from '../../../models/user';
 
-export default function(ctx: Koa.Context, user: ILocalUser, redirect: boolean) {
+export default function(ctx: Koa.Context, user: ILocalUser, redirect = false) {
 	const expires = 1000 * 60 * 60 * 24 * 365; // One Year
 	ctx.cookies.set('i', user.token, {
 		path: '/',
@@ -16,5 +16,7 @@ export default function(ctx: Koa.Context, user: ILocalUser, redirect: boolean) {
 
 	if (redirect) {
 		ctx.redirect(config.url);
+	} else {
+		ctx.status = 204;
 	}
 }
diff --git a/src/server/api/index.ts b/src/server/api/index.ts
index 2ea5fccb5b..009c99acae 100644
--- a/src/server/api/index.ts
+++ b/src/server/api/index.ts
@@ -14,7 +14,8 @@ const handler = require('./api-handler').default;
 // Init app
 const app = new Koa();
 app.use(bodyParser({
-	detectJSON: () => true
+	// リクエストが multipart/form-data でない限りはJSONだと見なす
+	detectJSON: ctx => !ctx.is('multipart/form-data')
 }));
 
 // Init multer instance
diff --git a/src/server/api/private/signin.ts b/src/server/api/private/signin.ts
index 1737007206..5450c7ad27 100644
--- a/src/server/api/private/signin.ts
+++ b/src/server/api/private/signin.ts
@@ -60,14 +60,14 @@ export default async (ctx: Koa.Context) => {
 			});
 
 			if (verified) {
-				signin(ctx, user, false);
+				signin(ctx, user);
 			} else {
 				ctx.throw(400, {
 					error: 'invalid token'
 				});
 			}
 		} else {
-			signin(ctx, user, false);
+			signin(ctx, user);
 		}
 	} else {
 		ctx.throw(400, {