From 4ec93e886fc10de2c65e9599dbfca2a70db578e3 Mon Sep 17 00:00:00 2001 From: jw255 Date: Mon, 16 Apr 2018 19:34:57 +0100 Subject: World War Files, part 1\nRoot folder is automatically created at the start of the server. also project folder should now appear in the filesystem --- src/files.ts | 62 +++++++++++++++++++++++++++++++++++------------------------- 1 file changed, 36 insertions(+), 26 deletions(-) (limited to 'src/files.ts') diff --git a/src/files.ts b/src/files.ts index 8ad7236..42e490a 100644 --- a/src/files.ts +++ b/src/files.ts @@ -6,17 +6,12 @@ import { default as Project } from './db/model/Project'; const CONTENT_BASE_DIRECTORY: string = './files'; const LOG_BASE_DIRECTORY: string = './logs'; - -const path: (filename: string, projectName: string) => string - = (filename: string, projectName: string): string => { +/* tslint:disable */ +const path: (filename: string, projectName: string) => string = (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)); }; @@ -25,29 +20,29 @@ interface RawSize { length: number; } -const readableStream: ( - filename: string, projectName: string, - { offset, length }?: Partial -) => fs.ReadStream = ( - filename: string, projectName: string, - { offset, length }: Partial = - { offset: undefined, length: undefined } -): fs.ReadStream => { +const readableStream: (filename: string, projectName: string, { offset, length }?: Partial) => fs.ReadStream + = (filename: string, projectName: string, { offset, length }: Partial = { offset: undefined, length: undefined }): fs.ReadStream => { const options: object = { - start: offset - ? offset - : 0, - end: offset && length - ? offset + length - : undefined + start: offset? offset: 0, + end: offset && length? offset + length: undefined }; return fs.createReadStream(path(filename, projectName), options); }; +/* +// prj name, file uuid => if exists and return path string/ parent folder +const findParent: (projectName: string, fileUUID: string) => File | null = (projectName: string, fileUUID: string) => { + if(!projectName || !fileUUID){ + return null; + } + function findFile(dir: string, fileList: string[]) : string { + + } -const logPath: -(type: string, projectName?: string) => string = ( - type: string, projectName?: string -): string => { +} +*/ + +const logPath: (type: string, projectName?: string) => string = ( + type: string, projectName?: string): string => { return projectName ? `${LOG_BASE_DIRECTORY}/projects/${projectName}/${type}` : `${LOG_BASE_DIRECTORY}/general/${type}`; @@ -198,3 +193,18 @@ class MimeTypeMap extends Map { export const mimes: MimeTypeMap = new MimeTypeMap([ [ 'inode/directory', 'directory' ] ]); + +export const rootPathId: string = '0'; +export const rootPathFile: File | null = null; + +export const createRootFolder: () => void = (): any => { + if (!fs.existsSync(CONTENT_BASE_DIRECTORY)){ + fs.mkdirSync(CONTENT_BASE_DIRECTORY); + } +} + +export const createProjectFolder: (name: string) => void = (projName: string): any => { + if (!fs.existsSync(CONTENT_BASE_DIRECTORY + '/' + projName)){ + fs.mkdirSync(CONTENT_BASE_DIRECTORY + '/' + projName); + } +} -- cgit v1.2.1 From c7874c8d2407fc7d046b6ba6856ebfdb46a30f37 Mon Sep 17 00:00:00 2001 From: jw255 Date: Mon, 16 Apr 2018 20:34:29 +0100 Subject: World War Files - ideas all there, but SQLize part still missing. --- src/files.ts | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'src/files.ts') diff --git a/src/files.ts b/src/files.ts index 42e490a..6572c53 100644 --- a/src/files.ts +++ b/src/files.ts @@ -208,3 +208,10 @@ export const createProjectFolder: (name: string) => void = (projName: string): a fs.mkdirSync(CONTENT_BASE_DIRECTORY + '/' + projName); } } + +export const addSubFileToFolder: (parentId: string, subFileId: string) => void = (parentId: string, subFileId: string): any => { + // TODO: + // fetch parent file object from database + // update parent file object + // save +} -- cgit v1.2.1 From 7905240ed1655f9db3f571976034e287d88e1315 Mon Sep 17 00:00:00 2001 From: jw255 Date: Tue, 17 Apr 2018 10:38:32 +0100 Subject: World War Files - changed precondition + filled addSubFileToFolder() function. Need to test tho --- src/files.ts | 40 ++++++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 16 deletions(-) (limited to 'src/files.ts') diff --git a/src/files.ts b/src/files.ts index 6572c53..a5fd08d 100644 --- a/src/files.ts +++ b/src/files.ts @@ -28,18 +28,6 @@ const readableStream: (filename: string, projectName: string, { offset, length } }; return fs.createReadStream(path(filename, projectName), options); }; -/* -// prj name, file uuid => if exists and return path string/ parent folder -const findParent: (projectName: string, fileUUID: string) => File | null = (projectName: string, fileUUID: string) => { - if(!projectName || !fileUUID){ - return null; - } - function findFile(dir: string, fileList: string[]) : string { - - } - -} -*/ const logPath: (type: string, projectName?: string) => string = ( type: string, projectName?: string): string => { @@ -209,9 +197,29 @@ export const createProjectFolder: (name: string) => void = (projName: string): a } } -export const addSubFileToFolder: (parentId: string, subFileId: string) => void = (parentId: string, subFileId: string): any => { - // TODO: - // fetch parent file object from database - // update parent file object +export const addSubFileToFolder: (parentId: string, subFileId: string) => Promise = async(parentId: string, subFileId: string): Promise => { + // Fetch parent and subFile object from database + const parent: File | null = await File.findOne({ + include: [{all: true}], + where: { + uuid: parentId + } + }); + const file: File | null = await File.findOne({ + include: [{all: true}], + where: { + uuid: subFileId + } + }); + if(!parent || !file){ + return false; + } + // update both objects + parent.containedFiles.push(file); + file.parentFolderId = parent.id; // save + await parent.save(); + await file.save(); + + return true; } -- cgit v1.2.1 From 8f1837af9400ef0ef5c1cab1e853b46e11b1b76b Mon Sep 17 00:00:00 2001 From: jw255 Date: Tue, 17 Apr 2018 18:43:06 +0100 Subject: Files - now actually works --- src/files.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'src/files.ts') 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 = async(parentId: string, subFileId: string): Promise => { + 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; } -- cgit v1.2.1 From 505ae2d6af5ce31360f0f0b018a75cfee59cf3ac Mon Sep 17 00:00:00 2001 From: jw255 Date: Tue, 17 Apr 2018 22:58:03 +0100 Subject: using multer for files now --- src/files.ts | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) (limited to 'src/files.ts') diff --git a/src/files.ts b/src/files.ts index 9be0381..b0e63aa 100644 --- a/src/files.ts +++ b/src/files.ts @@ -1,6 +1,7 @@ import { Request, Response } from 'express'; -import * as fs from 'fs'; +import * as fs from 'fs-extra'; import { logger } from './logger'; +import * as Multer from 'multer'; import { default as File } from './db/model/File'; import { default as Project } from './db/model/Project'; @@ -38,6 +39,23 @@ const logPath: (type: string, projectName?: string) => string = ( : `${LOG_BASE_DIRECTORY}/general/${type}`; }; +let storage = Multer.diskStorage({ + destination: function (req, file, cb) { + cb(null, CONTENT_BASE_DIRECTORY) + }, + filename: function (req, file, cb) { + cb(null, `temp-${file.originalname}`) + } +}); + +export const upload: Multer.Instance = Multer({ storage: storage }); + +export const moveFile: (filename: string, projectname: string, fileId: string) => void = + (filename: string, projectname: string, fileId: string): void => { + + fs.moveSync(`${CONTENT_BASE_DIRECTORY}/temp-${filename}`, `${CONTENT_BASE_DIRECTORY}/${projectname}/${fileId}`); +} + export const files: { path: (f: string, p: string) => string; writableStream: (f: string, p: string) => fs.WriteStream; -- cgit v1.2.1 From f37c8433ea3bca786898c46c03b86557fa0b6bb3 Mon Sep 17 00:00:00 2001 From: jw255 Date: Wed, 18 Apr 2018 16:50:30 +0100 Subject: POST file parameters maybe working --- src/files.ts | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) (limited to 'src/files.ts') diff --git a/src/files.ts b/src/files.ts index b0e63aa..92ed165 100644 --- a/src/files.ts +++ b/src/files.ts @@ -50,10 +50,30 @@ let storage = Multer.diskStorage({ export const upload: Multer.Instance = Multer({ storage: storage }); -export const moveFile: (filename: string, projectname: string, fileId: string) => void = - (filename: string, projectname: string, fileId: string): void => { +export const saveFile: (filename: string, projectname: string, fileId: string, offset: number, truncate?: boolean) => void = + (filename: string, projectname: string, fileId: string, offset: number, truncate?: boolean): void => { + // TODO only overwrite beginning of file and keep rest of data + // ensure file exists + fs.ensureFileSync(`${CONTENT_BASE_DIRECTORY}/${projectname}/${fileId}`); + // truncate to offset + truncateFile(fileId, projectname, offset); + // open streams to append to file + let rStream = fs.createReadStream(`${CONTENT_BASE_DIRECTORY}/temp-${filename}`); + let wStream = fs.createWriteStream(`${CONTENT_BASE_DIRECTORY}/${projectname}/${fileId}`, {'flags':'a'}); + rStream.pipe(wStream); +} + +export const deleteFile: (fileId: string, projectName: string) => void = + (fileId: string, projectName: string): void => { - fs.moveSync(`${CONTENT_BASE_DIRECTORY}/temp-${filename}`, `${CONTENT_BASE_DIRECTORY}/${projectname}/${fileId}`); + fs.removeSync(`${CONTENT_BASE_DIRECTORY}/${projectName}/${fileId}`); +} + +export const truncateFile: (fileId: string, projectName: string, newLength: number) => void = + (fileId: string, projectName: string, newLength: number): void => { + // get the file descriptor of the file to be truncated + const fd: number = fs.openSync(`${CONTENT_BASE_DIRECTORY}/${projectName}/${fileId}`, 'r+'); + fs.ftruncateSync(fd, newLength); } export const files: { -- cgit v1.2.1 From b9cb14f9368e281ab01d92cc8864cdcb4a0cb386 Mon Sep 17 00:00:00 2001 From: jw255 Date: Thu, 19 Apr 2018 11:08:40 +0100 Subject: minor fixes --- src/files.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'src/files.ts') diff --git a/src/files.ts b/src/files.ts index 92ed165..1b8a652 100644 --- a/src/files.ts +++ b/src/files.ts @@ -60,7 +60,11 @@ export const saveFile: (filename: string, projectname: string, fileId: string, o // open streams to append to file let rStream = fs.createReadStream(`${CONTENT_BASE_DIRECTORY}/temp-${filename}`); let wStream = fs.createWriteStream(`${CONTENT_BASE_DIRECTORY}/${projectname}/${fileId}`, {'flags':'a'}); - rStream.pipe(wStream); + rStream.pipe(wStream).on('finish', () => { + fs.removeSync(`${CONTENT_BASE_DIRECTORY}/temp-${filename}`); + }).on('error', (err: any) => { + logger.debug(`Error while saving file to proper location: ${err}`); + }); } export const deleteFile: (fileId: string, projectName: string) => void = -- cgit v1.2.1 From 8af06a4e7ea7ebaa29559a345d28791079b77dd3 Mon Sep 17 00:00:00 2001 From: jw255 Date: Thu, 19 Apr 2018 14:21:38 +0100 Subject: no reset --- src/files.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/files.ts') diff --git a/src/files.ts b/src/files.ts index 1b8a652..5b15a10 100644 --- a/src/files.ts +++ b/src/files.ts @@ -169,7 +169,7 @@ export const views: { getResponseData: async( file: File, project: Project, query: Query ): Promise => { - return readableStream(file.name, project.name, query); + return readableStream(file.uuid, project.name, query); }, getResponseFunction: (req: Request, res: Response): Function | null => { return res.send; -- cgit v1.2.1