-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheventbot.js
executable file
·130 lines (114 loc) · 4.12 KB
/
eventbot.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
#!/usr/bin/env node
console.log(process.env);
var fs = require('file-system');
var express = require('express');
var multer = require('multer');
var bodyParser = require('body-parser');
var app = express();
var fs = require('file-system');
const path = require('path');
var ExifImage = require('exif').ExifImage;
var sanitize = require("sanitize-filename");
app.use(bodyParser.json()); // to support JSON-encoded bodies
app.use(express.static(path.join(__dirname, "public")));
const mkdirSync = function (dirPath) {
try {
fs.mkdirSync(dirPath)
} catch (err) {
if (err.code !== 'EEXIST') throw err
}
};
// If this is a snap the current directory is not writable
var images_path;
if (process.env.SNAP_COMMON) {
images_path = path.join(process.env.SNAP_COMMON, "images");
app.use(express.static(process.env.SNAP_COMMON));
app.set('views', path.join(__dirname, 'views'));
} else {
images_path = "public/images";
}
console.log("Images path: " + images_path);
mkdirSync(images_path);
var Storage = multer.diskStorage({
destination: function(req, file, callback) {
callback(null, images_path);
},
filename: function(req, file, callback) {
var name = sanitize(req.body.name).replace(/ /g,"_");
callback(null, Date.now() + "_" + name + "_" + file.originalname);
}
});
var upload = multer({
storage: Storage
}).array("imgUploader", 10); //Field name and max count
var orientations = {};
app.get("/", function(req, res) {
//res.sendFile(__dirname + "/index.html");
var pictures = [];
var buttons = {};
fs.recurseSync(images_path,
['**/*.JPG', '**/*.PNG', '**/*.JPEG', '**/*.jpg', '**/*.png', '**/*.jpeg'],
function(filepath, relative, filename) {
try {
new ExifImage({ image : filepath }, function (error, exifData) {
if (error)
console.log('Error: '+error.message);
else {
orientations["/images/" + filename] = exifData.image.Orientation || 1;
}
});
} catch (error) {
console.log('Error: ' + error.message);
}
pictures.push("/images/" + filename);
});
for (i = 0; i < pictures.length; i += 10) {
buttons[i] = {
label: i,
url: '/?page=' + i
};
}
if (req.query.page > 0)
pictures.splice(0, req.query.page);
res.render("index.ejs", {
pictures: pictures,
orientations: orientations,
buttons: buttons,
page: req.query.page
});
});
app.get("/view", function(req, res) {
//res.sendFile(__dirname + "/index.html");
var pictures = [];
fs.recurseSync(images_path,
['**/*.JPG', '**/*.PNG', '**/*.JPEG', '**/*.jpg', '**/*.png', '**/*.jpeg'],
function(filepath, relative, filename) {
try {
new ExifImage({ image : filepath }, function (error, exifData) {
if (error)
console.log('Error: '+error.message);
else {
orientations["/images/" + filename] = exifData.image.Orientation || 1;
}
});
} catch (error) {
console.log('Error: ' + error.message);
}
pictures.push("/images/" + filename);
});
res.render("indexview.ejs", {pictures: pictures, orientations: orientations});
});
app.post("/api/upload", function(req, res) {
upload(req, res, function(err) {
if (err) {
console.error(err.stack);
return res.status(500).send("Something went wrong!");
}
else {
return res.end("File Uploaded Successfully!");
}
});
});
app.listen(8090, function(a) {
console.log("Listening to port 8090");
});