Skip to content

Instantly share code, notes, and snippets.

@herbert1228
Created November 12, 2018 11:14
Show Gist options
  • Save herbert1228/2e21be8e02f25e01956eaaa91e3cc8fe to your computer and use it in GitHub Desktop.
Save herbert1228/2e21be8e02f25e01956eaaa91e3cc8fe to your computer and use it in GitHub Desktop.
var fs = require('fs');
var formidable = require('formidable');
var MongoClient = require('mongodb').MongoClient;
var assert = require('assert');
var ObjectID = require('mongodb').ObjectID;
const express = require('express')
const app = express()
const mongourl = process.env.MONGOURL || "mongodb://developer:developer123@ds143593.mlab.com:43593/herbert1228";
MongoClient.connect(mongourl, function (err, db) {
try {
assert.equal(err, null);
console.log('Connected to MongoDB')
} catch (err) {
res.status(500).end("MongoClient connect() failed!");
return (-1);
}
app.set('view engine', 'ejs')
app.post('/fileupload', (req, res) => {
const form = new formidable.IncomingForm();
form.parse(req, function (err, fields, files) {
console.log(JSON.stringify(files));
const filename = files.filetoupload.path;
if (files.filetoupload.size == 0) {
res.status(500).end('No file uploaded!')
}
const title = (fields.title.length > 0) ? fields.title : "untitled";
const desc = (fields.desc.length > 0) ? fields.desc : "untitled";
const mimetype = files.filetoupload.type
console.log("title = " + title);
console.log("filename = " + filename);
fs.readFile(filename, function (err, data) {
const new_photo = {
title,
desc,
mimetype,
image: new Buffer(data).toString('base64')
}
insertPhoto(db, new_photo, function (result) {
res.status(200).end('Photo was inserted into MongoDB!');
})
})
})
})
app.get('/photos', (req, res) => {
findPhoto(db, {}, function (photos) {
res.render('list', {photos: photos})
})
})
app.get('/display', (req, res) => {
const criteria = {_id: ObjectID(req.query._id)}
findPhoto(db, criteria, function (photo) {
console.log('Photo returned = ' + photo.length);
const p = {image, title, desc, mimetype} = photo[0]
const contentType = {};
if (mimetype == "image/jpeg" || mimetype == "image/png") {
console.log(`Preparing to send ${mimetype}`);
res.render('photo', p)
} else {
res.status(500).end("Not JPEG format!!!");
}
});
})
app.get('/upload', (req, res) => {
res.render('upload', null)
})
app.get('*', (req, res) => {
res.redirect('/photos')
})
app.listen(4000, () => console.log('server listening port 4000'))
})
function insertPhoto(db, r, callback) {
db.collection('photo').insertOne(r, function (err, result) {
assert.equal(err, null);
console.log("insert was successful!");
console.log(JSON.stringify(result));
callback(result);
});
}
function findPhoto(db, criteria, callback) {
var cursor = db.collection("photo").find(criteria);
var photos = [];
cursor.each(function (err, doc) {
assert.equal(err, null);
if (doc != null) {
photos.push(doc);
} else {
callback(photos);
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment