projectRouter.js 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. const express = require("express");
  2. const cors = require('cors');
  3. const projectData = require("../data/projectData.js");
  4. const router = express.Router();
  5. /**
  6. * Read (GET) all projects, sorted by priority.
  7. *
  8. * Acccpeted request parameters:
  9. *
  10. * - lang: Two letter language code, case insensitive.. If the data exists in
  11. * the requested language it will be served, otherwise the default
  12. * language will be used.
  13. *
  14. * - images: True, false, or a positive integer. If true, projects will
  15. * include all of their images, sorted by priority. If false, none
  16. * will be included. If a number is provided, up to that many images
  17. * will be provided. Default value is true.
  18. */
  19. router.get("/", cors({origin: '*', methods: 'GET'}), async (req, res) => {
  20. var images = true;
  21. if (req.query.images == "false") images = false;
  22. else if (parseInt(req.query.images) != NaN && parseInt(req.query.images) >= 0)
  23. images = parseInt(req.query.images);
  24. var lang = process.env.DEFAULT_LANGUAGE;
  25. if (
  26. req.query.lang
  27. && process.env.AVAILABLE_LANGUAGES.split(" ").includes(req.query.lang.toLowerCase())
  28. ) lang = req.query.lang.toLowerCase();
  29. var children = true;
  30. if (req.query.children == "false") children = false;
  31. else if (parseInt(req.query.children) != NaN && parseInt(req.query.children) >= 0)
  32. children = parseInt(req.query.children);
  33. const data = await projectData.getProjects(children, lang, images);
  34. res.json(data);
  35. });
  36. // Read (GET) a specific project by ID or permalink
  37. router.get("/:id", cors({origin: '*', methods: 'GET'}), async (req, res) => {
  38. var images = true;
  39. if (req.query.images == "false") images = false;
  40. else if (parseInt(req.query.images) != NaN && parseInt(req.query.images) >= 0)
  41. images = parseInt(req.query.images);
  42. var lang = process.env.DEFAULT_LANGUAGE;
  43. if (
  44. req.query.lang
  45. && process.env.AVAILABLE_LANGUAGES.split(" ").includes(req.query.lang.toLowerCase())
  46. ) lang = req.query.lang.toLowerCase();
  47. var children = true;
  48. if (req.query.children == "false") children = false;
  49. else if (parseInt(req.query.children) != NaN && parseInt(req.query.children) >= 0)
  50. children = parseInt(req.query.children);
  51. const data = await projectData.getProject(req.params.id, children, lang, images);
  52. res.setHeader('Cross-Origin-Resource-Policy', 'cross-origin');
  53. res.setHeader('Access-Control-Allow-Origin', '*');
  54. if (!data || data == "[]") res.status(404).json({ error: "Project not found" });
  55. else res.json(data);
  56. });
  57. // Read (GET) a specific project images by ID or permalink
  58. router.get("/:id/images", cors({origin: '*', methods: 'GET'}), async (req, res) => {
  59. var lang = process.env.DEFAULT_LANGUAGE;
  60. if (
  61. req.query.lang
  62. && process.env.AVAILABLE_LANGUAGES.split(" ").includes(req.query.lang.toLowerCase())
  63. ) lang = req.query.lang.toLowerCase();
  64. var max = null;
  65. if (parseInt(req.query.max) != NaN && parseInt(req.query.max) >= 0)
  66. max = parseInt(req.query.max);
  67. const data = await projectData.getProjectImages(req.params.id, lang, max);
  68. if (!data) res.status(404).json({ error: "Project not found" });
  69. else res.json(data);
  70. });
  71. // Read (GET) a specific project images by ID or permalink
  72. router.get("/:projectId/images/:imageId", cors({origin: '*', methods: 'GET'}), async (req, res) => {
  73. var lang = process.env.DEFAULT_LANGUAGE;
  74. if (
  75. req.query.lang
  76. && process.env.AVAILABLE_LANGUAGES.split(" ").includes(req.query.lang.toLowerCase())
  77. ) lang = req.query.lang.toLowerCase();
  78. const data = await projectData.getProjectImage(req.params.projectId, req.params.imageId, lang);
  79. res.setHeader('Cross-Origin-Resource-Policy', 'cross-origin');
  80. res.setHeader('Access-Control-Allow-Origin', '*');
  81. if (!data) res.status(404).json({ error: "Image not found" });
  82. else{
  83. res.json(data);
  84. }
  85. });
  86. module.exports = router;