diff options
| author | Tom Harley | 2018-04-21 23:02:58 +0100 |
|---|---|---|
| committer | Tom Harley | 2018-04-21 23:02:58 +0100 |
| commit | 661ee231f676c642545b38ea43568acef777d3a7 (patch) | |
| tree | abe2f305de31881e613d97a22969ceec1585a2d7 /src/files.ts | |
| parent | Add support for tabular files (diff) | |
| parent | implimented moving and copying of files (diff) | |
| download | medicine-661ee231f676c642545b38ea43568acef777d3a7.tar.gz medicine-661ee231f676c642545b38ea43568acef777d3a7.zip | |
Merge branch ‘trh/gets’ of gitlab.cs.st-andrews.ac.uk:cs3099group-be-4/project-code into trh/gets
Diffstat (limited to 'src/files.ts')
| -rw-r--r-- | src/files.ts | 38 |
1 files changed, 37 insertions, 1 deletions
diff --git a/src/files.ts b/src/files.ts index af01798..aa4e24f 100644 --- a/src/files.ts +++ b/src/files.ts @@ -80,6 +80,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 pathNoView: (file: string, project: string) => string = + (file: string, project: string): string => + `${CONTENT_BASE_DIRECTORY}/${project}/${file}`; + export const tempPath: (file: string, project: string) => string = ( file: string ): string => @@ -98,7 +102,7 @@ export const saveFile: (filename: string, projectname: string, fileId: string, o let rStream = fs.createReadStream(`${CONTENT_BASE_DIRECTORY}/temp-${filename}`); let wStream = fs.createWriteStream(path(fileId, projectname), {'flags':'a'}); rStream.pipe(wStream).on('finish', async() => { - await fs.remove(`${CONTENT_BASE_DIRECTORY}/temp-${filename}`); + fs.remove(`${CONTENT_BASE_DIRECTORY}/temp-${filename}`); }).on('error', (err: any) => { logger.error(`Error while saving file to proper location: ${err}`); }); @@ -117,6 +121,38 @@ export const truncateFile: (fileId: string, projectName: string, newLength: numb await fs.ftruncate(fd, newLength); } +export const copyFile: (projName: string, fromId: string, toId: string, move?: boolean) + => Promise<void> = async(projName: string, fromId: string, toId: string, move?: boolean): Promise<void> => { + // ensure to path exists + fs.ensureDirSync(pathNoView(toId, projName)); + const fromDir: string = pathNoView(fromId, projName); + const toDir: string = pathNoView(toId, projName); + // loop all views in file + fs.readdir(fromDir, (err: any, files: string[]) => { + if(err){ + logger.error(`Error while copying file: ${err}`); + } else { + files.forEach((file: string, index: number) => { + const fromPathFull = fromDir + "/" + file; + const toPathFull = toDir + "/" + file; + fs.ensureFileSync(toPathFull); + // open streams to transfer data + let rStream = fs.createReadStream(fromPathFull); + let wStream = fs.createWriteStream(toPathFull); + rStream.pipe(wStream).on('finish', async() => { + if(!!move){ + await fs.remove(fromPathFull); + } + console.log(`copy success of file from \'${fromPathFull}\' to \'${toPathFull}\'`) + }).on('error', (err: any) => { + logger.error(`Error while copying file: ${err}`); + }); + }) + } + }); + +} + export const files: { path: (f: string, p: string) => string; writableStream: (f: string, p: string) => fs.WriteStream; |
