diff options
| author | jw255 | 2018-04-17 18:43:06 +0100 |
|---|---|---|
| committer | jw255 | 2018-04-17 18:43:06 +0100 |
| commit | 8f1837af9400ef0ef5c1cab1e853b46e11b1b76b (patch) | |
| tree | 20e504d4311d938a7569264341d96e8d1d1a934e | |
| parent | World War Files - changed precondition + filled addSubFileToFolder() function... (diff) | |
| download | medicine-8f1837af9400ef0ef5c1cab1e853b46e11b1b76b.tar.gz medicine-8f1837af9400ef0ef5c1cab1e853b46e11b1b76b.zip | |
Files - now actually works
| -rw-r--r-- | src/FileRouter.ts | 108 | ||||
| -rw-r--r-- | src/db/model/File.ts | 14 | ||||
| -rw-r--r-- | src/files.ts | 7 |
3 files changed, 78 insertions, 51 deletions
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<void> => { - 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<void> => { - // 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<void> => { - 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(); } diff --git a/src/db/model/File.ts b/src/db/model/File.ts index 193a785..8a12431 100644 --- a/src/db/model/File.ts +++ b/src/db/model/File.ts @@ -27,7 +27,7 @@ import { FileTypeName, mimes } from '../../files'; export default class File extends Model<File> { @AllowNull @Column - private nameInternal!: string; + public nameInternal!: string; @AllowNull @Column @@ -85,10 +85,14 @@ export default class File extends Model<File> { public set name(name: string) { this.nameInternal = name; - this.fullPathInternal = this.fullPathInternal.substring( - 0, - this.fullPathInternal.lastIndexOf('/') + 1 - ) + this.name; + if (this.fullPathInternal) { + this.fullPathInternal = this.fullPathInternal.substring( + 0, + this.fullPathInternal.lastIndexOf('/') + 1 + ) + this.name; + } else { + this.fullPathInternal = this.name; + } } public get fullPath(): string { diff --git a/src/files.ts b/src/files.ts index a5fd08d..9be0381 100644 --- a/src/files.ts +++ b/src/files.ts @@ -1,11 +1,13 @@ import { Request, Response } from 'express'; import * as fs from 'fs'; +import { logger } from './logger'; import { default as File } from './db/model/File'; import { default as Project } from './db/model/Project'; const CONTENT_BASE_DIRECTORY: string = './files'; const LOG_BASE_DIRECTORY: string = './logs'; + /* tslint:disable */ const path: (filename: string, projectName: string) => string = (filename: string, projectName: string): string => { return `${CONTENT_BASE_DIRECTORY}/${projectName}/${filename}`; @@ -198,6 +200,7 @@ export const createProjectFolder: (name: string) => void = (projName: string): a } export const addSubFileToFolder: (parentId: string, subFileId: string) => Promise<boolean> = async(parentId: string, subFileId: string): Promise<boolean> => { + logger.debug(`adding file ${subFileId}) to directory ${parentId}`) // Fetch parent and subFile object from database const parent: File | null = await File.findOne({ include: [{all: true}], @@ -216,10 +219,10 @@ export const addSubFileToFolder: (parentId: string, subFileId: string) => Promis } // update both objects parent.containedFiles.push(file); - file.parentFolderId = parent.id; + file.parentFolderId = parent.uuid; // save - await parent.save(); await file.save(); + await parent.save(); return true; } |
