-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathLogosApi.ts
More file actions
127 lines (119 loc) · 3.91 KB
/
Copy pathLogosApi.ts
File metadata and controls
127 lines (119 loc) · 3.91 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
/*
Copyright (C) 2019 Cloudbase Solutions SRL
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import express from "express";
import path from "path";
import fs from "fs";
const getModJsonProviders = (jsonPath: string) => {
const jsonContent: any = fs.readFileSync(jsonPath);
const json = JSON.parse(jsonContent);
if (!json.providers) {
throw new Error();
}
return json.providers;
};
const getOptimalLogoHeightKey = (
availableHeightKeys: string[],
requestedHeight: number,
style?: string | null,
): string => {
let heightKeys = availableHeightKeys;
if (style) {
const styledKeys = heightKeys.filter(k =>
style ? k.indexOf(style) > -1 : false,
);
if (styledKeys.length) {
heightKeys = styledKeys;
}
}
const optimal = heightKeys.reduce((prev, curr) => {
let prevHeight: any = /\d+/.exec(prev);
let currHeight: any = /\d+/.exec(curr);
prevHeight = prevHeight ? Number(prevHeight[0]) : 0;
currHeight = currHeight ? Number(currHeight[0]) : 0;
return Math.abs(currHeight - requestedHeight) <
Math.abs(prevHeight - requestedHeight)
? curr
: prev;
});
return optimal;
};
export default (router: express.Router) => {
router.get("/logos/:provider/:size{/:style}", (req, res) => {
const SIZES = [32, 42, 64, 128];
const STYLES = ["white", "disabled"];
const { provider, style } = req.params;
const size = Number(req.params.size);
if (SIZES.indexOf(size) === -1) {
res
.status(400)
.json({ error: { message: `Valid sizes are: ${SIZES.join(", ")}` } });
return;
}
if (style && STYLES.indexOf(style) === -1) {
res
.status(400)
.json({ error: { message: `Valid styles are: ${STYLES.join(", ")}` } });
return;
}
const logoBase = path.join(__dirname, "/resources/providerLogos");
let logoPath = `${logoBase}/${provider}-${size}`;
logoPath = style ? `${logoPath}-${style}.svg` : `${logoPath}.svg`;
const modJsonPath: string | null | undefined = process.env.MOD_JSON;
if (!modJsonPath) {
res.sendFile(logoPath);
return;
}
try {
const providersJson = getModJsonProviders(modJsonPath);
const providerJson = providersJson[provider];
if (!providerJson) {
res.sendFile(logoPath);
return;
}
const providerLogosJson = providerJson.logos;
if (!providerLogosJson) {
console.log(
`No logos specified in MOD_JSON file for '${provider}' provider`,
);
res.sendFile(logoPath);
return;
}
const providerLogosKeys = Object.keys(providerLogosJson);
if (!providerLogosKeys.length) {
console.log(
`No logo heights specified in MOD_JSON file for '${provider}' provider`,
);
res.sendFile(logoPath);
return;
}
const optimalHeightKey = getOptimalLogoHeightKey(
providerLogosKeys,
size,
style,
);
const modLogoPath = providerLogosJson[optimalHeightKey].path;
if (!modLogoPath) {
console.log(
`No logo path specified in MOD_JSON file for '${provider}' provider`,
);
res.sendFile(logoPath);
return;
}
res.sendFile(modLogoPath);
} catch (err) {
console.error(err);
res.status(400).json({ error: { message: "Invalid Mod JSON file" } });
}
});
};