LogosApi.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. // @flow
  15. import path from 'path'
  16. import fs from 'fs'
  17. const getModJsonProviders = (jsonPath: string) => {
  18. let jsonContent = fs.readFileSync(jsonPath)
  19. let 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
  29. ): string => {
  30. let heightKeys = availableHeightKeys
  31. if (style) {
  32. let styledKeys = heightKeys.filter(k => style ? k.indexOf(style) > -1 : false)
  33. if (styledKeys.length) {
  34. heightKeys = styledKeys
  35. }
  36. }
  37. let optimal = heightKeys.reduce((prev, curr) => {
  38. let prevHeight = /d+/.exec(prev)
  39. let currHeight = /d+/.exec(curr)
  40. prevHeight = prevHeight ? Number(prevHeight[0]) : 0
  41. currHeight = currHeight ? Number(currHeight[0]) : 0
  42. return Math.abs(currHeight - requestedHeight) < Math.abs(prevHeight - requestedHeight) ? curr : prev
  43. })
  44. return optimal
  45. }
  46. export default (router: express$Router) => {
  47. // $FlowIgnore
  48. router.get('/logos/:provider/:size/:style?', (req, res) => {
  49. const SIZES = [32, 42, 64, 128]
  50. const STYLES = ['white', 'disabled']
  51. let { provider, size, style } = req.params
  52. size = Number(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. let logoBase = path.join(__dirname, '/resources/providerLogos')
  62. let logoPath = `${logoBase}/${provider}-${size}`
  63. logoPath = style ? `${logoPath}-${style}.svg` : `${logoPath}.svg`
  64. let modJsonPath: ?string = process.env.MOD_JSON
  65. if (!modJsonPath) {
  66. res.sendFile(logoPath)
  67. return
  68. }
  69. try {
  70. let providersJson = getModJsonProviders(modJsonPath)
  71. let providerJson = providersJson[provider]
  72. if (!providerJson) {
  73. res.sendFile(logoPath)
  74. return
  75. }
  76. let 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. let 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. let optimalHeightKey = getOptimalLogoHeightKey(providerLogosKeys, size, style)
  89. let 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. }