-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouter.js
124 lines (122 loc) · 5.48 KB
/
router.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
const express = require('express');
const bodyParser = require('body-parser');
const ejs = require('ejs');
const path = require('path');
const formidable = require('express-formidable');
const fs = require('fs');
const jimp = require('jimp');
const HomeRoutes = express.Router();
const pathToPublic = path.join(__dirname,'public');
const pathToThumb = path.join(__dirname,'public','thumbnails');
const pathToFsize = path.join(__dirname,'public','fullsizes');
HomeRoutes.use(formidable({
maxFileSize: 5 * 1024 * 1024,
maxTotalFileSize: 5 * 1024 * 1024,
encoding: 'utf-8',
uploadDir: path.join(pathToPublic, 'upload'),
multiples: true,
keepExtensions: true
}));
const settings = {
defaultExtIfGif: '.png',
defaultExtIfElse: '.png',
defaultQuality: 80,
defaultThumbnailResizeWidth: 100,
defaultFullsizeResizeWidth: 600,
}
HomeRoutes.post('/', async function (req, res) {
if (req.fields?.title && req.fields?.message) {
if (req.files?.image?.size !== 0 && req.files?.image?.name !== '') {
const imageFile = path.parse(req.files.image.path);
const finalExt = (imageFile.ext === '.gif') ? settings.defaultExtIfGif : settings.defaultExtIfElse;
const loadPath = `${path.join(imageFile.dir, imageFile.base)}`;
const thumbPath = `${path.join(pathToThumb, imageFile.name.replace('upload_','')+finalExt)}`;
const fsizePath = `${path.join(pathToFsize, imageFile.name.replace('upload_','')+finalExt)}`;
let sizes = {
original: {
width: null,
height: null,
},
};
try {
await jimp.read(loadPath)
.then(image => {
sizes.original.width = image.bitmap.width;
sizes.original.height = image.bitmap.height;
return image
.resize(settings.defaultThumbnailResizeWidth, jimp.AUTO)
.quality(settings.defaultQuality)
.writeAsync(thumbPath)
})
.catch(err => {
throw err;
})
} catch (err) {
if (err) {
console.log(err)
try {
await jimp.read(loadPath)
.then(image => {
sizes.original.width = image.bitmap.width;
sizes.original.height = image.bitmap.height;
return image
.resize(settings.defaultThumbnailResizeWidth, jimp.AUTO)
.quality(settings.defaultQuality)
.writeAsync(thumbPath)
})
.catch(err => {
throw err;
})
} catch (err) {
if (err) throw err
}
}
}
try {
await jimp.read(loadPath)
.then(image => {
return image
.resize(settings.defaultFullsizeResizeWidth, jimp.AUTO)
.quality(settings.defaultQuality)
.writeAsync(fsizePath)
})
.catch(err => {
throw err;
})
} catch (err) {
if (err) {
console.log(err)
try {
await jimp.read(loadPath)
.then(image => {
return image
.resize(settings.defaultFullsizeResizeWidth, jimp.AUTO)
.quality(settings.defaultQuality)
.writeAsync(fsizePath)
})
.catch(err => {
throw err;
})
} catch (err) {
if (err) throw err
}
}
}
try {fs.unlink(req.files.image.path, (err) => {
if (err) throw err;
}) } catch (err) {
if (err) throw err;
}
res.render('home/home', { fields: { title: req.fields?.title, message: req.fields?.message }, thumbnail: `/thumbnails/${imageFile.name.replace('upload_','')}${finalExt}`, fullsize: `/fullsizes/${imageFile.name.replace('upload_','')}${finalExt}`, stats: { original: {size: req.files.image.size, path: req.files.image.path, name: req.files.image.name, type: req.files.image.type, hash: req.files.image.hash, hash2: imageFile.name.replace('upload_',''), modAt: req.files.image.lastModifiedDate, width: sizes.original.width, height: sizes.original.height }} })
}
else {
//error no image
res.render('home/home', { fields: { title: req.fields?.title, message: req.fields?.message }, thumbnail: 'empty.png', fullsize: 'logo.png', stats: null})
}
}
else {
//error title/message
res.render('home/home', { fields: { title: 'no title', message: 'no message' }, thumbnail: 'empty.png', fullsize: 'logo.png', stats: null})
}
});
module.exports = { "HomeRoutes": HomeRoutes };