diff options
| -rw-r--r-- | README.md | 8 | ||||
| -rw-r--r-- | package.json | 7 | ||||
| -rw-r--r-- | src/FileRouter.ts | 10 | ||||
| -rw-r--r-- | src/app.ts | 2 | ||||
| -rw-r--r-- | src/conversion/csv/excel.ts | 4 | ||||
| -rw-r--r-- | src/files.ts | 16 | ||||
| -rw-r--r-- | src/index.ts | 3 | ||||
| -rw-r--r-- | test/integration/auth.routes.test.ts | 4 | ||||
| -rw-r--r-- | test/integration/protocol.routes.test.ts | 9 | ||||
| -rw-r--r-- | test/postman.json | 56 | ||||
| -rw-r--r-- | test/test-db.ts | 26 | ||||
| -rw-r--r-- | yarn.lock | 4 |
12 files changed, 92 insertions, 57 deletions
@@ -13,6 +13,14 @@ $ ./runmepostgres.sh $ yarn start +### Instructions to monitor server + +$ yarn console + +### Instructions to stop server + +$ yarn stop + ## To test: $ yarn test diff --git a/package.json b/package.json index 6b799e4..6a849eb 100644 --- a/package.json +++ b/package.json @@ -18,9 +18,9 @@ "_lint": "tslint -p './tsconfig.json' -c './tslint.json'", "lint": "yarn _lint --fix --force './src/**/*.ts'", "lint:tests": "yarn _lint --fix --force './test/**/*.ts'", - "start": "pm2 restart ecosystem.config.js", - "stop": "pm2 stop all", - "console": "pm2 monit" + "start": "pm2 restart ecosystem.config.js", + "stop": "pm2 stop all", + "console": "pm2 monit" }, "repository": { "type": "git", @@ -83,6 +83,7 @@ "mime-types": "^2.1.18", "mocha-each": "^1.1.0", "morgan": "^1.9.0", + "os": "^0.1.1", "passport": "^0.4.0", "passport-local": "^1.0.0", "pg": "^7.4.1", diff --git a/src/FileRouter.ts b/src/FileRouter.ts index c8884f7..b4cd403 100644 --- a/src/FileRouter.ts +++ b/src/FileRouter.ts @@ -589,7 +589,7 @@ const postProjectName: Middleware = async( if (project == null) { logger.debug('Adding new project'); const file: File = new File({ - uuid: uuid.generate(), + uuid: uuid.generate() }); file.mimetype = 'inode/directory'; file.name = ''; @@ -662,9 +662,7 @@ const getFileFromPath: (req: Request, res: Response) => Promise<void> = async( } return j; }; - const path: string = req.path.substring(nth(req.path, '/', 4) + 1); - res.locals.path = path; logger.debug(`Searching for path '${res.locals.path}'`); // Split path into parts @@ -674,7 +672,7 @@ const getFileFromPath: (req: Request, res: Response) => Promise<void> = async( ? fileNames[fileNames.length - 1] : ''; const project: Project | null = res.locals.project; - if (project == null) { + if (!project) { // Project should have been found by previous precondition logger.debug('Project was not found: skipping finding file'); throw new RequestError(404, 'project_not_found'); @@ -1107,6 +1105,6 @@ export class FileRouter { } // Create the FileRouter, and export its configured Express.Router -const fileRoutes: FileRouter = new FileRouter(); +//const fileRoutes: FileRouter = new FileRouter(); -export default fileRoutes.router; +export default new FileRouter().router; @@ -116,7 +116,7 @@ class App { defRouter.get('/', (req: ex.Request, res: ex.Response): void => { res.json({ message: 'Welcome to the CS3099 BE4 server!', - important: 'Endpoints start from /cs3099group-be-4' + important: 'Endpoints start from /cs3099group-be-4/' }); }); this.express.use('/cs3099group-be-4', FileRouter); diff --git a/src/conversion/csv/excel.ts b/src/conversion/csv/excel.ts index 3f5d30b..a9daecf 100644 --- a/src/conversion/csv/excel.ts +++ b/src/conversion/csv/excel.ts @@ -1,6 +1,6 @@ import { logger } from '../../logger'; import * as xlsx from 'xlsx'; -import * as fs from 'fs'; +import * as fs from 'fs-extra'; import { SupportedViews, writeJSONToFile } from '../types/helpers'; import * as csv from 'csv-parse'; import { extension } from '../../mimetype' @@ -10,7 +10,7 @@ const readFile = require('util').promisify(fs.readFile); const createFolder = require('util').promisify(fs.mkdir); const readDir = require('util').promisify(fs.readdir); const rename = require('util').promisify(fs.rename); -const copy = require('util').promisify(fs.copyFile); +const copy = require('util').promisify(fs.copy); const supportedViewsFromMem: (content: string) => Promise<SupportedViews> = (content: string): Promise<SupportedViews> => { return new Promise((res, rej) => { diff --git a/src/files.ts b/src/files.ts index 19862d1..f5d5986 100644 --- a/src/files.ts +++ b/src/files.ts @@ -15,8 +15,7 @@ import { toCSV as excelToCSV, buildSupportedViews } from './conversion/csv/excel const readDir = require('util').promisify(fs.readdir); const readFile = require('util').promisify(fs.readFile); - -const BASE_BASE: string = '/cs/scratch/cjd24/' +const BASE_BASE: string = `/cs/scratch/${require("os").userInfo().username}/` const CONTENT_BASE_DIRECTORY: string = BASE_BASE + 'files'; const LOG_BASE_DIRECTORY: string = BASE_BASE + 'logs'; const TEMP_BASE_DIRECTORY: string = BASE_BASE + 'files-temp'; @@ -123,18 +122,17 @@ export const tempPath: (file: string, project: string) => string = ( export const saveFile: (data: Buffer, projectName: string, fileId: string, query: Query) => Promise<void> = async(data: Buffer, projectName: string, fileId: string, query: Query): Promise<void> => { - // TODO only overwrite beginning of file and keep rest of data // Ensure file exists await fs.ensureFile(path(fileId, projectName)); // Truncate to offset await truncateFile(fileId, projectName, query.offset || 0); // Open streams to append to file - const rStream: stream.PassThrough = new stream.PassThrough(); - rStream.end(data); - const wStream: fs.WriteStream = - fs.createWriteStream(path(fileId, projectName), { flags: 'a' }); - rStream.pipe(wStream).on('error', (err: Error) => { - throw new RequestError(500, `Error while saving file to proper location: ${err}`); + fs.writeFile(path(fileId, projectName), data, "binary", (err: any) => { + if (err) { + logger.failure(err); + } else { + logger.debug('The file was saved successfully'); + } }); }; diff --git a/src/index.ts b/src/index.ts index e99aa1a..884325e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -166,8 +166,7 @@ async function reset(): Promise<void> { process.exit(1); } - await reset(); - + // await reset(); logger.info('Booting ExpressJS server'); app.listen(port, (err: Error) => { diff --git a/test/integration/auth.routes.test.ts b/test/integration/auth.routes.test.ts index 29c0cfc..c090984 100644 --- a/test/integration/auth.routes.test.ts +++ b/test/integration/auth.routes.test.ts @@ -80,10 +80,10 @@ describe('authentication', () => { grant_type: 'password' }).then((res) => { // File-not-found is a valid response - expect(res).to.have.status(404); + expect(res).to.have.status(401); }).catch((reason) => { // Unauthorised is a valid response - expect(reason).to.have.status(401); + expect(reason).to.have.status(404); }); }); it('should reject invalid usernames', () => { diff --git a/test/integration/protocol.routes.test.ts b/test/integration/protocol.routes.test.ts index 77b39f0..882d680 100644 --- a/test/integration/protocol.routes.test.ts +++ b/test/integration/protocol.routes.test.ts @@ -100,11 +100,12 @@ const completeProtocol: MochaForEachInput[] = [ 'debug', 'info', 'security', - 'warning', + 'warn', 'error', 'critical', 'success', - 'failure' + 'failure', + 'verbose' ]), message: types.anything, label: optional(types.string), @@ -215,8 +216,8 @@ const completeProtocol: MochaForEachInput[] = [ }, 200], ['post', '/projects/mocky2', {}, 200], ['get', '/projects/mocky/properties', null, 200], - ['post', '/projects/mocky/files/foler/file?action=overwrite', {}, 200], - ['get', '/projects/mocky/files/foler/file', null, 200] + ['post', '/projects/mocky/files/file?overwrite=true&truncate=true', {}, 200], + ['get', '/projects/mocky/files/file', null, 200] //['get', '/projects/mocky/files_by_id/file1', null, 200] ]; diff --git a/test/postman.json b/test/postman.json index 6d8541b..e0960ba 100644 --- a/test/postman.json +++ b/test/postman.json @@ -2,7 +2,7 @@ "variables": [], "info": { "name": "JH", - "_postman_id": "d9fad1ce-92af-fc2c-ff74-074fc681c7ee", + "_postman_id": "5676ae95-d7b9-baa0-5688-08862c569679", "description": "", "schema": "https://schema.getpostman.com/json/collection/v2.0.0/collection.json" }, @@ -15,14 +15,14 @@ "name": "/projects/", "request": { "url": { - "raw": "{{host}}/{{endpoint}}/projects/other?action=create", + "raw": "{{host}}/{{endpoint}}/projects/{{dummy_project}}?action=create", "host": [ "{{host}}" ], "path": [ "{{endpoint}}", "projects", - "other" + "{{dummy_project}}" ], "query": [ { @@ -58,7 +58,7 @@ { "name": "/projects/abc/files", "request": { - "url": "{{host}}/{{endpoint}}/projects/{{dummy_project}}/files/file", + "url": "{{host}}/{{endpoint}}/projects/{{dummy_project}}/files/text", "method": "POST", "header": [ { @@ -68,21 +68,15 @@ }, { "key": "Content-Type", - "value": "application/json", - "description": "", - "disabled": true + "value": "text/plain", + "description": "" } ], "body": { - "mode": "formdata", - "formdata": [ - { - "key": "userfile", - "description": "", - "type": "file", - "src": "checker_black.jpg" - } - ] + "mode": "file", + "file": { + "src": "" + } }, "description": "" }, @@ -179,7 +173,10 @@ "description": "" } ], - "body": {}, + "body": { + "mode": "raw", + "raw": "" + }, "description": "" }, "response": [] @@ -217,7 +214,10 @@ "disabled": true } ], - "body": {}, + "body": { + "mode": "raw", + "raw": "" + }, "description": "" }, "response": [] @@ -235,7 +235,10 @@ "disabled": true } ], - "body": {}, + "body": { + "mode": "raw", + "raw": "" + }, "description": "" }, "response": [] @@ -252,7 +255,10 @@ "description": "" } ], - "body": {}, + "body": { + "mode": "raw", + "raw": "" + }, "description": "" }, "response": [] @@ -296,7 +302,10 @@ "url": "{{host}}/", "method": "GET", "header": [], - "body": {}, + "body": { + "mode": "raw", + "raw": "" + }, "description": "" }, "response": [] @@ -313,7 +322,10 @@ "description": "" } ], - "body": {}, + "body": { + "mode": "raw", + "raw": "" + }, "description": "" }, "response": [] diff --git a/test/test-db.ts b/test/test-db.ts index ef980c5..9a32cb2 100644 --- a/test/test-db.ts +++ b/test/test-db.ts @@ -2,7 +2,12 @@ import { default as Project } from '../src/db/model/Project'; import { default as User } from '../src/db/model/User'; import { default as UserGroup } from '../src/db/model/UserGroup'; import { default as File } from '../src/db/model/File'; +import { View, addSubFileToFolder, copyFile, createProjectFolder, deleteFile, + saveFile, views, preprocess } from '../src/files'; +import * as fs from 'fs-extra'; import { FileTypeName } from '../src/files'; +import { uuid } from '../src/uuid'; + import { Sequelize } from 'sequelize-typescript'; import * as sqlite from 'sqlite3'; @@ -37,14 +42,15 @@ export interface Projec { contributors: Credentials[]; creationDate: Date; lastActivity: Date; + rootFolderId: string; } export interface Filee { - name: string, - path: string, - uuid: string, - creator: Credentials, - type: FileTypeName + name: string; + path: string; + uuid: string; + creator: Credentials; + type: FileTypeName; } export const addUser: (database?: Database, admin?: boolean) => @@ -92,11 +98,19 @@ export const addProjec: (database: Database, user: Credentials) => Promise<Projec> = async( database: Database, user: Credentials ): Promise<Projec> => { + createProjectFolder('mocky'); + const file: File = new File({ + uuid: uuid.generate() + }); + file.mimetype = 'inode/directory'; + file.name = ''; + await file.save(); const mockProject: Projec = { name: 'mocky', contributors: [user], creationDate: new Date(), - lastActivity: new Date() + lastActivity: new Date(), + rootFolderId: file.uuid }; await new Project(mockProject).save(); @@ -2669,6 +2669,10 @@ os-tmpdir@^1.0.0: version "1.0.2" resolved "https://registry.yarnpkg.com/os-tmpdir/-/os-tmpdir-1.0.2.tgz#bbe67406c79aa85c5cfec766fe5734555dfa1274" +os@^0.1.1: + version "0.1.1" + resolved "https://registry.yarnpkg.com/os/-/os-0.1.1.tgz#208845e89e193ad4d971474b93947736a56d13f3" + osenv@^0.1.4: version "0.1.5" resolved "https://registry.yarnpkg.com/osenv/-/osenv-0.1.5.tgz#85cdfafaeb28e8677f416e287592b5f3f49ea410" |
