-
Notifications
You must be signed in to change notification settings - Fork 0
/
express.js
188 lines (164 loc) · 6.49 KB
/
express.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
(function () {
var express = require('express');
//var bodyParser = require('body-parser')
var app = module.exports = express();
var fs = require('fs');
var exif = require('exiftool');
var bodyParser = require('body-parser');
var formidable = require('formidable');
app.use(bodyParser.json()); // to support JSON-encoded bodies
app.set('views', __dirname + '/views');
app.engine('html', require('ejs').renderFile);
app.set('view options', {
layout: false
});
app.use(express.static(__dirname + '/app'));
app.get('/', function (req, res) {
res.render('index.html');
});
app.get('/partials/:name', function (req, res) {
var name = req.params.name;
res.render('partials/' + name);
});
var getExifMetadata = function (path, callback) {
//console.log('filepath: ' + path);
fs.readFile(path, function (err, data) {
if (err)
throw err;
else {
exif.metadata(data, callback)
}
});
};
var xxx = function (filenames, nonFilenames, totalFiles, res) {
return function () {
if (filenames.length + nonFilenames.length >= totalFiles) {
console.log("finally done");
res.status(200).send(filenames);
} else {
console.log("looping: " + filenames.length + ":" + nonFilenames.length + ":" + totalFiles);
setTimeout(xxx(filenames, nonFilenames, totalFiles, res), 500);
}
}
};
app.get('/contest', function (req, res) {
var rootFolder = req.query.rootFolder;
var contestName = req.query.name;
var dirPath = rootFolder + "/" + contestName + "/";
console.log("dirPath: " + dirPath);
try {
if (fs.existsSync(dirPath)) {
var contestContent = fs.readdirSync(dirPath);
getAndReturnFileInfo(contestContent, dirPath, res);
} else {
res.status(500).send("could not find path: " + dirPath);
}
} catch (exc) {
res.status(500).send("could not find path: " + dirPath + ": " + exc.message);
}
});
function getAndReturnFileInfo(contestContent, dirPath, res){
var filenames = [];
var nonFilenames = [];
console.log("files: " + contestContent);
contestContent.forEach(function (filename) {
var filenameSplit = filename.split(".");
if (filenameSplit.length === 2 && (filenameSplit[1] === "jpg" || filenameSplit[1] === "JPG" || filenameSplit[1] === "jpeg" || filenameSplit[1] === "JPEG")) {
var filePath = dirPath + filename;
var fsStats = fs.statSync(filePath);
if (fsStats.isFile()) {
try {
getExifMetadata(filePath, function (err, metadata) {
if (err) {
nonFilenames.push(err);
} else {
//-imagesize -iptc:CopyrightNotice -iptc:caption-abstract -xmp:title -DateTimeOriginal -FileSize
// console.log(metadata);
filenames.push({
filename: filename,
imageWidth: metadata.imageWidth,
imageHeight: metadata.imageHeight,
copyrightNotice: metadata.copyrightNotice,
dateCreated: metadata['date/timeCreated'],
fileSize: fsStats.size
});
}
});
} catch (ex){
console.log("done ex: " + ex.message);
console.log("done ex: " + ex.stack);
}
}
} else {
nonFilenames.push(filename);
}
});
setTimeout(xxx(filenames, nonFilenames, contestContent.length, res), 500);
}
app.get('*', function (req, res) {
// console.log("*: " + req.originalUrl);
res.redirect('/');
});
app.post('/addFiles', function (req, res) {
var form = new formidable.IncomingForm(),
rootFolder, contestName,
files = [],
filenames = [];
form.uploadDir = __dirname + '/uploads';
form.on('field', function(field, value) {
if (field === "rootFolder"){
rootFolder = value;
} else {
contestName = value;
}
});
form.on('file', function(field, file) {
console.log(file.name);
files.push([field, file]);
});
form.on('end', function() {
console.log('done');
var dirPath = rootFolder + "/" + contestName + "/";
files.forEach(function (entry){
var file = entry[1];
var fn = file.name;
filenames.push(fn);
fs.renameSync(file.path, dirPath + fn);
});
getAndReturnFileInfo(filenames, dirPath, res);
});
form.parse(req);
});
app.post('/getContests', function (req, res) {
var rootFolder = req.body.rootFolder;
try {
if (!fs.existsSync(rootFolder)) {
fs.mkdir(rootFolder);
}
var rootFolderContent = fs.readdirSync(rootFolder);
res.status(200).send(rootFolderContent);
} catch (exc) {
res.status(500).send("could not read contents from rootFolder: " + rootFolder + ": " + exc.message);
}
});
app.post('/createContest', function (req, res) {
var rootFolder = req.body.rootFolder;
var contestName = req.body.name;
var dirPath = rootFolder + "/" + contestName + "/";
var contestContent;
try {
if (fs.existsSync(dirPath)) {
contestContent = fs.readdirSync(dirPath);
} else {
fs.mkdirSync(dirPath);
contestContent = [];
}
res.status(200).send(contestContent);
} catch (exc) {
res.status(500).send("could not find path: " + dirPath + ": " + exc.message);
}
});
var server = app.listen(3000, function () {
console.log("Express server listening on port %d", server.address().port);
});
}());