Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
.env

node_modules/

tmp/
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ Copy the .env.sample file to a .env file and fill in the missing values
- The .env file should not be checked into the repository, it contains production secrets!
- When starting up, the server will read the database credentials/other info from the .env file.

Run `npm run knex seed:run` to setup the initial database state
Run `npm run knex seed:run` and `npm run knex migrate:latest` to setup the initial database state
Run `npm run start` to start the server



### Setup Postgres
These are some helpful tutorials:
- https://blog.logrocket.com/setting-up-a-restful-api-with-node-js-and-postgresql-d96d6fc892d8/
Expand Down
131 changes: 131 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"main": "src/server.js",
"scripts": {
"knex": "knex --knexfile ./src/database/knexfile.js",
"start": "node src/server.js"
"start": "node src/server.js",
"setup": "npm run knex seed:run & npm run knex migrate:latest"
},
"author": "Vedant Roy",
"license": "ISC",
Expand All @@ -14,6 +15,7 @@
"express": "^4.17.1",
"find-config": "^1.0.0",
"knex": "^0.20.10",
"multer": "^1.4.2",
"pg": "^7.18.2"
},
"devDependencies": {}
Expand Down
17 changes: 17 additions & 0 deletions src/database/migrations/file_migrate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// NOTE TO DEVS:
// You can also run "npm run knex migrate:down file_migrate.js" to drop the table
// if you run into any issues.

// Adds a files table that has a relation with the user table
exports.up = async (knex) => {
return await knex.schema.createTable('files', (table) => {
table.string('filename');
table.binary('file');
table.string('username');
});
}

// Drops the files table
exports.down = async (knex) => {
return await knex.schema.dropTable('files');
}
42 changes: 42 additions & 0 deletions src/routes/files.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const fs = require('fs');
const knex = require('../database/database');
const multer = require('multer');
const router = require('express').Router();

const dir = "\\tmp";

let storage = multer.diskStorage({
// file uploaded is stored in local directory
destination: (req, file, cb) => {
const uploadDir = "\\tmp/uploads/"
// create a new local directory if it did not already exist
fs.exists(uploadDir, exist => {
if (!exist) {
return fs.mkdir(uploadDir, {recursive: true }, err => cb(err, uploadDir));
}
return cb(null, uploadDir);
});
},
filename: (req, file, cb) => {
cb(null, Date.now() + '-' + file.originalname);
}
});

let upload = multer({ storage: storage });

// [base] = localhost:[port]/files
// POST [base]/upload/[a username] attached with a file that has fieldname:'myfile'
// to upload an arbitrary file to the database
router.post('/upload/:user', upload.single('myfile'), async (req, res, next) => {
try {
let username = req.params.user;
let path = req.file.destination + req.file.filename
await knex.raw('INSERT INTO "files" VALUES (?, pg_read_binary_file(?), ?)',
[req.file.filename, path, username]);
res.send("Succesfully uploads file");
} catch (error) {
console.log(error);
}
});

module.exports = router;
1 change: 1 addition & 0 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const express = require('express')
const app = express()

app.use('/users', require('./routes/users'))
app.use('/files', require('./routes/files'))

const port = 8080
app.listen(port, () => console.log(`Server listening on port ${port}.`))