aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/FileRouter.ts6
-rw-r--r--src/files.ts48
2 files changed, 46 insertions, 8 deletions
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<object>;
getResponseFunction: (req: Request, res: Response) => Function | null;
+ preprocess: (original: string, space: string) =>
+ Promise<void>;
}
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<void> => {
+ 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<void> => {
+ 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<void> => {
+ return;
}
}
};
@@ -324,6 +345,23 @@ const fileTypes: {
}
};
+export const preprocess: (
+ file: File, project: Project
+) => Promise<void> = async(
+ file: File, project: Project
+): Promise<void> => {
+ const promises: Promise<void>[] = [];
+ 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<string, string> {
public constructor(pairs: [string, FileTypeName][]) {
super(pairs);