-
Notifications
You must be signed in to change notification settings - Fork 2
/
routes.js
93 lines (78 loc) · 2.39 KB
/
routes.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
const fs = require('fs');
const mime = require('mime-types');
const indexOf = require('lodash.indexof');
const site = 'https://github.com/wgenial/mimetypeicons';
const icons_path = '/mimetypes-icons';
const icons_manifest = 'FileTypeIcons.json';
const icons_directory_names = ['directory', 'dir', 'folder', 'text/directory', 'application/folder'];
const icons_size_default = '32';
let icons_manifest_json = null;
module.exports = {
// default, redirect to project page
home: (req, res) => {
return res.redirect(site);
},
// load icon and redirect
icon: (req, res) => {
let defaultMime,
icon,
iconUrl,
iconname,
mimetype,
size,
icon_extension,
status_code = 301,
icon_found = true,
size_found = true;
// get icon
icon = req.params.icon;
// set icon size
size = req.query.size != null ? req.query.size : icons_size_default;
// set default mime type
defaultMime = req.query['default'] != null ? req.query['default'] : '.bin';
mime.default_type = mime.lookup(defaultMime);
// set icon name
if (indexOf(icons_directory_names, icon) >= 0) {
iconname = 'text-directory';
} else {
mimetype = mime.lookup(icon);
if (mimetype == false || indexOf(icons_manifest_json.Names, mimetype.replace('/', '-')) == -1) {
mimetype = mime.default_type;
iconname = mimetype.replace('/', '-');
icon_found = false;
} else {
iconname = mimetype.replace('/', '-');
}
}
// verify is size received exists in default sizes (.json)
if (indexOf(icons_manifest_json.Info.Sizes, size) == -1) {
size = icons_size_default;
size_found = false;
}
// icon extension
if (size == 'scalable') {
icon_extension = '.svg';
} else {
icon_extension = '.png';
}
// if icon not found, set status code 302
if (!icon_found) {
status_code = 302;
} else if (icon_found && !size_found) {
status_code = 302;
}
// uri to redirect
iconUrl = '' + icons_path +'/'+ size + '/' + iconname + icon_extension;
return res.redirect(status_code, iconUrl);
},
// load json manifest
init: () => {
fs.readFile(icons_manifest, 'utf8', (err, data) => {
if (err) {
console.log('Error: ' + err);
return;
}
icons_manifest_json = JSON.parse(data);
});
}
};