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 +++++++++++++++++++++++++------------------------------ src/files.ts | 40 +++++++++++++++++----------- 2 files changed, 60 insertions(+), 59 deletions(-) (limited to 'src') 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(); } ); diff --git a/src/files.ts b/src/files.ts index 6572c53..a5fd08d 100644 --- a/src/files.ts +++ b/src/files.ts @@ -28,18 +28,6 @@ const readableStream: (filename: string, projectName: string, { offset, length } }; return fs.createReadStream(path(filename, projectName), options); }; -/* -// prj name, file uuid => if exists and return path string/ parent folder -const findParent: (projectName: string, fileUUID: string) => File | null = (projectName: string, fileUUID: string) => { - if(!projectName || !fileUUID){ - return null; - } - function findFile(dir: string, fileList: string[]) : string { - - } - -} -*/ const logPath: (type: string, projectName?: string) => string = ( type: string, projectName?: string): string => { @@ -209,9 +197,29 @@ export const createProjectFolder: (name: string) => void = (projName: string): a } } -export const addSubFileToFolder: (parentId: string, subFileId: string) => void = (parentId: string, subFileId: string): any => { - // TODO: - // fetch parent file object from database - // update parent file object +export const addSubFileToFolder: (parentId: string, subFileId: string) => Promise = async(parentId: string, subFileId: string): Promise => { + // Fetch parent and subFile object from database + const parent: File | null = await File.findOne({ + include: [{all: true}], + where: { + uuid: parentId + } + }); + const file: File | null = await File.findOne({ + include: [{all: true}], + where: { + uuid: subFileId + } + }); + if(!parent || !file){ + return false; + } + // update both objects + parent.containedFiles.push(file); + file.parentFolderId = parent.id; // save + await parent.save(); + await file.save(); + + return true; } -- cgit v1.2.1