aboutsummaryrefslogtreecommitdiff
path: root/src/files.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/files.ts')
-rw-r--r--src/files.ts172
1 files changed, 169 insertions, 3 deletions
diff --git a/src/files.ts b/src/files.ts
index 644d24e..8ad7236 100644
--- a/src/files.ts
+++ b/src/files.ts
@@ -1,5 +1,9 @@
+import { Request, Response } from 'express';
import * as fs from 'fs';
+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';
@@ -8,12 +12,38 @@ const path: (filename: string, projectName: string) => string
return `${CONTENT_BASE_DIRECTORY}/${projectName}/${filename}`;
};
-const writableStream:
- (filename: string, projectName: string) => fs.WriteStream =
- (filename: string, projectName: string): fs.WriteStream => {
+const writableStream: (
+ filename: string, projectName: string
+) => fs.WriteStream = (
+ filename: string, projectName: string
+): fs.WriteStream => {
return fs.createWriteStream(path(filename, projectName));
};
+interface RawSize {
+ offset: number;
+ length: number;
+}
+
+const readableStream: (
+ filename: string, projectName: string,
+ { offset, length }?: Partial<RawSize>
+) => fs.ReadStream = (
+ filename: string, projectName: string,
+ { offset, length }: Partial<RawSize> =
+ { offset: undefined, length: undefined }
+): fs.ReadStream => {
+ const options: object = {
+ start: offset
+ ? offset
+ : 0,
+ end: offset && length
+ ? offset + length
+ : undefined
+ };
+ return fs.createReadStream(path(filename, projectName), options);
+};
+
const logPath:
(type: string, projectName?: string) => string = (
type: string, projectName?: string
@@ -32,3 +62,139 @@ export const files: {
writableStream,
logPath
};
+
+export type ViewName = 'raw' | 'meta';
+
+interface Query extends Partial<RawSize> {
+ children?: boolean;
+}
+
+export interface View {
+ getContents: (file: File) => object;
+ getResponseData: (file: File, project: Project, query: Query) =>
+ Promise<object>;
+ getResponseFunction: (req: Request, res: Response) => Function | null;
+}
+
+interface MetaResponseData {
+ file_path: string;
+ file_name: string;
+ id: string;
+ type: FileTypeName;
+ metadata: object;
+ status: string;
+ supported_views: {
+ [key: string]: object | undefined;
+ };
+ children?: MetaResponseData[];
+}
+
+const metaGetResponseData: (
+ file: File, project: Project, query: Query
+) => Promise<MetaResponseData> = async(
+ file: File, project: Project, query: Query
+): Promise<MetaResponseData> => {
+ const data: MetaResponseData = {
+ file_path: file.fullPath,
+ file_name: file.name,
+ id: file.id,
+ type: file.type,
+ metadata: file.metadata,
+ status: file.status,
+ supported_views: {}
+ };
+ for (const view of fileTypes[file.type].supportedViews) {
+ data.supported_views[view] =
+ views[view].getContents(file);
+ }
+ if (query.children) {
+ data.children = [];
+ const children: File[] | null = await File.findAll({
+ where: {
+ parentFolder: file
+ }
+ });
+ if (children != null) {
+ for (const child of children) {
+ data.children.push(await metaGetResponseData(
+ file,
+ project,
+ {
+ ...query,
+ children: undefined
+ }
+ ));
+ }
+ }
+ }
+ return data;
+};
+
+export const views: {
+ [view: string]: View;
+} = {
+ raw: {
+ getContents: (file: File): object => {
+ return {
+ size: file.size
+ };
+ },
+ getResponseData: async(
+ file: File, project: Project, query: Query
+ ): Promise<object> => {
+ return readableStream(file.name, project.name, query);
+ },
+ getResponseFunction: (req: Request, res: Response): Function | null => {
+ return res.send;
+ }
+ },
+ meta: {
+ getContents: (file: File): object => {
+ return {};
+ },
+ getResponseData: metaGetResponseData,
+ getResponseFunction: (req: Request, res: Response): Function | null => {
+ return null;
+ }
+ }
+};
+
+export type FileTypeName = 'generic' | 'directory';
+
+export interface FileType {
+ supportedViews: ViewName[];
+}
+
+const fileTypes: {
+ [type: string]: FileType;
+} = {
+ generic: {
+ supportedViews: [
+ 'raw', 'meta'
+ ]
+ },
+ directory: {
+ supportedViews: [
+ 'meta'
+ ]
+ }
+};
+
+class MimeTypeMap extends Map<string, string> {
+ public constructor(pairs: [string, FileTypeName][]) {
+ super(pairs);
+ }
+
+ public get(mime: string): FileTypeName {
+ const ret: string | undefined = super.get(mime);
+ if (ret) {
+ return ret as FileTypeName;
+ } else {
+ return 'generic';
+ }
+ }
+}
+
+export const mimes: MimeTypeMap = new MimeTypeMap([
+ [ 'inode/directory', 'directory' ]
+]);