From 3a02f5b8c7f3ae40072f41466c214a984885c2ac Mon Sep 17 00:00:00 2001 From: Hafeez Date: Thu, 12 Apr 2018 14:57:47 +0100 Subject: fixed precondition --- src/app.ts | 122 +------------------------------------------------------------ 1 file changed, 1 insertion(+), 121 deletions(-) (limited to 'src/app.ts') diff --git a/src/app.ts b/src/app.ts index c51c574..d8dfe5d 100644 --- a/src/app.ts +++ b/src/app.ts @@ -43,8 +43,7 @@ class App { this.express = ex(); this.express.use(( req: ex.Request, res: ex.Response, next: ex.NextFunction - ): void => { console.log(req.url); next(); }); - this.precondition(); + ): void => { console.log(req.url); next(); }); this.middleware(); this.mountRoutes(); this.express.use( @@ -126,125 +125,6 @@ class App { this.express.use(unauthorisedErr); this.express.use(errorHandler); } - - /** - * Introduces variable-preconditioning middleware to the Express app. - * - * These middleware functions pre-process Express' matching variables - * in routes (e.g. `:project_name`). - * If an entity cannot be found in the database, the corresponding - * value is set to `null` instead. - */ - 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) { - return next(new RequestError(404, 'user_not_found')); - } 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 => { - console.log("## as " + project) - 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) { - return next(new RequestError(404, 'project_not_found')); - } - 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) { - return next(); - } 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) { - return next(); - } - 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) { - return next(new RequestError(404, 'file_not_found')); - } else { - res.locals.file = file; - } - next(); - } - ); - } } export default new App(true).express; -- cgit v1.2.1