forked from nirgeier/NodeJsExpressMongoDBTutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
photos.js
128 lines (110 loc) · 2.78 KB
/
photos.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
/**
* This file will contain the data for our photos application
**/
var Photos = require('../models/Photos');
var path = require('path');
var fs = require('fs');
var join = path.join;
/**
* This method will render out the images
* The funciton is a middleware
*
* @param req - The request object
* @param res - The response object
* @param next
*
*/
function listImages(req, res, next) {
// Use the mongoose to find all the images in the model
Photos.find({}, // Find all images
function (err, photos) {
// Check for error
if (err) {
return next(err);
}
// Render the images gallery
res.render('photos', {
title: 'Photos',
photos: photos
});
});
}
/**
* Get the upload form
* @param req
* @param res
* @param next
*/
function getUploadForm(req, res, next) {
res.render('photos/upload', {
title: 'Photo upload form'
});
}
/**
* The submit for (upload) action
*
* @param dir - The directory to save the images to
* @returns {Function}
*/
function uploadImage(dir) {
// This is middleware so we have the middleware parameters
// The file are already parsed with the parseBody middleware
return function (req, res, next) {
var img = req.file, // Get the images - if any
name = img.originalname, // Get the image name
path = join(dir, img.originalname); // Set the path where to store the image = dir+imgName
// Use the fs module to create and save the file
fs.rename(
img.path, // Old path
path, // New path
function (err) { // callback
// Check to see if there wa any error while trying to move the image around
if (err) {
return next(err);
}
// Add the Photo to our DB
Photos.create({
name: name,
path: name
},
function (err) {
// If there was an error while trying to add the image to the model
// "skip" to the next middleware
if (err) {
return next(err);
}
// Display the images gallery page
res.redirect('/');
});
});
};
}
/**
*
* @param dir
* @returns {Function}
*/
function downloadImage(dir) {
return function (req, res, next) {
// Get the image id
var id = req.params.id;
// find the image in the DB
Photos.findById(id,
function (err, photo) {
if (err) {
return next(err);
}
// get the image path
var path = join(dir, photo.path);
// Download the image name
res.download(path, photo.name + '.jpeg');
});
};
}
// Expose the public methods
module.exports = {
listImages: listImages,
uploadImage: uploadImage,
downloadImage: downloadImage,
getUploadForm: getUploadForm
};