-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
131 lines (102 loc) · 3.3 KB
/
server.js
File metadata and controls
131 lines (102 loc) · 3.3 KB
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
let express = require("express"),
app = express(),
exphbs = require("express-handlebars"),
imagesArray = [],
resizeImg = require('resize-img');
const http = require('http'),
fs = require('fs'),
tqdm = require('tqdm');
PORT = 80;
const util = require('util');
const exec = util.promisify(require('child_process').exec);
// Sets up the Express app to handle data parsing
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
// set public directory for assetts
app.use(express.static('public'));
app.engine("handlebars", exphbs({ defaultLayout: "main" }));
app.set("view engine", "handlebars");
async function blip(file) {
const { stdout, stderr } = await exec(`py blip.py ./public/img/${file}`);
console.log(file + ':' + stdout);
return stdout;
}
function preProcessImage(image) {
console.log(image);
}
let id = 0,
caption = "",
directoryList = [],
directoryListSimple = [],
assetPath = "./public/img/";
fs.readdir('./public/img/', (err, files) => {
for (let file of tqdm(files)) {
console.log(file)
let isDir = fs.existsSync(`${assetPath}${file}`) && fs.lstatSync(`${assetPath}${file}`).isDirectory();
console.log(isDir);
if (isDir) {
directoryList.push(`${assetPath}${file}`);
directoryListSimple.push(file);
}
else if (!file.includes('thumb.png')) {
(async () => {
const image = await resizeImg(fs.readFileSync(`${assetPath}${file}`), {
width: 128,
height: 128
});
imagesArray.unshift({
name: file,
image: `img/${file}`,
thumb: `img/${file}.thumb.png`,
description: caption.trim(),
id: id
});
id++;
fs.writeFileSync(`./public/img/${file}.thumb.png`, image);
})();
}
}
if (directoryList.length > 0) {
for (let dir of directoryList) {
console.log(dir);
let dirImagesArray = [];
app.get(`/${directoryListSimple[directoryList.indexOf(dir)]}`, (req, res) => {
res.render("index", { directoryListSimple: directoryListSimple, images: dirImagesArray });
})
let dirFiles = fs.readdirSync(dir);
for (let file of dirFiles) {
// console.log(file);
console.log(file);
if (!file.includes('thumb.png')) {
(async () => {
const image = await resizeImg(fs.readFileSync(`${dir}/${file}`), {
width: 128,
height: 128
});
dirImagesArray.unshift({
name: file.split("_")[0],
image: `img/${directoryListSimple[directoryList.indexOf(dir)]}/${file}`,
thumb: `img/${directoryListSimple[directoryList.indexOf(dir)]}/${file}.thumb.png`,
description: caption.trim(),
id: id
});
id++;
fs.writeFileSync(`${dir}/${file}.thumb.png`, image);
// fs.writeFileSync(`./public/img/${dir}/${file}.thumb.png`, image);
})();
}
}
}
}
})
app.get('/', (req, res) => {
res.render("index", { directoryListSimple: directoryListSimple, images: imagesArray });
});
app.get('/about', (req, res) => {
res.render("about", { directoryListSimple: directoryListSimple, images: imagesArray });
});
let options = {},
server = http.createServer(options, app);
server.listen(PORT, () => {
console.log("server starting on port : " + PORT)
});