From 8bd1d19dbd899047bc8503dbe682358ddfc564cc Mon Sep 17 00:00:00 2001 From: Tom Harley Date: Thu, 1 Mar 2018 19:48:47 +0000 Subject: Implement GET and POST request handlers --- src/app.ts | 122 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 121 insertions(+), 1 deletion(-) (limited to 'src/app.ts') diff --git a/src/app.ts b/src/app.ts index a843179..86b0c1c 100644 --- a/src/app.ts +++ b/src/app.ts @@ -2,15 +2,20 @@ import * as bodyParser from 'body-parser'; import * as ex from 'express'; import * as logger from 'morgan'; -import { errorHandler } from './errors/errorware'; +import { RequestError, errorHandler } from './errors/errorware'; import FileRouter from './FileRouter'; +import { default as File } from './db/model/File'; +import { default as Project } from './db/model/Project'; +import { default as User } from './db/model/User'; + class App { public express: ex.Express; public logEnabled: boolean = true; public constructor() { this.express = ex(); + this.precondition(); this.middleware(); this.mountRoutes(); this.errorware(); @@ -41,6 +46,121 @@ class App { private errorware(): void { this.express.use(errorHandler); } + + private precondition(): void { + // Fetch a user from their name: + this.express.param( + 'username', + async( + req: ex.Request, res: ex.Response, next: ex.NextFunction, + name: string + ): Promise => { + const user: User | null = await User.findOne({ + where: { + username: name + } + }); + if (user === null) { + next(new RequestError(404, 'user_not_found')); + return; + } else { + res.locals.user = user; + } + next(); + } + ); + + // Fetch a project from its name: + this.express.param( + 'project_name', + async( + req: ex.Request, res: ex.Response, next: ex.NextFunction, + project: string + ): Promise => { + res.locals.project = await Project.findOne({ + where: { + name: project + } + }); + next(); + } + ); + + // Fetch a file from its path: + this.express.param( + 'file', + async( + req: ex.Request, res: ex.Response, next: ex.NextFunction, + filename: string + ): Promise => { + const fileNames: string[] = req.params.path.split('/'); + const project: Project | null = res.locals.project; + if (project === null) { + next(new RequestError(404, 'project_not_found')); + return; + } + let file: File = project.rootFolder; + // TODO Rewrite using sub-queries instead of repeated queries. + while (fileNames.length > 2) { + const fileOrNull: File | null = await File.findOne({ + where: { + name: fileNames[0], + parentFolder: file + } + }); + if (fileOrNull === null) { + next(); + return; + } else { + file = fileOrNull; + } + fileNames.splice(0, 1); + } + const parentFolder: File | null = await File.findOne({ + include: [ + { model: File, as: 'containedFiles' } + ], + where: { + parentFolder: file, + name: fileNames[0] + } + }); + if (parentFolder === null) { + next(); + return; + } + res.locals.parentFolder = parentFolder; + const tFile: File | undefined = parentFolder.containedFiles.find( + (f: File): boolean => f.name === fileNames[1] + ); + res.locals.file = tFile; + res.locals.filename = fileNames[1]; + next(); + } + ); + + // Fetch a file from its UUID: + this.express.param( + 'id', + async( + req: ex.Request, res: ex.Response, next: ex.NextFunction, + fileId: string + ): Promise => { + const file: File | null = await File.findOne({ + where: { + uuid: fileId + } + }); + if (file === null) { + next(new RequestError(404, 'file_not_found')); + return; + } else { + res.locals.file = file; + } + next(); + } + ); + } } export default new App().express; -- cgit v1.2.1 From 10e00fba97f0227a7e3d61d2725326d4a5d37bfa Mon Sep 17 00:00:00 2001 From: Tom Harley Date: Thu, 1 Mar 2018 22:06:42 +0000 Subject: Add miniature response-wrapper middleware --- src/app.ts | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'src/app.ts') diff --git a/src/app.ts b/src/app.ts index 86b0c1c..28b97a7 100644 --- a/src/app.ts +++ b/src/app.ts @@ -18,6 +18,15 @@ class App { this.precondition(); this.middleware(); this.mountRoutes(); + this.express.use( + async(req: ex.Request, res: ex.Response, next: ex.NextFunction): + Promise => { + res.json({ + status: 'success', + data: await res.locals.data + }); + } + ); this.errorware(); } -- cgit v1.2.1