Skip to content

Commit

Permalink
Added document uploader. Added tests. Fixed vagrantfile
Browse files Browse the repository at this point in the history
  • Loading branch information
Oleksandr Hutsulyak committed Jan 13, 2017
1 parent dd5622f commit cbcf8e2
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 54 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,5 @@ jspm_packages
.node_repl_history

.vagrant/

public/
4 changes: 2 additions & 2 deletions Vagrantfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ Vagrant.configure("2") do |config|
# Create a forwarded port mapping which allows access to a specific port
# within the machine from a port on the host machine. In the example below,
# accessing "localhost:8080" will access port 80 on the guest machine.
# config.vm.network "forwarded_port", guest: 80, host: 8080
config.vm.network "forwarded_port", guest: 8080, host: 5656

# Create a private network, which allows host-only access to the machine
# using a specific IP.
config.vm.network "private_network", ip: "192.168.33.10"
# config.vm.network "private_network", ip: "192.168.33.10"

# Create a public network, which generally matched to bridged network.
# Bridged networks make the machine appear as another physical device on
Expand Down
89 changes: 51 additions & 38 deletions app/routes/document.js
Original file line number Diff line number Diff line change
@@ -1,49 +1,49 @@
const multer = require('multer');
const fs = require('fs');
const storage = multer.diskStorage({
// uploaded files will go to public/uploads
destination: (req, file, cb) => {
cb(null, 'public/uploads');
},
// filename will be jsTimestamp.ext to avoid filenames collisions
filename: (req, file, cb) => {
cb(null, `${new Date().getTime()}${path.extname(file.originalname)}`);
}
});
const upload = multer({ storage });
const path = require('path');

const Document = require('../models/document');

module.exports = (router) => {
router.use(upload.single('document'));

router.route('/document')
/**
* Create document
*/
.post((req, res) => {
if (!req.files.document) {
if (!req.file) {
res.status(400).send({ message: 'No document files specified' });

return;
}

// Read uploaded document
fs.readFile(req.files.document.path, (err, data) => {
if (err) {
res.send(err);
return;
}

let newDocName = `/public/uploads/${req.files.document.name}`
let newPath = `${__dirname}${newDocName}`;
// Write uploaded document into new direction
fs.writeFile(newPath, data, (err) => {
if (err) {
res.send(err);
return;
}
// Store available document entry in database
const doc = new Document();
// Store available document entry in database
const doc = new Document();

doc.size = req.files.document.size;
doc.title = req.files.document.name;
doc.url = newDocName;
doc.size = req.file.size;
doc.title = req.file.originalname;
doc.url = req.file.path.replace('public', '');

// save the doc and check for errors
doc.save()
.then(() => {
res.json({ message: 'Document created', url: doc.url });
})
.catch((err) => {
res.send(err);
});
// save the doc and check for errors
doc.save()
.then((id) => {
res.json({ message: 'Document created', url: doc.url, id: doc._id });
})
.catch((err) => {
res.send(err);
});
});
})
/**
* Get all available documents
Expand All @@ -63,15 +63,28 @@ module.exports = (router) => {
* Delete document
*/
.delete((req, res) => {
Document.remove({
_id: req.params.id
}, (err, doc) => {
if (err) {
res.send(err);
}
// Remove file entry from db
Document.findById(req.params.id)
.then((doc) => {
const docUrl = doc.url;

res.json({ message: 'Document deleted' });
});
Document.remove({
_id: doc._id
}, (err) => {
if (err) {
res.send(err);
return;
}
// Remove file from disk
fs.unlink(`public${docUrl}`, (err) => {
if (err) {
res.send(err);
return;
}
res.json({ message: 'Document deleted' });
});
});
});
});

return router;
Expand Down
1 change: 0 additions & 1 deletion app/routes/service-category.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ module.exports = (router) => {
res.json({ message: 'Service category created' });
})
.catch((err) => {
console.log(err)
res.send(err);
});
})
Expand Down
19 changes: 7 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
// server.js

// BASE SETUP
// =============================================================================

// call the packages we need
const express = require('express'); // call express
const express = require('express');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const app = express(); // define our app using express
const app = express();

mongoose.connect('mongodb://developer:travis_ci@travis_ci.mlab.com:45828/active_travel'); // connect to our database
mongoose.connect('mongodb://developer:dev123@ds145828.mlab.com:45828/active_travel'); // connect to database

// configure app to use bodyParser()
// this will let us get the data from a POST
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

Expand All @@ -28,15 +24,14 @@ router.get('/', (req, res) => {
});

require('./app/routes/category')(router);
require('./app/routes/document')(router);
require('./app/routes/service-category')(router);
require('./app/routes/uploaded-image')(router);
require('./app/routes/document')(router);

// more routes for our API will happen here

// REGISTER OUR ROUTES -------------------------------
// all of our routes will be prefixed with /api
// REGISTER ROUTES -------------------------------
// all routes will be prefixed with /api
app.use('/api', router);
app.use(express.static('public'));

// START THE SERVER
// =============================================================================
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"dependencies": {
"body-parser": "^1.15.2",
"express": "^4.14.0",
"mongoose": "^4.7.6"
"mongoose": "^4.7.6",
"multer": "^1.2.1"
},
"devDependencies": {
"nodemon": "^1.11.0"
Expand Down

0 comments on commit cbcf8e2

Please sign in to comment.