-
Notifications
You must be signed in to change notification settings - Fork 0
/
engines.js
144 lines (127 loc) · 4.78 KB
/
engines.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/**
# UPLOAD IMAGES TO REPOSITORIES
* upload images to mongodb using mongoose and multer
*/
// require dependencies
require("dotenv").config();
const express = require("express");
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
const multer = require("multer");
const GridFsStorage = require("multer-gridfs-storage");
const Grid = require("gridfs-stream");
const methodOverride = require("method-override");
const path = require("path");
const util = require("util");
const crypto = require("crypto");
// end of requiring dependencies
// varibale declaration and assignments
const app = express();
let gfs;
// end of variable declarations
/**
* initialize middleware
*/
app.set("view engine", "ejs");
app.use(bodyParser.json());
app.use(methodOverride("_method"));
// end of initializing middleware
/**
### GALLERY DATABASE ================================================================================= /
* create model for gallery collection
* create storage engine
* initialize gridfs stream
*/
// configure model for gallery collection
const gallerySchema = mongoose.Schema({
path: {type: String}, // store path of uploaded image
caption: {type: String} // user caption
});
const Gallery = mongoose.model("Gallery", gallerySchema);
// create storage engine
const storage = new GridFsStorage({
url: process.env.GALLERYDB_URI,
options: {useNewUrlParser: true, useUnifiedTopology: true},
file: function(req, file){
return new Promise(function(resolve, reject){
crypto.randomBytes(16, function(err, buf){
if (err) {
return reject(err);
}
const fileInfo = {
filename: "image_" + Date.now() + path.extname(file.originalname),
bucketName: "uploads"
};
resolve(fileInfo);
});
});
}
});
// initialize GridFS stream and upload up to 5 images
const upload = multer({storage: storage}).array("files", 5);
const uploadFilesEngine = util.promisify(upload);
const connect = mongoose.createConnection(process.env.GALLERYDB_URI, {useUnifiedTopology: true, useNewUrlParser: true});
connect.once("open", function(){
gfs = Grid(connect.db, mongoose.mongo);
gfs.collection("uploads");
});
// ------------------------------------------------------------------------------------------------- /
// handle multiple image uploads and export to `app.js`
async function uploadImages(request, respond){
try {
await uploadFilesEngine(request, respond);
console.log(request.files);
if(request.files.length <= 0)
return respond.send("Ay, select a file! At least ONE file!");
else {
respond.redirect("/upload");
}
} catch (err) {
console.log(err);
if (err.code === "LIMIT_UNEXPECTED_FILE")
return respond.send("Woah, calm down! How many fingers you got?");
else
return respond.send("Might I interest you in upload error? No? Ok.");
}
}
module.exports.uploadFiles = uploadImages;
// ================================================================================================= /
/**
* CALLBACKS FOR APP.JS ROUTES ===================================================================== //
*/
// display image
module.exports.displayImage = function(request, respond){
gfs.files.findOne({filename: request.params.filename}, function(error, file){
if(!file || file.length === 0)
return respond.status(404).send("Oops. The file you are looking for does not exist");
else {
return gfs.createReadStream(file.filename).pipe(respond);
}
});
}
// load web gallery
module.exports.galleryDisplay = function(request, respond){
gfs.files.find().toArray(function(error, files){
if(!files || files.length === 0)
respond.render("gallery", {files: false});
else {
files.map(function(file){
if(file.contentType === "image/jpeg" || file.contentType === "image/png")
file.isImage = true;
else
file.isImage = false;
});
respond.render("gallery", {files: files})
}
});
}
// delete images
module.exports.deleteImage = function(request, respond){
gfs.remove({_id: request.params.id, root: "uploads"}, function(error, gridStore){
if(error)
return respond.send("You won't let me live... You won't let me die");
else
respond.redirect("/upload");
});
}
// ================================================================================================= //