From b8b24a97f02013bbd5adecad07a9e019777a2a95 Mon Sep 17 00:00:00 2001 From: Tom Harley Date: Mon, 23 Apr 2018 00:33:26 +0100 Subject: Start introduction of preprocessing --- src/FileRouter.ts | 6 +++--- src/files.ts | 48 +++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 46 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/FileRouter.ts b/src/FileRouter.ts index 8896e47..bb022d3 100644 --- a/src/FileRouter.ts +++ b/src/FileRouter.ts @@ -9,7 +9,7 @@ import * as bodyParser from 'body-parser'; import { RequestError } from './errors/errorware'; import { NextFunction, Request, Response, Router } from 'express'; import { View, addSubFileToFolder, copyFile, createProjectFolder, deleteFile, - saveFile, views } from './files'; + saveFile, views, preprocess } from './files'; import { appendFileSync } from 'fs'; import { logger } from './logger'; import { mimetype } from './mimetype'; @@ -921,7 +921,7 @@ const postFilePath: Middleware = async( if (req.query.final) { res.locals.file.status = 'preprocessing'; await res.locals.file.save(); - // Start file preprocesssing + preprocess(res.locals.file, res.locals.project); } } else { logger.warn('overwriting without truncating not supported'); @@ -965,7 +965,7 @@ const postFilePath: Middleware = async( // Sets final if requested if (qH.isFinal(req)) { res.locals.file.status = 'preprocessing'; - // TODO Start file preprocessing + preprocess(res.locals.file, res.locals.project); } } await res.locals.file.save(); diff --git a/src/files.ts b/src/files.ts index a0bed00..7890d84 100644 --- a/src/files.ts +++ b/src/files.ts @@ -66,16 +66,21 @@ interface Query extends } const readableStream: ( - filename: string, projectName: string, { offset, length }?: Query + filename: string, + projectName: string, + { offset, length }?: Query, + view?: ViewName ) => fs.ReadStream = ( - filename: string, projectName: string, { offset, length }: Query = - { offset: undefined, length: undefined } + filename: string, + projectName: string, + { offset, length }: Query = { offset: undefined, length: undefined }, + view: ViewName = 'raw' ): fs.ReadStream => { const options: object = { start: offset ? offset : 0, end: offset && length ? offset + length : undefined }; - return fs.createReadStream(path(filename, projectName), options); + return fs.createReadStream(path(filename, projectName, view), options); }; export const logPath: (type: string, projectName?: string) => string = ( @@ -90,6 +95,10 @@ export const path: (file: string, project: string, view?: ViewName) => string = (file: string, project: string, view: ViewName = 'raw'): string => `${CONTENT_BASE_DIRECTORY}/${project}/${file}/${view}`; +export const originalPath: (file: string, project: string) => string = + (file: string, project: string): string => + `${CONTENT_BASE_DIRECTORY}/${project}/${file}/ORIGINAL`; + export const pathNoView: (file: string, project: string) => string = (file: string, project: string): string => `${CONTENT_BASE_DIRECTORY}/${project}/${file}`; @@ -182,6 +191,8 @@ export interface View { getResponseData: (file: File, project: Project, query: Query) => Promise; getResponseFunction: (req: Request, res: Response) => Function | null; + preprocess: (original: string, space: string) => + Promise; } interface MetaResponseData { @@ -260,7 +271,11 @@ export const views: { return readableStream(file.uuid, project.name, query); }, getResponseFunction: (req: Request, res: Response): Function | null => { - return (stream: fs.ReadStream) => stream.pipe(res); + return (rStream: fs.ReadStream): fs.ReadStream => + rStream.pipe(res as any); + }, + preprocess: async(original: string, space: string): Promise => { + return; } }, meta: { @@ -270,6 +285,9 @@ export const views: { getResponseData: metaGetResponseData, getResponseFunction: (req: Request, res: Response): Function | null => { return null; + }, + preprocess: async(original: string, space: string): Promise => { + return; } }, tabular: { @@ -294,6 +312,9 @@ export const views: { }, getResponseFunction: (req: Request, res: Response): Function | null => { return (stream: fs.ReadStream) => stream.pipe(res); + }, + preprocess: async(original: string, space: string): Promise => { + return; } } }; @@ -324,6 +345,23 @@ const fileTypes: { } }; +export const preprocess: ( + file: File, project: Project +) => Promise = async( + file: File, project: Project +): Promise => { + const promises: Promise[] = []; + for (const view of fileTypes[file.type].supportedViews) { + promises.push(views[view].preprocess( + originalPath(file.uuid, project.name), + path(file.uuid, project.name, view) + )); + } + await Promise.all(promises); + file.status = 'ready'; + await file.save(); +}; + class MimeTypeMap extends Map { public constructor(pairs: [string, FileTypeName][]) { super(pairs); -- cgit v1.2.1