From 3a95396b773c3bfb5f8a407e256f98d926297002 Mon Sep 17 00:00:00 2001 From: Tom Harley Date: Tue, 10 Apr 2018 18:18:12 +0100 Subject: WIP: Add support for raw and meta views --- src/FileRouter.ts | 33 +++++++++++++-------------------- 1 file changed, 13 insertions(+), 20 deletions(-) (limited to 'src/FileRouter.ts') diff --git a/src/FileRouter.ts b/src/FileRouter.ts index 4d6eb16..ee10662 100644 --- a/src/FileRouter.ts +++ b/src/FileRouter.ts @@ -8,7 +8,7 @@ import { default as UserGroup } from './db/model/UserGroup'; import { NextFunction, Request, Response, Router } from 'express'; import { RequestError } from './errors/errorware'; -import { files } from './files'; +import { View, files, views } from './files'; import { logger } from './logger'; import { Property, default as serverConfig } from './serverConfig'; import { uuid } from './uuid'; @@ -268,7 +268,7 @@ const getUserProperties: Middleware = */ const getUsername: Middleware = (req: Request, res: Response, next: NextFunction): void => { - if (res.locals.user === null) { + if (res.locals.user == null) { next(new RequestError(404, 'user_not_found')); return; } @@ -473,36 +473,29 @@ const getProjectProperties: Middleware = /** * GET a File. */ -const getFilePath: Middleware = async( +const getFile: Middleware = async( req: Request, res: Response, next: NextFunction ): Promise => { const file: File | null = res.locals.file; - const project: Project | null = res.locals.project; if (file == null) { return next(new RequestError(404, 'file_not_found')); - } else if (project == null) { - return next(new RequestError(404, 'project_not_found')); } - res.locals.function = res.sendFile; - res.locals.data = - `${BASE_FILE_STORAGE}/${res.locals.project.name}/${file.uuid}`; + const project: Project = res.locals.project; + const view: View = views[req.query.view || 'meta']; + res.locals.function = view.getResponseFunction(req, res); + res.locals.data = view.getResponseData(file, project, req.query); next(); }; /** * GET a File. */ -const getFileId: Middleware = - (req: Request, res: Response, next: NextFunction): void => { - const file: File | null = res.locals.file; - if (file === null) { - next(new RequestError(404, 'file_not_found')); - return; - } - res.locals.function = res.sendFile; - res.locals.data = files.path(req.params.id, res.locals.project.name); - next(); -}; +const getFilePath: Middleware = getFile; + +/** + * GET a File. + */ +const getFileId: Middleware = getFile; /** * Post a File. / delete / move / etc -- cgit v1.2.1 From f7220be4e1b80cdba0fbaf41f7382cbf18561e2b Mon Sep 17 00:00:00 2001 From: jw255 Date: Tue, 10 Apr 2018 23:30:51 +0100 Subject: sth works maybe --- src/FileRouter.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'src/FileRouter.ts') diff --git a/src/FileRouter.ts b/src/FileRouter.ts index 4d6eb16..4d68b6d 100644 --- a/src/FileRouter.ts +++ b/src/FileRouter.ts @@ -63,7 +63,7 @@ const getProtocols: Middleware = const postLog: Middleware = (req: Request, res: Response, next: NextFunction): void => { if (!req.user.hasPrivilege('logging')) { - next(new RequestError(400, 'invalid_privilege')); + next(new RequestError(401, 'invalid_privilege')); return; } else if (!logger.isEnabled()) { next(new RequestError(501, 'logging_not_enabled')); @@ -103,7 +103,7 @@ const postLog: Middleware = const getLog: Middleware = (req: Request, res: Response, next: NextFunction): void => { if (!req.user.hasPrivilege('admin')) { - next(new RequestError(400, 'invalid_privilege')); + next(new RequestError(401, 'invalid_privilege')); } res.locals.data = logger.fetch( req.params.level, { @@ -416,9 +416,11 @@ const getProjectName: Middleware = async(req: Request, res: Response, next: NextFunction): Promise => { const project: Project | null = res.locals.project; if (project == null) { + console.log("was null") return next(new RequestError(404, 'project_not_found')); } else { res.locals.data = project.fullInfo; + console.log("wasnt " + res.locals.data) next(); } }; -- cgit v1.2.1 From 59d2c5c4b1dbc929f4f22192b781c0acd35eb78e Mon Sep 17 00:00:00 2001 From: jw255 Date: Wed, 11 Apr 2018 00:06:25 +0100 Subject: oh god --- src/FileRouter.ts | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'src/FileRouter.ts') diff --git a/src/FileRouter.ts b/src/FileRouter.ts index 4d68b6d..630f64f 100644 --- a/src/FileRouter.ts +++ b/src/FileRouter.ts @@ -444,8 +444,10 @@ const postProjectName: Middleware = let promise: PromiseLike | null = null; if (project == null) { const file: File = new File({ + uuid: uuid.generate(), mimetype: 'inode/directory' }); + console.log(req.params.project_name) promise = file.save(); project = new Project({ name: req.params.project_name, @@ -454,8 +456,11 @@ const postProjectName: Middleware = } project.metadata = req.body; // tslint:disable-next-line:await-promise + console.log("anything prequel") await promise; + console.log("anything") await project.save(); + console.log("anything part 2") next(); }; -- cgit v1.2.1 From 3a02f5b8c7f3ae40072f41466c214a984885c2ac Mon Sep 17 00:00:00 2001 From: Hafeez Date: Thu, 12 Apr 2018 14:57:47 +0100 Subject: fixed precondition --- src/FileRouter.ts | 121 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 121 insertions(+) (limited to 'src/FileRouter.ts') diff --git a/src/FileRouter.ts b/src/FileRouter.ts index d922023..f01e1a0 100644 --- a/src/FileRouter.ts +++ b/src/FileRouter.ts @@ -532,11 +532,132 @@ export class FileRouter { this.init(); } + /** + * 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.router.param( + 'username', + async( + req: Request, res: Response, next: 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.router.param( + 'project_name', + async( + req: Request, res: Response, next: 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.router.param( + 'file', + async( + req: Request, res: Response, next: 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.router.param( + 'id', + async( + req: Request, res: Response, next: 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(); + } + ); + } + /** * Take each handler, and attach to one of the Express.Router's * endpoints. */ public init(): void { + this.precondition(); + // General this.router.get ('/_supported_protocols_', getProtocols); this.router.get ('/log', getLog); -- cgit v1.2.1 From 22efa1b090df7a427bfc1990d705624ae53d6231 Mon Sep 17 00:00:00 2001 From: Hafeez Date: Thu, 12 Apr 2018 15:30:34 +0100 Subject: not much progress tbh --- src/FileRouter.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'src/FileRouter.ts') diff --git a/src/FileRouter.ts b/src/FileRouter.ts index f01e1a0..d6d49da 100644 --- a/src/FileRouter.ts +++ b/src/FileRouter.ts @@ -419,6 +419,7 @@ const getProjectName: Middleware = console.log("was null") return next(new RequestError(404, 'project_not_found')); } else { + console.log(project.contributors); res.locals.data = project.fullInfo; console.log("wasnt " + res.locals.data) next(); @@ -568,13 +569,13 @@ export class FileRouter { async( req: Request, res: Response, next: NextFunction, project: string - ): Promise => { - console.log("## as " + project) + ): Promise => { res.locals.project = await Project.findOne({ where: { name: project } }); + console.log("## as " + res.locals.project); next(); } ); -- cgit v1.2.1 From 4430d61b381d0b3dce4831fea1a9faa78d9efdf6 Mon Sep 17 00:00:00 2001 From: jw255 Date: Fri, 13 Apr 2018 14:25:42 +0100 Subject: changed 2 debug msgs --- src/FileRouter.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/FileRouter.ts') diff --git a/src/FileRouter.ts b/src/FileRouter.ts index f01e1a0..ddcb3d5 100644 --- a/src/FileRouter.ts +++ b/src/FileRouter.ts @@ -569,7 +569,7 @@ export class FileRouter { req: Request, res: Response, next: NextFunction, project: string ): Promise => { - console.log("## as " + project) + logger.debug('Retrieving project \'${project}\''); res.locals.project = await Project.findOne({ where: { name: project -- cgit v1.2.1 From 01f3bde6fc51c398a36b42215c4056990821991b Mon Sep 17 00:00:00 2001 From: Hafeez Date: Fri, 13 Apr 2018 14:44:22 +0100 Subject: datebase retrieves all properties --- src/FileRouter.ts | 1 + 1 file changed, 1 insertion(+) (limited to 'src/FileRouter.ts') diff --git a/src/FileRouter.ts b/src/FileRouter.ts index aa13872..619be9e 100644 --- a/src/FileRouter.ts +++ b/src/FileRouter.ts @@ -572,6 +572,7 @@ export class FileRouter { ): Promise => { logger.debug('Retrieving project \'${project}\''); res.locals.project = await Project.findOne({ + include: [{all: true}], where: { name: project } -- cgit v1.2.1 From 8f013438e4e4c96594b7ed567e7a58dfc1b7ce5a Mon Sep 17 00:00:00 2001 From: Hafeez Date: Fri, 13 Apr 2018 15:53:10 +0100 Subject: fixed mock file tests --- src/FileRouter.ts | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) (limited to 'src/FileRouter.ts') diff --git a/src/FileRouter.ts b/src/FileRouter.ts index 619be9e..b76dea7 100644 --- a/src/FileRouter.ts +++ b/src/FileRouter.ts @@ -415,13 +415,10 @@ const getProjects: Middleware = const getProjectName: Middleware = async(req: Request, res: Response, next: NextFunction): Promise => { const project: Project | null = res.locals.project; - if (project == null) { - console.log("was null") + if (project == null) { return next(new RequestError(404, 'project_not_found')); - } else { - console.log(project.contributors); - res.locals.data = project.fullInfo; - console.log("wasnt " + res.locals.data) + } else { + res.locals.data = project.fullInfo; next(); } }; @@ -639,6 +636,7 @@ export class FileRouter { fileId: string ): Promise => { const file: File | null = await File.findOne({ + include: [{all: true}], where: { uuid: fileId } -- cgit v1.2.1 From deab81f8ef469c4119838d72b56683b8dd4cbcfe Mon Sep 17 00:00:00 2001 From: jw255 Date: Fri, 13 Apr 2018 16:10:32 +0100 Subject: fixed some privs --- src/FileRouter.ts | 1 + 1 file changed, 1 insertion(+) (limited to 'src/FileRouter.ts') diff --git a/src/FileRouter.ts b/src/FileRouter.ts index 619be9e..7db7eb2 100644 --- a/src/FileRouter.ts +++ b/src/FileRouter.ts @@ -550,6 +550,7 @@ export class FileRouter { name: string ): Promise => { const user: User | null = await User.findOne({ + include: [{all: true}], where: { username: name } -- cgit v1.2.1 From 244fa29c38dcae5732a06cdb39de1a2a00557117 Mon Sep 17 00:00:00 2001 From: Tom Harley Date: Fri, 13 Apr 2018 16:12:04 +0100 Subject: Fix minor places, and add Postman collection --- src/FileRouter.ts | 194 ++++++++++++++++++++++++++++++++---------------------- 1 file changed, 115 insertions(+), 79 deletions(-) (limited to 'src/FileRouter.ts') diff --git a/src/FileRouter.ts b/src/FileRouter.ts index d6d49da..bbcc0f2 100644 --- a/src/FileRouter.ts +++ b/src/FileRouter.ts @@ -32,8 +32,10 @@ export type Middleware = * "required": array(string) * }) */ -const getProtocols: Middleware = - (req: Request, res: Response, next: NextFunction): void => { +const getProtocols: Middleware = ( + req: Request, res: Response, next: NextFunction +): void => { + logger.debug('Fetching supported protocols'); res.locals.data = { supported: [], required: [ @@ -60,8 +62,10 @@ const getProtocols: Middleware = * "value": anything * }) */ -const postLog: Middleware = - (req: Request, res: Response, next: NextFunction): void => { +const postLog: Middleware = ( + req: Request, res: Response, next: NextFunction +): void => { + logger.debug('Adding forwarded log'); if (!req.user.hasPrivilege('logging')) { next(new RequestError(401, 'invalid_privilege')); return; @@ -100,8 +104,10 @@ const postLog: Middleware = * "timestamp": string * }) */ -const getLog: Middleware = - (req: Request, res: Response, next: NextFunction): void => { +const getLog: Middleware = ( + req: Request, res: Response, next: NextFunction +): void => { + logger.debug('Fetching log'); if (!req.user.hasPrivilege('admin')) { next(new RequestError(401, 'invalid_privilege')); } @@ -135,8 +141,10 @@ const getLog: Middleware = * "value": alternative([string, integer, boolean]) * }) */ -const getProperties: Middleware = - (req: Request, res: Response, next: NextFunction): void => { +const getProperties: Middleware = ( + req: Request, res: Response, next: NextFunction +): void => { + logger.debug('Listing server properties'); res.locals.data = serverConfig; next(); }; @@ -153,8 +161,10 @@ const getProperties: Middleware = * "value": alternative([string, integer, boolean]) * }) */ -const postProperties: Middleware = - (req: Request, res: Response, next: NextFunction): void => { +const postProperties: Middleware = ( + req: Request, res: Response, next: NextFunction +): void => { + logger.debug('Editing server properties'); for (const newProp of req.body) { const prop: Property | undefined = serverConfig.find( (p: Property): boolean => p.id === newProp.id @@ -188,8 +198,10 @@ const postProperties: Middleware = * "internal": boolean * }) */ -const getUserPrivileges: Middleware = - async(req: Request, res: Response, next: NextFunction): Promise => { +const getUserPrivileges: Middleware = async( + req: Request, res: Response, next: NextFunction +): Promise => { + logger.debug('Listing all user privileges'); const usergroups: UserGroup[] = await UserGroup.findAll(); res.locals.data = usergroups.map( (ug: UserGroup) => ug.getPrivilege() @@ -220,6 +232,7 @@ const getUserPrivileges: Middleware = const getUsers: Middleware = async( req: Request, res: Response, next: NextFunction ): Promise => { + logger.debug('Listing all registered users'); const users: User[] = await User.findAll({ include: [ UserGroup, @@ -240,8 +253,10 @@ const getUsers: Middleware = async( /** * Expose additional configuration options for individual users */ -const getUserProperties: Middleware = - (req: Request, res: Response, next: NextFunction): void => { +const getUserProperties: Middleware = ( + req: Request, res: Response, next: NextFunction +): void => { + logger.debug('Listing additional properties for a user'); res.locals.data = res.locals.user.properties; next(); }; @@ -266,8 +281,10 @@ const getUserProperties: Middleware = * "private_admin_metadata": metadata * } */ -const getUsername: Middleware = - (req: Request, res: Response, next: NextFunction): void => { +const getUsername: Middleware = ( + req: Request, res: Response, next: NextFunction +): void => { + logger.debug(`Listing user ${res.locals.user}`); if (res.locals.user == null) { next(new RequestError(404, 'user_not_found')); return; @@ -276,8 +293,10 @@ const getUsername: Middleware = next(); }; -const getCurUser: Middleware = - (req: Request, res: Response, next: NextFunction): void => { +const getCurUser: Middleware = ( + req: Request, res: Response, next: NextFunction +): void => { + logger.debug('Listing current user'); res.locals.data = req.user.fullInfo; next(); }; @@ -300,8 +319,10 @@ const getCurUser: Middleware = * "private_admin_metadata": not_present * } */ -const postCurUser: Middleware = - async(req: Request, res: Response, next: NextFunction): Promise => { +const postCurUser: Middleware = async( + req: Request, res: Response, next: NextFunction +): Promise => { + logger.debug('Editing current user'); try { await req.user.updateInfo(req.body); await req.user.save(); @@ -328,15 +349,18 @@ const postCurUser: Middleware = * "private_admin_metadata": optional(metadata) * } */ -const postUsername: Middleware = - async(req: Request, res: Response, next: NextFunction): Promise => { +const postUsername: Middleware = async( + req: Request, res: Response, next: NextFunction +): Promise => { let user: User | null | undefined = res.locals.user; if (user == null) { + logger.debug('Adding new user'); user = new User({ username: req.params.username, password: req.params.password }); } else { + logger.debug('Editing user'); user.password = req.params.password; } user.metadata = req.body; @@ -354,8 +378,10 @@ const postUsername: Middleware = * "internal": boolean * }) */ -const getProjectRoles: Middleware = - async(req: Request, res: Response, next: NextFunction): Promise => { +const getProjectRoles: Middleware = async( + req: Request, res: Response, next: NextFunction +): Promise => { + logger.debug('Listing available project roles'); const roles: ContributorGroup[] = await ContributorGroup.findAll(); res.locals.data = roles.filter( (cg: ContributorGroup): boolean => !cg.isInternal @@ -382,8 +408,10 @@ const getProjectRoles: Middleware = * "admin_metadata": optional(metadata) * }) */ -const getProjects: Middleware = - async(req: Request, res: Response, next: NextFunction): Promise => { +const getProjects: Middleware = async( + req: Request, res: Response, next: NextFunction +): Promise => { + logger.debug('Listing all projects'); const projects: Project[] = await Project.findAll({ include: [ User @@ -412,16 +440,15 @@ const getProjects: Middleware = * "admin_metadata": optional(metadata) * } */ -const getProjectName: Middleware = - async(req: Request, res: Response, next: NextFunction): Promise => { +const getProjectName: Middleware = async( + req: Request, res: Response, next: NextFunction +): Promise => { + logger.debug('Getting project properties'); const project: Project | null = res.locals.project; if (project == null) { - console.log("was null") return next(new RequestError(404, 'project_not_found')); } else { - console.log(project.contributors); res.locals.data = project.fullInfo; - console.log("wasnt " + res.locals.data) next(); } }; @@ -439,37 +466,39 @@ const getProjectName: Middleware = * "admin_metadata": optional(metadata) * } */ -const postProjectName: Middleware = - async(req: Request, res: Response, next: NextFunction): Promise => { +const postProjectName: Middleware = async( + req: Request, res: Response, next: NextFunction +): Promise => { let project: Project | null = res.locals.project; let promise: PromiseLike | null = null; if (project == null) { + logger.debug('Adding new project'); const file: File = new File({ uuid: uuid.generate(), mimetype: 'inode/directory' }); - console.log(req.params.project_name) promise = file.save(); project = new Project({ name: req.params.project_name, rootFolder: file }); + } else { + logger.debug('Updating project properties'); } project.metadata = req.body; // tslint:disable-next-line:await-promise - console.log("anything prequel") await promise; - console.log("anything") await project.save(); - console.log("anything part 2") next(); }; /** * additional configuration options for individual projects */ -const getProjectProperties: Middleware = - (req: Request, res: Response, next: NextFunction): void => { +const getProjectProperties: Middleware = ( + req: Request, res: Response, next: NextFunction +): void => { + logger.debug('Getting project properties'); if (res.locals.project == null) { next(new RequestError(404, 'project_not_found')); } else { @@ -484,6 +513,7 @@ const getProjectProperties: Middleware = const getFile: Middleware = async( req: Request, res: Response, next: NextFunction ): Promise => { + logger.debug(`Getting file in ${req.query.view} view`); const file: File | null = res.locals.file; if (file == null) { return next(new RequestError(404, 'file_not_found')); @@ -508,8 +538,10 @@ const getFileId: Middleware = getFile; /** * Post a File. / delete / move / etc */ -const postFilePath: Middleware = - (req: Request, res: Response, next: NextFunction): void => { +const postFilePath: Middleware = ( + req: Request, res: Response, next: NextFunction +): void => { + logger.debug('Receiving file'); if (res.locals.file != null) { next(new RequestError(400, 'file_exists')); } @@ -533,6 +565,39 @@ export class FileRouter { this.init(); } + /** + * Take each handler, and attach to one of the Express.Router's + * endpoints. + */ + public init(): void { + this.precondition(); + + // General + this.router.get ('/_supported_protocols_', getProtocols); + this.router.get ('/log', getLog); + this.router.post('/log', postLog); + this.router.get ('/properties', getProperties); + this.router.post('/properties', postProperties); + // Users + this.router.get ('/user_privileges', getUserPrivileges); + this.router.get ('/users', getUsers); + this.router.get ('/users/:username', getUsername); + this.router.post('/users/:username', postUsername); + this.router.get ('/users/:username/properties', getUserProperties); + this.router.get ('/current_user', getCurUser); + this.router.post('/current_user', postCurUser); + // Projects + this.router.get ('/project_roles', getProjectRoles); + this.router.get ('/projects', getProjects); + this.router.get ('/projects/:project_name', getProjectName); + this.router.post('/projects/:project_name', postProjectName); + this.router.get ('/projects/:project_name/properties', getProjectProperties); + // File access + this.router.get ('/projects/:project_name/files/:path', getFilePath); + this.router.post('/projects/:project_name/files/:id', postFilePath); + this.router.get ('/projects/:project_name/files_by_id/:id', getFileId); + } + /** * Introduces variable-preconditioning middleware to the Express app. * @@ -542,6 +607,7 @@ export class FileRouter { * value is set to `null` instead. */ private precondition(): void { + logger.debug('Adding parameter preconditioning to router'); // Fetch a user from their name: this.router.param( 'username', @@ -549,6 +615,7 @@ export class FileRouter { req: Request, res: Response, next: NextFunction, name: string ): Promise => { + logger.debug(`Searching for user ${name}`); const user: User | null = await User.findOne({ where: { username: name @@ -568,14 +635,14 @@ export class FileRouter { 'project_name', async( req: Request, res: Response, next: NextFunction, - project: string - ): Promise => { + projectName: string + ): Promise => { + logger.debug(`Searching for project ${projectName}`); res.locals.project = await Project.findOne({ where: { - name: project + name: projectName } }); - console.log("## as " + res.locals.project); next(); } ); @@ -587,9 +654,11 @@ export class FileRouter { req: Request, res: Response, next: NextFunction, filename: string ): Promise => { + logger.debug(`Searching for file ${filename}`); const fileNames: string[] = req.params.path.split('/'); const project: Project | null = res.locals.project; if (project == null) { + logger.debug('Project was not found: skipping finding file'); return next(new RequestError(404, 'project_not_found')); } let file: File = project.rootFolder; @@ -637,6 +706,7 @@ export class FileRouter { req: Request, res: Response, next: NextFunction, fileId: string ): Promise => { + logger.debug(`Searching for file ${fileId}`); const file: File | null = await File.findOne({ where: { uuid: fileId @@ -651,43 +721,9 @@ export class FileRouter { } ); } - - /** - * Take each handler, and attach to one of the Express.Router's - * endpoints. - */ - public init(): void { - this.precondition(); - - // General - this.router.get ('/_supported_protocols_', getProtocols); - this.router.get ('/log', getLog); - this.router.post('/log', postLog); - this.router.get ('/properties', getProperties); - this.router.post('/properties', postProperties); - // Users - this.router.get ('/user_privileges', getUserPrivileges); - this.router.get ('/users', getUsers); - this.router.get ('/users/:username', getUsername); - this.router.post('/users/:username', postUsername); - this.router.get ('/users/:username/properties', getUserProperties); - this.router.get ('/current_user', getCurUser); - this.router.post('/current_user', postCurUser); - // Projects - this.router.get ('/project_roles', getProjectRoles); - this.router.get ('/projects', getProjects); - this.router.get ('/projects/:project_name', getProjectName); - this.router.post('/projects/:project_name', postProjectName); - this.router.get ('/projects/:project_name/properties', getProjectProperties); - // File access - this.router.get ('/projects/:project_name/files/:path', getFilePath); - this.router.post('/projects/:project_name/files/:id', postFilePath); - this.router.get ('/projects/:project_name/files_by_id/:id', getFileId); - } } // Create the FileRouter, and export its configured Express.Router const fileRoutes: FileRouter = new FileRouter(); -fileRoutes.init(); export default fileRoutes.router; -- cgit v1.2.1 From a7567c50bbf01e3a8489d732d2825fa01921f2d3 Mon Sep 17 00:00:00 2001 From: jw255 Date: Fri, 13 Apr 2018 16:35:50 +0100 Subject: got it down to 34 errors in the protocol --- src/FileRouter.ts | 1 + 1 file changed, 1 insertion(+) (limited to 'src/FileRouter.ts') diff --git a/src/FileRouter.ts b/src/FileRouter.ts index b750094..990110a 100644 --- a/src/FileRouter.ts +++ b/src/FileRouter.ts @@ -639,6 +639,7 @@ export class FileRouter { projectName: string ): Promise => { res.locals.project = await Project.findOne({ + include: [{all: true}], where: { name: projectName } -- cgit v1.2.1 From f2f471e9c081d94622de02a22ca2faeaf8f6647b Mon Sep 17 00:00:00 2001 From: Tom Harley Date: Fri, 13 Apr 2018 16:45:31 +0100 Subject: Fix some tests by including users in projects --- src/FileRouter.ts | 3 +++ 1 file changed, 3 insertions(+) (limited to 'src/FileRouter.ts') diff --git a/src/FileRouter.ts b/src/FileRouter.ts index b750094..48ec9c7 100644 --- a/src/FileRouter.ts +++ b/src/FileRouter.ts @@ -639,6 +639,9 @@ export class FileRouter { projectName: string ): Promise => { res.locals.project = await Project.findOne({ + include: [ + User + ], where: { name: projectName } -- cgit v1.2.1 From c9a574ec946dc7fda6637db45f3223acd0a08e9f Mon Sep 17 00:00:00 2001 From: jw255 Date: Fri, 13 Apr 2018 16:45:32 +0100 Subject: fixed user not found --- src/FileRouter.ts | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) (limited to 'src/FileRouter.ts') diff --git a/src/FileRouter.ts b/src/FileRouter.ts index 990110a..e890241 100644 --- a/src/FileRouter.ts +++ b/src/FileRouter.ts @@ -257,6 +257,10 @@ const getUserProperties: Middleware = ( req: Request, res: Response, next: NextFunction ): void => { logger.debug('Listing additional properties for a user'); + if (res.locals.user == null) { + next(new RequestError(404, 'user_not_found')); + return; + } res.locals.data = res.locals.user.properties; next(); }; @@ -354,16 +358,18 @@ const postUsername: Middleware = async( ): Promise => { let user: User | null | undefined = res.locals.user; if (user == null) { - logger.debug('Adding new user'); + console.log('Adding new user'); user = new User({ username: req.params.username, password: req.params.password }); } else { - logger.debug('Editing user'); + console.log('Editing user'); user.password = req.params.password; } - user.metadata = req.body; + if (req.body) { + user.metadata = req.body; + } next(); }; @@ -622,11 +628,7 @@ export class FileRouter { username: name } }); - if (user == null) { - return next(new RequestError(404, 'user_not_found')); - } else { - res.locals.user = user; - } + res.locals.user = user; next(); } ); -- cgit v1.2.1 From 9c0ff9a0b7d73e5e2a12c4f87d1aa586c59adec9 Mon Sep 17 00:00:00 2001 From: jw255 Date: Fri, 13 Apr 2018 21:12:53 +0100 Subject: only 12 failing tests :D --- src/FileRouter.ts | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) (limited to 'src/FileRouter.ts') diff --git a/src/FileRouter.ts b/src/FileRouter.ts index e890241..4ee42dc 100644 --- a/src/FileRouter.ts +++ b/src/FileRouter.ts @@ -547,7 +547,7 @@ const getFileId: Middleware = getFile; const postFilePath: Middleware = ( req: Request, res: Response, next: NextFunction ): void => { - logger.debug('Receiving file'); + logger.debug('Receiving file to ' + res.locals.parentFolder); if (res.locals.file != null) { next(new RequestError(400, 'file_exists')); } @@ -556,7 +556,18 @@ const postFilePath: Middleware = ( name: res.locals.filename, parentFolder: res.locals.parentFolder }); - req.pipe(files.writableStream(file.uuid, res.locals.project.name)); + /* + console.log("1: " + file.uuid + " -- " + res.locals.project.name) + try { + let x = files.writableStream(file.uuid, res.locals.project.name); + console.log("2") + req.pipe(x); + console.log("3") + } catch (err) { + console.log(err); + } + console.log("4") + */ next(); }; @@ -652,11 +663,12 @@ export class FileRouter { // Fetch a file from its path: this.router.param( - 'file', + 'path', async( req: Request, res: Response, next: NextFunction, filename: string ): Promise => { + console.log("wrong precond") logger.debug(`Searching for file ${filename}`); const fileNames: string[] = req.params.path.split('/'); const project: Project | null = res.locals.project; @@ -709,18 +721,15 @@ export class FileRouter { req: Request, res: Response, next: NextFunction, fileId: string ): Promise => { - logger.debug(`Searching for file ${fileId}`); + console.log(`Searching for file ${fileId}`); const file: File | null = await File.findOne({ include: [{all: true}], where: { uuid: fileId } }); - if (file == null) { - return next(new RequestError(404, 'file_not_found')); - } else { - res.locals.file = file; - } + res.locals.file = file; + console.log("precondition: " + file) next(); } ); -- cgit v1.2.1 From 71a674b70f0003e215f6d9ba5e5b94db36a83b36 Mon Sep 17 00:00:00 2001 From: Hafeez Date: Sat, 14 Apr 2018 19:47:18 +0100 Subject: think i fixed something --- src/FileRouter.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/FileRouter.ts') diff --git a/src/FileRouter.ts b/src/FileRouter.ts index 4ee42dc..ff0e65d 100644 --- a/src/FileRouter.ts +++ b/src/FileRouter.ts @@ -425,7 +425,7 @@ const getProjects: Middleware = async( }); res.locals.data = projects.map( (p: Project): ProjectFullInfo => p.fullInfo - ); + ); next(); }; -- cgit v1.2.1 From 1e6cb08fcdec54f1263eb1f6fdae6e0f438dfc97 Mon Sep 17 00:00:00 2001 From: jw255 Date: Sun, 15 Apr 2018 17:24:10 +0100 Subject: fixed logging --- src/FileRouter.ts | 34 ++++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-) (limited to 'src/FileRouter.ts') diff --git a/src/FileRouter.ts b/src/FileRouter.ts index ff0e65d..4e32fb3 100644 --- a/src/FileRouter.ts +++ b/src/FileRouter.ts @@ -65,6 +65,7 @@ const getProtocols: Middleware = ( const postLog: Middleware = ( req: Request, res: Response, next: NextFunction ): void => { + res.locals.modified = true; logger.debug('Adding forwarded log'); if (!req.user.hasPrivilege('logging')) { next(new RequestError(401, 'invalid_privilege')); @@ -108,9 +109,15 @@ const getLog: Middleware = ( req: Request, res: Response, next: NextFunction ): void => { logger.debug('Fetching log'); + res.locals.modified = true; if (!req.user.hasPrivilege('admin')) { next(new RequestError(401, 'invalid_privilege')); } + /* tslint:disable */ + if(!req.params.level) req.params.level = 'info'; + if(!req.params.before) req.params.before = new Date(); + if(!req.params.after) req.params.after = new Date(0); + /* tslint:enable */ res.locals.data = logger.fetch( req.params.level, { before: new Date(req.params.before), @@ -165,6 +172,7 @@ const postProperties: Middleware = ( req: Request, res: Response, next: NextFunction ): void => { logger.debug('Editing server properties'); + res.locals.modified = true; for (const newProp of req.body) { const prop: Property | undefined = serverConfig.find( (p: Property): boolean => p.id === newProp.id @@ -257,6 +265,7 @@ const getUserProperties: Middleware = ( req: Request, res: Response, next: NextFunction ): void => { logger.debug('Listing additional properties for a user'); + res.locals.modified = true; if (res.locals.user == null) { next(new RequestError(404, 'user_not_found')); return; @@ -289,6 +298,7 @@ const getUsername: Middleware = ( req: Request, res: Response, next: NextFunction ): void => { logger.debug(`Listing user ${res.locals.user}`); + res.locals.modified = true; if (res.locals.user == null) { next(new RequestError(404, 'user_not_found')); return; @@ -327,6 +337,7 @@ const postCurUser: Middleware = async( req: Request, res: Response, next: NextFunction ): Promise => { logger.debug('Editing current user'); + res.locals.modified = true; try { await req.user.updateInfo(req.body); await req.user.save(); @@ -357,14 +368,15 @@ const postUsername: Middleware = async( req: Request, res: Response, next: NextFunction ): Promise => { let user: User | null | undefined = res.locals.user; + res.locals.modified = true; if (user == null) { - console.log('Adding new user'); + logger.debug('Adding new user'); user = new User({ username: req.params.username, password: req.params.password }); } else { - console.log('Editing user'); + logger.debug('Editing user'); user.password = req.params.password; } if (req.body) { @@ -387,6 +399,7 @@ const postUsername: Middleware = async( const getProjectRoles: Middleware = async( req: Request, res: Response, next: NextFunction ): Promise => { + res.locals.modified = true; logger.debug('Listing available project roles'); const roles: ContributorGroup[] = await ContributorGroup.findAll(); res.locals.data = roles.filter( @@ -418,6 +431,7 @@ const getProjects: Middleware = async( req: Request, res: Response, next: NextFunction ): Promise => { logger.debug('Listing all projects'); + res.locals.modified = true; const projects: Project[] = await Project.findAll({ include: [ User @@ -425,7 +439,7 @@ const getProjects: Middleware = async( }); res.locals.data = projects.map( (p: Project): ProjectFullInfo => p.fullInfo - ); + ); next(); }; @@ -450,6 +464,7 @@ const getProjectName: Middleware = async( req: Request, res: Response, next: NextFunction ): Promise => { logger.debug('Getting project properties'); + res.locals.modified = true; const project: Project | null = res.locals.project; if (project == null) { return next(new RequestError(404, 'project_not_found')); @@ -476,6 +491,7 @@ const postProjectName: Middleware = async( req: Request, res: Response, next: NextFunction ): Promise => { let project: Project | null = res.locals.project; + res.locals.modified = true; let promise: PromiseLike | null = null; if (project == null) { logger.debug('Adding new project'); @@ -505,6 +521,7 @@ const getProjectProperties: Middleware = ( req: Request, res: Response, next: NextFunction ): void => { logger.debug('Getting project properties'); + res.locals.modified = true; if (res.locals.project == null) { next(new RequestError(404, 'project_not_found')); } else { @@ -520,6 +537,7 @@ const getFile: Middleware = async( req: Request, res: Response, next: NextFunction ): Promise => { logger.debug(`Getting file in ${req.query.view} view`); + res.locals.modified = true; const file: File | null = res.locals.file; if (file == null) { return next(new RequestError(404, 'file_not_found')); @@ -547,7 +565,8 @@ const getFileId: Middleware = getFile; const postFilePath: Middleware = ( req: Request, res: Response, next: NextFunction ): void => { - logger.debug('Receiving file to ' + res.locals.parentFolder); + logger.debug(`Receiving file to ${res.locals.parentFolder}`); + res.locals.modified = true; if (res.locals.file != null) { next(new RequestError(400, 'file_exists')); } @@ -611,7 +630,7 @@ export class FileRouter { this.router.get ('/projects/:project_name/properties', getProjectProperties); // File access this.router.get ('/projects/:project_name/files/:path', getFilePath); - this.router.post('/projects/:project_name/files/:id', postFilePath); + this.router.post('/projects/:project_name/files/:path', postFilePath); this.router.get ('/projects/:project_name/files_by_id/:id', getFileId); } @@ -668,7 +687,7 @@ export class FileRouter { req: Request, res: Response, next: NextFunction, filename: string ): Promise => { - console.log("wrong precond") + console.log(`parsing path ${filename}`) logger.debug(`Searching for file ${filename}`); const fileNames: string[] = req.params.path.split('/'); const project: Project | null = res.locals.project; @@ -721,7 +740,7 @@ export class FileRouter { req: Request, res: Response, next: NextFunction, fileId: string ): Promise => { - console.log(`Searching for file ${fileId}`); + console.log(`parsing id ${fileId}`); const file: File | null = await File.findOne({ include: [{all: true}], where: { @@ -729,7 +748,6 @@ export class FileRouter { } }); res.locals.file = file; - console.log("precondition: " + file) next(); } ); -- cgit v1.2.1 From 848f02bcacc11578314ab2e75ccb5b65dba0a6bc Mon Sep 17 00:00:00 2001 From: Hafeez Date: Sun, 15 Apr 2018 18:22:05 +0100 Subject: started modifying file upload --- src/FileRouter.ts | 1 + 1 file changed, 1 insertion(+) (limited to 'src/FileRouter.ts') diff --git a/src/FileRouter.ts b/src/FileRouter.ts index ff0e65d..0a8d06a 100644 --- a/src/FileRouter.ts +++ b/src/FileRouter.ts @@ -556,6 +556,7 @@ const postFilePath: Middleware = ( name: res.locals.filename, parentFolder: res.locals.parentFolder }); + console.log(res.locals); /* console.log("1: " + file.uuid + " -- " + res.locals.project.name) try { -- cgit v1.2.1 From acd2cd460ef781ba5ae37d579ac984ca4ee1948c Mon Sep 17 00:00:00 2001 From: Hafeez Date: Sun, 15 Apr 2018 19:44:38 +0100 Subject: file upload works, i thnk --- src/FileRouter.ts | 22 ++++++---------------- 1 file changed, 6 insertions(+), 16 deletions(-) (limited to 'src/FileRouter.ts') diff --git a/src/FileRouter.ts b/src/FileRouter.ts index d6cea53..9a7dcac 100644 --- a/src/FileRouter.ts +++ b/src/FileRouter.ts @@ -575,19 +575,8 @@ const postFilePath: Middleware = ( name: res.locals.filename, parentFolder: res.locals.parentFolder }); - console.log(res.locals); - /* - console.log("1: " + file.uuid + " -- " + res.locals.project.name) - try { - let x = files.writableStream(file.uuid, res.locals.project.name); - console.log("2") - req.pipe(x); - console.log("3") - } catch (err) { - console.log(err); - } - console.log("4") - */ + //console.log(res.locals); + req.pipe(files.writableStream(file.uuid, res.locals.project.name)); next(); }; @@ -714,13 +703,14 @@ export class FileRouter { } const parentFolder: File | null = await File.findOne({ include: [ - { model: File, as: 'containedFiles' } + { model: File, as: 'containedFilesInternal'}, ], where: { - parentFolder: file, - name: fileNames[0] + //parentFolder: file, + //name: fileNames[0] } }); + //console.log(parentFolder); if (parentFolder == null) { return next(); } -- cgit v1.2.1 From a41380c2ab3b06dd78f61f723f83b84a386b83a8 Mon Sep 17 00:00:00 2001 From: Hafeez Date: Mon, 16 Apr 2018 16:07:21 +0100 Subject: precondition files works --- src/FileRouter.ts | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'src/FileRouter.ts') diff --git a/src/FileRouter.ts b/src/FileRouter.ts index 9a7dcac..f941dd6 100644 --- a/src/FileRouter.ts +++ b/src/FileRouter.ts @@ -575,7 +575,7 @@ const postFilePath: Middleware = ( name: res.locals.filename, parentFolder: res.locals.parentFolder }); - //console.log(res.locals); + //console.log(res.locals); req.pipe(files.writableStream(file.uuid, res.locals.project.name)); next(); }; @@ -701,13 +701,16 @@ export class FileRouter { } fileNames.splice(0, 1); } + if (file == null) { + return next(); + } const parentFolder: File | null = await File.findOne({ include: [ { model: File, as: 'containedFilesInternal'}, ], where: { - //parentFolder: file, - //name: fileNames[0] + parentFolderId: file.id, + name: fileNames[0] } }); //console.log(parentFolder); -- cgit v1.2.1 From d628ed540c883764eff623c7a059bcb588c017bc Mon Sep 17 00:00:00 2001 From: jw255 Date: Mon, 16 Apr 2018 16:08:08 +0100 Subject: nothing major --- src/FileRouter.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'src/FileRouter.ts') diff --git a/src/FileRouter.ts b/src/FileRouter.ts index 9a7dcac..f7a1809 100644 --- a/src/FileRouter.ts +++ b/src/FileRouter.ts @@ -689,6 +689,7 @@ export class FileRouter { // TODO Rewrite using sub-queries instead of repeated queries. while (fileNames.length > 2) { const fileOrNull: File | null = await File.findOne({ + include: [{all: true}], where: { name: fileNames[0], parentFolder: file @@ -703,7 +704,7 @@ export class FileRouter { } const parentFolder: File | null = await File.findOne({ include: [ - { model: File, as: 'containedFilesInternal'}, + { model: File, as: 'containedFilesInternal'} ], where: { //parentFolder: file, -- cgit v1.2.1 From 4ec93e886fc10de2c65e9599dbfca2a70db578e3 Mon Sep 17 00:00:00 2001 From: jw255 Date: Mon, 16 Apr 2018 19:34:57 +0100 Subject: World War Files, part 1\nRoot folder is automatically created at the start of the server. also project folder should now appear in the filesystem --- src/FileRouter.ts | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) (limited to 'src/FileRouter.ts') diff --git a/src/FileRouter.ts b/src/FileRouter.ts index 70243c3..ca2ed50 100644 --- a/src/FileRouter.ts +++ b/src/FileRouter.ts @@ -8,7 +8,7 @@ import { default as UserGroup } from './db/model/UserGroup'; import { NextFunction, Request, Response, Router } from 'express'; import { RequestError } from './errors/errorware'; -import { View, files, views } from './files'; +import { View, files, views, rootPathId, createProjectFolder } from './files'; import { logger } from './logger'; import { Property, default as serverConfig } from './serverConfig'; import { uuid } from './uuid'; @@ -497,9 +497,12 @@ const postProjectName: Middleware = async( logger.debug('Adding new project'); const file: File = new File({ uuid: uuid.generate(), - mimetype: 'inode/directory' + mimetype: 'inode/directory', + name: req.params.project_name, + parentFolderId: rootPathId }); promise = file.save(); + createProjectFolder(req.params.project_name); project = new Project({ name: req.params.project_name, rootFolder: file @@ -575,7 +578,6 @@ const postFilePath: Middleware = ( name: res.locals.filename, parentFolder: res.locals.parentFolder }); - //console.log(res.locals); req.pipe(files.writableStream(file.uuid, res.locals.project.name)); next(); }; @@ -677,11 +679,12 @@ export class FileRouter { req: Request, res: Response, next: NextFunction, filename: string ): Promise => { - console.log(`parsing path ${filename}`) + filename = filename.replace(/\+/g, "/"); // Replace + with / logger.debug(`Searching for file ${filename}`); const fileNames: string[] = req.params.path.split('/'); const project: Project | null = res.locals.project; if (project == null) { + // Project should have been found by previous precondition logger.debug('Project was not found: skipping finding file'); return next(new RequestError(404, 'project_not_found')); } @@ -696,15 +699,21 @@ export class FileRouter { } }); if (fileOrNull == null) { + console.log("fileOrNull was null") return next(); } else { + console.log("^^") + console.log(file) file = fileOrNull; } fileNames.splice(0, 1); } + if (file == null) { + console.log("file was null") return next(); } + console.log(file) const parentFolder: File | null = await File.findOne({ include: [ { model: File, as: 'containedFilesInternal'} @@ -714,7 +723,8 @@ export class FileRouter { name: fileNames[0] } }); - //console.log(parentFolder); + console.log("parent folder: ") + console.log(parentFolder); if (parentFolder == null) { return next(); } -- cgit v1.2.1 From c7874c8d2407fc7d046b6ba6856ebfdb46a30f37 Mon Sep 17 00:00:00 2001 From: jw255 Date: Mon, 16 Apr 2018 20:34:29 +0100 Subject: World War Files - ideas all there, but SQLize part still missing. --- src/FileRouter.ts | 52 ++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 46 insertions(+), 6 deletions(-) (limited to 'src/FileRouter.ts') diff --git a/src/FileRouter.ts b/src/FileRouter.ts index ca2ed50..df7ca94 100644 --- a/src/FileRouter.ts +++ b/src/FileRouter.ts @@ -8,7 +8,7 @@ import { default as UserGroup } from './db/model/UserGroup'; import { NextFunction, Request, Response, Router } from 'express'; import { RequestError } from './errors/errorware'; -import { View, files, views, rootPathId, createProjectFolder } from './files'; +import { addSubFileToFolder, View, files, views, rootPathId, createProjectFolder } from './files'; import { logger } from './logger'; import { Property, default as serverConfig } from './serverConfig'; import { uuid } from './uuid'; @@ -565,22 +565,56 @@ const getFileId: Middleware = getFile; /** * Post a File. / delete / move / etc */ -const postFilePath: Middleware = ( +/* tslint:disable */ +const postFilePath: Middleware = async( req: Request, res: Response, next: NextFunction -): void => { +): Promise => { logger.debug(`Receiving file to ${res.locals.parentFolder}`); - res.locals.modified = true; + if (res.locals.file != null) { next(new RequestError(400, 'file_exists')); } + + // lets have a res.locals.deepestFolderName (and Id) which is a string of the deepest parent folder found + let remainingPath: string = res.locals.path.split(res.locals.deepestFolderName)[1]; + console.log(`remainingPath: ${remainingPath}`) + + while((remainingPath.match(/\//g) || []).length > 1){ + // If there are more subfolders that dont exist, then create them + const subFolder: string = remainingPath.split("/")[1] + logger.debug(`Adding subfolder \'${subFolder}`); + const dir: File = new File({ + uuid: uuid.generate(), + mimetype: 'inode/directory', + name: subFolder, + parentFolderId: res.locals.deepestFolderId + }); + await dir.save(); + + // update parent directory of new directory + addSubFileToFolder(res.locals.deepestFolderId, dir.id); + + // update vars + remainingPath = remainingPath.split(subFolder)[1] + res.locals.deepestFolderName = subFolder; + res.locals.deepestFolderId = dir.id; + console.log(`\tremainingPath: ${remainingPath}`) + console.log(`\tdeepestFolderName: ${res.locals.deepestFolderName}`) + console.log(`\tdeepestFolderId: ${res.locals.deepestFolderId}`) + } + const file: File = new File({ uuid: uuid.generate(), name: res.locals.filename, - parentFolder: res.locals.parentFolder + parentFolder: res.locals.deepestFolderId }); + // update parent directory of new directory + addSubFileToFolder(res.locals.deepestFolderId, file.id); + // pipe the data req.pipe(files.writableStream(file.uuid, res.locals.project.name)); next(); }; +/* tslint:enable */ export class FileRouter { public router: Router; @@ -679,8 +713,14 @@ export class FileRouter { req: Request, res: Response, next: NextFunction, filename: string ): Promise => { - filename = filename.replace(/\+/g, "/"); // Replace + with / + // TODO \/ + // We need to find the res.locals.deepestFolderName (and Id) for the currently requested path + + // Replace + with / + filename = filename.replace(/\+/g, '/'); logger.debug(`Searching for file ${filename}`); + res.locals.path = filename; + const fileNames: string[] = req.params.path.split('/'); const project: Project | null = res.locals.project; if (project == null) { -- cgit v1.2.1 From 7905240ed1655f9db3f571976034e287d88e1315 Mon Sep 17 00:00:00 2001 From: jw255 Date: Tue, 17 Apr 2018 10:38:32 +0100 Subject: World War Files - changed precondition + filled addSubFileToFolder() function. Need to test tho --- src/FileRouter.ts | 79 +++++++++++++++++++++++++------------------------------ 1 file changed, 36 insertions(+), 43 deletions(-) (limited to 'src/FileRouter.ts') diff --git a/src/FileRouter.ts b/src/FileRouter.ts index df7ca94..4b56b19 100644 --- a/src/FileRouter.ts +++ b/src/FileRouter.ts @@ -13,9 +13,6 @@ import { logger } from './logger'; import { Property, default as serverConfig } from './serverConfig'; import { uuid } from './uuid'; -// TODO: figure this out -const BASE_FILE_STORAGE: string = 'TODO: figure this out'; - /** * The type of an Express middleware callback. */ @@ -540,7 +537,6 @@ const getFile: Middleware = async( req: Request, res: Response, next: NextFunction ): Promise => { logger.debug(`Getting file in ${req.query.view} view`); - res.locals.modified = true; const file: File | null = res.locals.file; if (file == null) { return next(new RequestError(404, 'file_not_found')); @@ -566,9 +562,7 @@ const getFileId: Middleware = getFile; * Post a File. / delete / move / etc */ /* tslint:disable */ -const postFilePath: Middleware = async( - req: Request, res: Response, next: NextFunction -): Promise => { +const postFilePath: Middleware = async(req: Request, res: Response, next: NextFunction): Promise => { logger.debug(`Receiving file to ${res.locals.parentFolder}`); if (res.locals.file != null) { @@ -579,6 +573,7 @@ const postFilePath: Middleware = async( let remainingPath: string = res.locals.path.split(res.locals.deepestFolderName)[1]; console.log(`remainingPath: ${remainingPath}`) + // Loop the part of the path that doesn't exist yet and create all the folders while((remainingPath.match(/\//g) || []).length > 1){ // If there are more subfolders that dont exist, then create them const subFolder: string = remainingPath.split("/")[1] @@ -610,6 +605,8 @@ const postFilePath: Middleware = async( }); // update parent directory of new directory addSubFileToFolder(res.locals.deepestFolderId, file.id); + + // TODO respond to correct query params (like update metadata, move, etc) // pipe the data req.pipe(files.writableStream(file.uuid, res.locals.project.name)); next(); @@ -721,59 +718,49 @@ export class FileRouter { logger.debug(`Searching for file ${filename}`); res.locals.path = filename; + // split path into parts const fileNames: string[] = req.params.path.split('/'); + res.locals.filename = fileNames[-1]; const project: Project | null = res.locals.project; if (project == null) { // Project should have been found by previous precondition logger.debug('Project was not found: skipping finding file'); 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) { + console.debug(fileNames); + + let curDir: File | null = project.rootFolder; + res.locals.deepestFolderId = curDir.id; + res.locals.deepestFolderName = curDir.name; + let i: number = 0; + + // Loop through the path and find the deepest folder that exists. Also find the file if possible. + while (curDir && i < fileNames.length) { const fileOrNull: File | null = await File.findOne({ include: [{all: true}], where: { - name: fileNames[0], - parentFolder: file + name: fileNames[i], + parentFolder: curDir } }); if (fileOrNull == null) { - console.log("fileOrNull was null") + console.log("next subdir not found in DB"); return next(); - } else { - console.log("^^") - console.log(file) - file = fileOrNull; + } + + if(i < fileNames.length-1){ + // Found next subdirectory + console.log(`found subdir ${fileOrNull.name}`); + res.locals.deepestFolderId = fileOrNull.id; + res.locals.deepestFolderName = fileOrNull.name; + }else{ + // Found actual file + console.log(`found file ${fileOrNull.name}`); + res.locals.file = fileOrNull; } - fileNames.splice(0, 1); + curDir = fileOrNull; } - if (file == null) { - console.log("file was null") - return next(); - } - console.log(file) - const parentFolder: File | null = await File.findOne({ - include: [ - { model: File, as: 'containedFilesInternal'} - ], - where: { - parentFolderId: file.id, - name: fileNames[0] - } - }); - console.log("parent folder: ") - console.log(parentFolder); - 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(); } ); @@ -793,6 +780,12 @@ export class FileRouter { } }); res.locals.file = file; + if(file) { + res.locals.filename = file.name; + res.locals.deepestFolderId = file.parentFolderId; + if(file.parentFolder) res.locals.deepestFolderName = file.parentFolder.name; + else console.log("parentfolder not set!") + } next(); } ); -- cgit v1.2.1 From 8f1837af9400ef0ef5c1cab1e853b46e11b1b76b Mon Sep 17 00:00:00 2001 From: jw255 Date: Tue, 17 Apr 2018 18:43:06 +0100 Subject: Files - now actually works --- src/FileRouter.ts | 108 ++++++++++++++++++++++++++++++++---------------------- 1 file changed, 64 insertions(+), 44 deletions(-) (limited to 'src/FileRouter.ts') diff --git a/src/FileRouter.ts b/src/FileRouter.ts index 4b56b19..ed6567c 100644 --- a/src/FileRouter.ts +++ b/src/FileRouter.ts @@ -495,18 +495,21 @@ const postProjectName: Middleware = async( const file: File = new File({ uuid: uuid.generate(), mimetype: 'inode/directory', - name: req.params.project_name, parentFolderId: rootPathId }); + file.name = ''; // Leave blank as discussed promise = file.save(); createProjectFolder(req.params.project_name); + project = new Project({ name: req.params.project_name, rootFolder: file }); + project.rootFolderId = file.uuid; } else { logger.debug('Updating project properties'); } + project.metadata = req.body; // tslint:disable-next-line:await-promise await promise; @@ -563,48 +566,57 @@ const getFileId: Middleware = getFile; */ /* tslint:disable */ const postFilePath: Middleware = async(req: Request, res: Response, next: NextFunction): Promise => { - logger.debug(`Receiving file to ${res.locals.parentFolder}`); + logger.debug(`Receiving file ${res.locals.path}`); if (res.locals.file != null) { + // TODO update metadata or append to file next(new RequestError(400, 'file_exists')); } // lets have a res.locals.deepestFolderName (and Id) which is a string of the deepest parent folder found - let remainingPath: string = res.locals.path.split(res.locals.deepestFolderName)[1]; - console.log(`remainingPath: ${remainingPath}`) + let remainingPath: string = (res.locals.deepestFolderName == '') ? '/' + res.locals.path + : res.locals.path.split(res.locals.deepestFolderName)[1]; // Loop the part of the path that doesn't exist yet and create all the folders while((remainingPath.match(/\//g) || []).length > 1){ // If there are more subfolders that dont exist, then create them const subFolder: string = remainingPath.split("/")[1] - logger.debug(`Adding subfolder \'${subFolder}`); + logger.debug(`Adding subfolder \'${subFolder}\'`); const dir: File = new File({ uuid: uuid.generate(), mimetype: 'inode/directory', - name: subFolder, - parentFolderId: res.locals.deepestFolderId + nameInternal: subFolder//, + //parentFolderId: res.locals.deepestFolderId }); await dir.save(); // update parent directory of new directory - addSubFileToFolder(res.locals.deepestFolderId, dir.id); - + if(!await addSubFileToFolder(res.locals.deepestFolderId, dir.uuid)){ + logger.debug("adding file to folder failed!") + next(new RequestError(500, 'Adding file to folder failed')) + } // update vars remainingPath = remainingPath.split(subFolder)[1] res.locals.deepestFolderName = subFolder; - res.locals.deepestFolderId = dir.id; - console.log(`\tremainingPath: ${remainingPath}`) - console.log(`\tdeepestFolderName: ${res.locals.deepestFolderName}`) - console.log(`\tdeepestFolderId: ${res.locals.deepestFolderId}`) + res.locals.deepestFolderId = dir.uuid; } + logger.debug(`now adding the actual file \'${res.locals.filename}\' to folder \'${res.locals.deepestFolderName}\'`) + const file: File = new File({ uuid: uuid.generate(), - name: res.locals.filename, - parentFolder: res.locals.deepestFolderId + nameInternal: res.locals.filename//, + //parentFolder: res.locals.deepestFolderId }); + file.mimetype = req.headers['content-type']!; + + await file.save(); + // update parent directory of new directory - addSubFileToFolder(res.locals.deepestFolderId, file.id); + if(!await addSubFileToFolder(res.locals.deepestFolderId, file.uuid)){ + logger.debug("adding file to folder failed!") + next(new RequestError(500, 'Adding file to folder failed')) + } // TODO respond to correct query params (like update metadata, move, etc) // pipe the data @@ -710,55 +722,60 @@ export class FileRouter { req: Request, res: Response, next: NextFunction, filename: string ): Promise => { - // TODO \/ - // We need to find the res.locals.deepestFolderName (and Id) for the currently requested path + // Vars we assign here: + // - res.locals.deepestFolderName (...Id): deepest existing folder in path + // - res.locals.path: requested path (using "/") + // - res.locals.filename: requested file name (may be '') + // - res.locals.file: actual file object if it exists + // Vars arleady assigned: + // - res.locals.project: requested project object // Replace + with / - filename = filename.replace(/\+/g, '/'); - logger.debug(`Searching for file ${filename}`); - res.locals.path = filename; - - // split path into parts - const fileNames: string[] = req.params.path.split('/'); - res.locals.filename = fileNames[-1]; + res.locals.path = filename.replace(/\+/g, '/'); + logger.debug(`Searching for path \'${res.locals.path}\'`); + // Split path into parts + const fileNames: string[] = res.locals.path.split('/'); + res.locals.filename = (fileNames.length > 0) ? + fileNames[fileNames.length - 1] : ''; + logger.debug(`Searching for file \'${res.locals.filename}\'`); const project: Project | null = res.locals.project; if (project == null) { // Project should have been found by previous precondition logger.debug('Project was not found: skipping finding file'); return next(new RequestError(404, 'project_not_found')); } - console.debug(fileNames); - let curDir: File | null = project.rootFolder; - res.locals.deepestFolderId = curDir.id; + res.locals.deepestFolderId = curDir.uuid; res.locals.deepestFolderName = curDir.name; let i: number = 0; - - // Loop through the path and find the deepest folder that exists. Also find the file if possible. + // Loop through the path and find the deepest folder that exists. + // Also find the file if possible. while (curDir && i < fileNames.length) { + logger.debug(`Searching for \'${fileNames[i]}\'`); const fileOrNull: File | null = await File.findOne({ include: [{all: true}], where: { - name: fileNames[i], - parentFolder: curDir + nameInternal: fileNames[i], + parentFolderId: curDir.uuid } }); if (fileOrNull == null) { - console.log("next subdir not found in DB"); + logger.debug(`Sub-file \'${fileNames[i]}\' not found in DB`); return next(); - } - - if(i < fileNames.length-1){ + } + + if (i < fileNames.length - 1) { // Found next subdirectory - console.log(`found subdir ${fileOrNull.name}`); - res.locals.deepestFolderId = fileOrNull.id; - res.locals.deepestFolderName = fileOrNull.name; - }else{ + logger.debug(`Found Sub-folder \'${fileOrNull.name}\'`); + res.locals.deepestFolderId = fileOrNull.uuid; + res.locals.deepestFolderName = fileOrNull.name; + } else { // Found actual file - console.log(`found file ${fileOrNull.name}`); + logger.debug(`Found file \'${fileOrNull.name}\'`); res.locals.file = fileOrNull; } curDir = fileOrNull; + i++; } next(); @@ -772,7 +789,7 @@ export class FileRouter { req: Request, res: Response, next: NextFunction, fileId: string ): Promise => { - console.log(`parsing id ${fileId}`); + logger.info(`parsing id ${fileId}`); const file: File | null = await File.findOne({ include: [{all: true}], where: { @@ -783,8 +800,11 @@ export class FileRouter { if(file) { res.locals.filename = file.name; res.locals.deepestFolderId = file.parentFolderId; - if(file.parentFolder) res.locals.deepestFolderName = file.parentFolder.name; - else console.log("parentfolder not set!") + if (file.parentFolder) { + res.locals.deepestFolderName = file.parentFolder.name; + } else { + logger.debug('parentfolder not set!') + } } next(); } -- cgit v1.2.1 From 505ae2d6af5ce31360f0f0b018a75cfee59cf3ac Mon Sep 17 00:00:00 2001 From: jw255 Date: Tue, 17 Apr 2018 22:58:03 +0100 Subject: using multer for files now --- src/FileRouter.ts | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) (limited to 'src/FileRouter.ts') diff --git a/src/FileRouter.ts b/src/FileRouter.ts index ed6567c..8dc8468 100644 --- a/src/FileRouter.ts +++ b/src/FileRouter.ts @@ -6,13 +6,13 @@ import { UserFullInfo, default as User } from './db/model/User'; import { default as UserGroup } from './db/model/UserGroup'; import { NextFunction, Request, Response, Router } from 'express'; - import { RequestError } from './errors/errorware'; -import { addSubFileToFolder, View, files, views, rootPathId, createProjectFolder } from './files'; +import { addSubFileToFolder, View, files, views, rootPathId, createProjectFolder, upload, moveFile } from './files'; import { logger } from './logger'; import { Property, default as serverConfig } from './serverConfig'; import { uuid } from './uuid'; + /** * The type of an Express middleware callback. */ @@ -567,6 +567,8 @@ const getFileId: Middleware = getFile; /* tslint:disable */ const postFilePath: Middleware = async(req: Request, res: Response, next: NextFunction): Promise => { logger.debug(`Receiving file ${res.locals.path}`); + console.log(req.file); + // req.query.action if (res.locals.file != null) { // TODO update metadata or append to file @@ -605,10 +607,9 @@ const postFilePath: Middleware = async(req: Request, res: Response, next: NextFu const file: File = new File({ uuid: uuid.generate(), - nameInternal: res.locals.filename//, - //parentFolder: res.locals.deepestFolderId + nameInternal: res.locals.filename }); - file.mimetype = req.headers['content-type']!; + file.mimetype = req.file.mimetype; await file.save(); @@ -618,9 +619,8 @@ const postFilePath: Middleware = async(req: Request, res: Response, next: NextFu next(new RequestError(500, 'Adding file to folder failed')) } - // TODO respond to correct query params (like update metadata, move, etc) - // pipe the data - req.pipe(files.writableStream(file.uuid, res.locals.project.name)); + // Move temp file to proper location + moveFile(req.file.originalname, res.locals.project.name, file.uuid); next(); }; /* tslint:enable */ @@ -665,7 +665,8 @@ export class FileRouter { this.router.get ('/projects/:project_name/properties', getProjectProperties); // File access this.router.get ('/projects/:project_name/files/:path', getFilePath); - this.router.post('/projects/:project_name/files/:path', postFilePath); + this.router.post('/projects/:project_name/files/:path', + upload.single('userfile'), postFilePath); this.router.get ('/projects/:project_name/files_by_id/:id', getFileId); } @@ -730,6 +731,9 @@ export class FileRouter { // Vars arleady assigned: // - res.locals.project: requested project object + //console.log(`req.file.filename: ${req.file}`) + console.log(req.file) + // Replace + with / res.locals.path = filename.replace(/\+/g, '/'); logger.debug(`Searching for path \'${res.locals.path}\'`); @@ -748,7 +752,7 @@ export class FileRouter { res.locals.deepestFolderId = curDir.uuid; res.locals.deepestFolderName = curDir.name; let i: number = 0; - // Loop through the path and find the deepest folder that exists. + // Loop through the path and find the deepest folder that exists. // Also find the file if possible. while (curDir && i < fileNames.length) { logger.debug(`Searching for \'${fileNames[i]}\'`); @@ -797,13 +801,13 @@ export class FileRouter { } }); res.locals.file = file; - if(file) { + if (file) { res.locals.filename = file.name; res.locals.deepestFolderId = file.parentFolderId; if (file.parentFolder) { res.locals.deepestFolderName = file.parentFolder.name; } else { - logger.debug('parentfolder not set!') + logger.debug('parentfolder not set!'); } } next(); -- cgit v1.2.1 From f37c8433ea3bca786898c46c03b86557fa0b6bb3 Mon Sep 17 00:00:00 2001 From: jw255 Date: Wed, 18 Apr 2018 16:50:30 +0100 Subject: POST file parameters maybe working --- src/FileRouter.ts | 177 ++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 137 insertions(+), 40 deletions(-) (limited to 'src/FileRouter.ts') diff --git a/src/FileRouter.ts b/src/FileRouter.ts index 8dc8468..3796544 100644 --- a/src/FileRouter.ts +++ b/src/FileRouter.ts @@ -7,7 +7,7 @@ import { default as UserGroup } from './db/model/UserGroup'; import { NextFunction, Request, Response, Router } from 'express'; import { RequestError } from './errors/errorware'; -import { addSubFileToFolder, View, files, views, rootPathId, createProjectFolder, upload, moveFile } from './files'; +import { addSubFileToFolder, View, files, views, rootPathId, createProjectFolder, upload, deleteFile, truncateFile, saveFile } from './files'; import { logger } from './logger'; import { Property, default as serverConfig } from './serverConfig'; import { uuid } from './uuid'; @@ -546,7 +546,9 @@ const getFile: Middleware = async( } const project: Project = res.locals.project; const view: View = views[req.query.view || 'meta']; - res.locals.function = view.getResponseFunction(req, res); + // TODO return 400 for unsuported views + // TODO raw view accepts extra query params offset and length + res.locals.function = view.getResponseFunction(req, res); res.locals.data = view.getResponseData(file, project, req.query); next(); }; @@ -561,6 +563,42 @@ const getFilePath: Middleware = getFile; */ const getFileId: Middleware = getFile; +namespace qH { + export const hasAction: ((req: Request) => boolean) = (req: Request): boolean => { + return (req.query.action ? true : false); + } + export const setsMetadata: ((req: Request) => boolean) = (req: Request): boolean => { + return (req.query.action && req.query.action == 'set_metadata' ? true : false); + } + export const mksDir: ((req: Request) => boolean) = (req: Request): boolean => { + return (req.query.action && req.query.action == 'mkdir' ? true : false); + } + export const deletes: ((req: Request) => boolean) = (req: Request): boolean => { + return (req.query.action && req.query.action == 'delete' ? true : false); + } + export const moves: ((req: Request) => boolean) = (req: Request): boolean => { + return (req.query.action && req.query.action == 'move' ? true : false); + } + export const copys: ((req: Request) => boolean) = (req: Request): boolean => { + return (req.query.action && req.query.action == 'copy' ? true : false); + } + export const overwrites: ((req: Request) => boolean) = (req: Request): boolean => { + return (req.query.overwrite ? req.query.overwrite : false); + } + export const offset: ((req: Request) => number) = (req: Request): number => { + return (req.query.offset ? req.query.offset : 0); + } + export const truncates: ((req: Request) => boolean) = (req: Request): boolean => { + return (req.query.truncate ? req.query.truncate : false); + } + export const isFinal: ((req: Request) => boolean) = (req: Request): boolean => { + return (req.query.final ? req.query.final : false); + } + export const nothingSet: ((req: Request) => boolean) = (req: Request): boolean => { + return (!hasAction(req) && !overwrites(req) && !truncates(req) && !isFinal(req)); + } +} + /** * Post a File. / delete / move / etc */ @@ -568,60 +606,119 @@ const getFileId: Middleware = getFile; const postFilePath: Middleware = async(req: Request, res: Response, next: NextFunction): Promise => { logger.debug(`Receiving file ${res.locals.path}`); console.log(req.file); - // req.query.action + // req.query.action: set_metadata, mkdir, delete, move, copy + // req.query.overwrite (bool) + // req.query.offset (int) + // req.query.truncate (bool) + // req.query.final (bool) + + // Return 404 when one of these actions is attempted on non-existing file + if (res.locals.file == null && (qH.setsMetadata(req) || qH.deletes(req) || qH.moves(req) || qH.copys(req))){ + logger.debug(`Action \'${req.query.action}\' not valid if file does not exist`); + next(new RequestError(404, 'file_not_found')); + } - if (res.locals.file != null) { - // TODO update metadata or append to file + // Return 400 when one of these actions is attempted on existing file + if (res.locals.file != null && (qH.nothingSet(req) || qH.mksDir(req))) { + logger.debug(`Action \'${req.query.action}\' not valid if file exists`); next(new RequestError(400, 'file_exists')); } - // lets have a res.locals.deepestFolderName (and Id) which is a string of the deepest parent folder found - let remainingPath: string = (res.locals.deepestFolderName == '') ? '/' + res.locals.path - : res.locals.path.split(res.locals.deepestFolderName)[1]; - - // Loop the part of the path that doesn't exist yet and create all the folders - while((remainingPath.match(/\//g) || []).length > 1){ - // If there are more subfolders that dont exist, then create them - const subFolder: string = remainingPath.split("/")[1] - logger.debug(`Adding subfolder \'${subFolder}\'`); - const dir: File = new File({ + // Sets final if requested + if (res.locals.file && qH.isFinal(req)) { + res.locals.file.status = 'final'; + } + + // Sets Metadata if requested + if(qH.setsMetadata(req)){ + res.locals.file.metadata = req.body; + await res.locals.file.save(); + next(); + } + + // Deletes File if requested + if(qH.deletes(req)){ + deleteFile(res.locals.file.uuid, res.locals.project.name); + await res.locals.file.delete(); + next(); + } + + // Moves File if requested + if(qH.moves(req)){ + // TODO + next(new RequestError(500, 'not_implemented')); + } + + // Copies File if requested + if(qH.copys(req)){ + // TODO + next(new RequestError(500, 'not_implemented')); + } + + // Overwrite data but not truncate + // eg. OOOONNNNNNOOOO (O = old data, N = new data) + if(qH.overwrites(req) && !qH.truncates(req)){ + // TODO + next(new RequestError(500, 'not_implemented', 'please specify truncate parameter')); + } + + // Create File or Folder if file doesn't exist + if (res.locals.file == null){ + // lets have a res.locals.deepestFolderName (and Id) which is a string of the deepest parent folder found + let remainingPath: string = (res.locals.deepestFolderName == '') ? '/' + res.locals.path + : res.locals.path.split(res.locals.deepestFolderName)[1]; + + // Loop the part of the path that doesn't exist yet and create all the folders + while((remainingPath.match(/\//g) || []).length > 1){ + // If there are more subfolders that dont exist, then create them + const subFolder: string = remainingPath.split("/")[1] + logger.debug(`Adding subfolder \'${subFolder}\'`); + const dir: File = new File({ + uuid: uuid.generate(), + mimetype: 'inode/directory', + nameInternal: subFolder + }); + await dir.save(); + + // update parent directory of new directory + if(!await addSubFileToFolder(res.locals.deepestFolderId, dir.uuid)){ + logger.debug("adding file to folder failed!") + next(new RequestError(500, 'Adding file to folder failed')) + } + // update vars + remainingPath = remainingPath.split(subFolder)[1] + res.locals.deepestFolderName = subFolder; + res.locals.deepestFolderId = dir.uuid; + } + + logger.debug(`Writing file \'${res.locals.filename}\' to folder \'${res.locals.deepestFolderName}\'`) + res.locals.file = new File({ uuid: uuid.generate(), - mimetype: 'inode/directory', - nameInternal: subFolder//, - //parentFolderId: res.locals.deepestFolderId + nameInternal: res.locals.filename }); - await dir.save(); + res.locals.file.mimetype = qH.mksDir(req) ? 'inode/directory' : req.file.mimetype; + + await res.locals.file.save(); // update parent directory of new directory - if(!await addSubFileToFolder(res.locals.deepestFolderId, dir.uuid)){ + if(!await addSubFileToFolder(res.locals.deepestFolderId, res.locals.file.uuid)){ logger.debug("adding file to folder failed!") next(new RequestError(500, 'Adding file to folder failed')) } - // update vars - remainingPath = remainingPath.split(subFolder)[1] - res.locals.deepestFolderName = subFolder; - res.locals.deepestFolderId = dir.uuid; } - logger.debug(`now adding the actual file \'${res.locals.filename}\' to folder \'${res.locals.deepestFolderName}\'`) - - const file: File = new File({ - uuid: uuid.generate(), - nameInternal: res.locals.filename - }); - file.mimetype = req.file.mimetype; - - await file.save(); - - // update parent directory of new directory - if(!await addSubFileToFolder(res.locals.deepestFolderId, file.uuid)){ - logger.debug("adding file to folder failed!") - next(new RequestError(500, 'Adding file to folder failed')) + // Move temp file to proper location (if not directory) + if(!qH.mksDir(req)){ + saveFile(req.file.originalname, res.locals.project.name, res.locals.file.uuid, qH.offset(req)); } - // Move temp file to proper location - moveFile(req.file.originalname, res.locals.project.name, file.uuid); + // Sets final if requested + if (qH.isFinal(req)) { + res.locals.file.status = 'final'; + } + next(); + }; /* tslint:enable */ -- cgit v1.2.1 From b9cb14f9368e281ab01d92cc8864cdcb4a0cb386 Mon Sep 17 00:00:00 2001 From: jw255 Date: Thu, 19 Apr 2018 11:08:40 +0100 Subject: minor fixes --- src/FileRouter.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'src/FileRouter.ts') diff --git a/src/FileRouter.ts b/src/FileRouter.ts index 3796544..70d3d51 100644 --- a/src/FileRouter.ts +++ b/src/FileRouter.ts @@ -563,6 +563,9 @@ const getFilePath: Middleware = getFile; */ const getFileId: Middleware = getFile; +/** + * Checks the parameters for file posting + */ namespace qH { export const hasAction: ((req: Request) => boolean) = (req: Request): boolean => { return (req.query.action ? true : false); @@ -638,8 +641,9 @@ const postFilePath: Middleware = async(req: Request, res: Response, next: NextFu // Deletes File if requested if(qH.deletes(req)){ + logger.info(`File ${res.locals.file.name} will be deleted`) deleteFile(res.locals.file.uuid, res.locals.project.name); - await res.locals.file.delete(); + await res.locals.file.destroy(); next(); } -- cgit v1.2.1 From 8af06a4e7ea7ebaa29559a345d28791079b77dd3 Mon Sep 17 00:00:00 2001 From: jw255 Date: Thu, 19 Apr 2018 14:21:38 +0100 Subject: no reset --- src/FileRouter.ts | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) (limited to 'src/FileRouter.ts') diff --git a/src/FileRouter.ts b/src/FileRouter.ts index 70d3d51..0798ce8 100644 --- a/src/FileRouter.ts +++ b/src/FileRouter.ts @@ -548,7 +548,7 @@ const getFile: Middleware = async( const view: View = views[req.query.view || 'meta']; // TODO return 400 for unsuported views // TODO raw view accepts extra query params offset and length - res.locals.function = view.getResponseFunction(req, res); + res.locals.function = view.getResponseFunction(req, res); res.locals.data = view.getResponseData(file, project, req.query); next(); }; @@ -832,9 +832,6 @@ export class FileRouter { // Vars arleady assigned: // - res.locals.project: requested project object - //console.log(`req.file.filename: ${req.file}`) - console.log(req.file) - // Replace + with / res.locals.path = filename.replace(/\+/g, '/'); logger.debug(`Searching for path \'${res.locals.path}\'`); -- cgit v1.2.1