-
Notifications
You must be signed in to change notification settings - Fork 0
/
serverHelper.js
65 lines (54 loc) · 1.95 KB
/
serverHelper.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
const fs = require("fs"),
{ unzip, readDir, grayScale } = require("./milestone1/IOhandler"),
path = require('path');
// check for request url, stream the requested file as response
const responseFile = (req, res) => {
// main page html, or upload page html, or other assosicated files.
if (req.url === '/favicon.ico' || req.url === '/') {
let stream = fs.createReadStream(path.join(__dirname, 'main.html'));
stream.pipe(res);
} else if (req.url === '/upload') {
let stream = fs.createReadStream(path.join(__dirname, 'webfile/upload.html'));
stream.pipe(res);
} else {
let stream = fs.createReadStream(__dirname + req.url);
stream.pipe(res);
}
};
// apply grayscale to newly uploaded photos in gallery
const processGrayscale = () => {
return new Promise((resolve, reject) => {
// an array of uploaded photos
readDir('gallery')
.then(gallery => {
// an array of uploaded photos that are grayscaled
return readDir('galleryGrayscale')
.then(galleryGrayscale => {
// find the photos that are not yet grayscaled, then apply grayscale
for (let photo of gallery) {
if (!galleryGrayscale.includes('galleryGrayscale/' + path.basename(photo))) {
grayScale(photo, path.join(__dirname, 'galleryGrayscale'));
}
}
});
})
.then(() => resolve())
.catch(err => reject(err));
});
};
// update newly added photos/grayscaled photos to gallery display
const updateGallery = (req, res, photos) => {
if (req.url === '/') {
res.writeHead(200, { 'content-type': 'text/html' });
// response each photo in gallery & galleryGrayscale to client mainpage
for (let photo of photos) {
res.write(`<body><img class='photo' src=${photo}></body>`);
res.write(`<body><img class='photo' src=${path.join('galleryGrayscale', path.basename(photo))}></body>`);
}
}
};
module.exports = {
responseFile,
processGrayscale,
updateGallery
};