aboutsummaryrefslogtreecommitdiff
path: root/src/files.ts
diff options
context:
space:
mode:
authorTom Harley2018-04-23 00:33:26 +0100
committerTom Harley2018-04-23 00:33:26 +0100
commitb8b24a97f02013bbd5adecad07a9e019777a2a95 (patch)
treeecb9847f03f25ef27fe02a6103d9cd0f05dab07d /src/files.ts
parentMerge branch 'trh/gets' of gitlab.cs.st-andrews.ac.uk:cs3099group-be-4/projec... (diff)
downloadmedicine-b8b24a97f02013bbd5adecad07a9e019777a2a95.tar.gz
medicine-b8b24a97f02013bbd5adecad07a9e019777a2a95.zip

Start introduction of preprocessing

Diffstat (limited to 'src/files.ts')
-rw-r--r--src/files.ts48
1 files changed, 43 insertions, 5 deletions
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);