LogosApi.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. Copyright (C) 2019 Cloudbase Solutions SRL
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Affero General Public License as
  5. published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Affero General Public License for more details.
  11. You should have received a copy of the GNU Affero General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. import express from 'express'
  15. import path from 'path'
  16. import fs from 'fs'
  17. const getModJsonProviders = (jsonPath: string) => {
  18. const jsonContent: any = fs.readFileSync(jsonPath)
  19. const json = JSON.parse(jsonContent)
  20. if (!json.providers) {
  21. throw new Error()
  22. }
  23. return json.providers
  24. }
  25. const getOptimalLogoHeightKey = (
  26. availableHeightKeys: string[],
  27. requestedHeight: number,
  28. style: string | null,
  29. ): string => {
  30. let heightKeys = availableHeightKeys
  31. if (style) {
  32. const styledKeys = heightKeys.filter(k => (style ? k.indexOf(style) > -1 : false))
  33. if (styledKeys.length) {
  34. heightKeys = styledKeys
  35. }
  36. }
  37. const optimal = heightKeys.reduce((prev, curr) => {
  38. let prevHeight: any = /d+/.exec(prev)
  39. let currHeight: any = /d+/.exec(curr)
  40. prevHeight = prevHeight ? Number(prevHeight[0]) : 0
  41. currHeight = currHeight ? Number(currHeight[0]) : 0
  42. return Math.abs(currHeight - requestedHeight)
  43. < Math.abs(prevHeight - requestedHeight) ? curr : prev
  44. })
  45. return optimal
  46. }
  47. export default (router: express.Router) => {
  48. router.get('/logos/:provider/:size/:style?', (req, res) => {
  49. const SIZES = [32, 42, 64, 128]
  50. const STYLES = ['white', 'disabled']
  51. const { provider, style } = req.params
  52. const size = Number(req.params.size)
  53. if (SIZES.indexOf(size) === -1) {
  54. res.status(400).json({ error: { message: `Valid sizes are: ${SIZES.join(', ')}` } })
  55. return
  56. }
  57. if (style && STYLES.indexOf(style) === -1) {
  58. res.status(400).json({ error: { message: `Valid styles are: ${STYLES.join(', ')}` } })
  59. return
  60. }
  61. const logoBase = path.join(__dirname, '/resources/providerLogos')
  62. let logoPath = `${logoBase}/${provider}-${size}`
  63. logoPath = style ? `${logoPath}-${style}.svg` : `${logoPath}.svg`
  64. const modJsonPath: string | null | undefined = process.env.MOD_JSON
  65. if (!modJsonPath) {
  66. res.sendFile(logoPath)
  67. return
  68. }
  69. try {
  70. const providersJson = getModJsonProviders(modJsonPath)
  71. const providerJson = providersJson[provider]
  72. if (!providerJson) {
  73. res.sendFile(logoPath)
  74. return
  75. }
  76. const providerLogosJson = providerJson.logos
  77. if (!providerLogosJson) {
  78. console.log(`No logos specified in MOD_JSON file for '${provider}' provider`)
  79. res.sendFile(logoPath)
  80. return
  81. }
  82. const providerLogosKeys = Object.keys(providerLogosJson)
  83. if (!providerLogosKeys.length) {
  84. console.log(`No logo heights specified in MOD_JSON file for '${provider}' provider`)
  85. res.sendFile(logoPath)
  86. return
  87. }
  88. const optimalHeightKey = getOptimalLogoHeightKey(providerLogosKeys, size, style)
  89. const modLogoPath = providerLogosJson[optimalHeightKey].path
  90. if (!modLogoPath) {
  91. console.log(`No logo path specified in MOD_JSON file for '${provider}' provider`)
  92. res.sendFile(logoPath)
  93. return
  94. }
  95. res.sendFile(modLogoPath)
  96. } catch (err) {
  97. console.error(err)
  98. res.status(400).json({ error: { message: 'Invalid Mod JSON file' } })
  99. }
  100. })
  101. }