diff options
| author | jw255 | 2018-04-18 16:50:30 +0100 |
|---|---|---|
| committer | jw255 | 2018-04-18 16:50:30 +0100 |
| commit | f37c8433ea3bca786898c46c03b86557fa0b6bb3 (patch) | |
| tree | e556bbc52dcb3c8345d73221339c352075338608 /src/files.ts | |
| parent | using multer for files now (diff) | |
| download | medicine-f37c8433ea3bca786898c46c03b86557fa0b6bb3.tar.gz medicine-f37c8433ea3bca786898c46c03b86557fa0b6bb3.zip | |
POST file parameters maybe working
Diffstat (limited to 'src/files.ts')
| -rw-r--r-- | src/files.ts | 26 |
1 files changed, 23 insertions, 3 deletions
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: { |
