From 4e70d59c6325654f3af2d91934d4daf6b10cff24 Mon Sep 17 00:00:00 2001 From: Azure Date: Wed, 28 Jan 2026 00:14:13 +0100 Subject: [PATCH] Added product route Added a single product route to get metadata and products list --- routes/products/index.js | 15 +++++++++++++++ services/productsFileService.js | 30 +++++++++++++++++++++++++++++- 2 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 routes/products/index.js diff --git a/routes/products/index.js b/routes/products/index.js new file mode 100644 index 0000000..048e05c --- /dev/null +++ b/routes/products/index.js @@ -0,0 +1,15 @@ +const express = require("express") +const router = express.Router({ mergeParams: false }) +const productsFileService = require("../../services/productsFileService") + +router.get("/", async (req, res) => { + const products = await productsFileService.getProducts() + return res.status(200).json(products) +}) + +router.get("/product/:productName", async (req, res) => { + const product = await productsFileService.getProduct(req.params.productName) + return res.status(200).json(product) +}) + +module.exports = router \ No newline at end of file diff --git a/services/productsFileService.js b/services/productsFileService.js index 92a16b9..a5fc0e6 100644 --- a/services/productsFileService.js +++ b/services/productsFileService.js @@ -85,7 +85,7 @@ async function canAccess(productName, productPlatform) { try { const brikConfig = JSON.parse(await fs.promises.readFile(fixedPath)) - if (!brikConfig.canAccess) { + if (!brikConfig.public) { throw new DefaultError(403, "Forbidden", "Locked resource.", "InsuffisentPrivilegeException") } return { code: 200 } @@ -98,9 +98,37 @@ async function canAccess(productName, productPlatform) { } +async function getProduct(productName) { + const fixedPath = path.join(productsDataPath, productName, ".brikpkg") + const fileState = await fs.promises.exists(fixedPath, fs.constants.F_OK) + if (!fileState) { + throw new DefaultError(404, "Not found", "MissingDir", "NotFoundException") + } + + try { + const brikPackage = JSON.parse(await fs.promises.readFile(fixedPath)) + if (!brikPackage.public) { + throw new DefaultError(403, "Forbidden", "Locked resource.", "InsuffisentPrivilegeException") + } + return brikPackage + } catch (error) { + if (!error instanceof DefaultError) { + throw new DefaultError(500, "Please contact the maintener", "CONFIGURATION", "0x00") + } + throw error + } +} + +async function getProducts() { + const files = await fs.promises.readdir(path.join(productsDataPath), { recursive: false }) + return files +} + module.exports = { getFile, canAccess, + getProduct, getFileSha1, + getProducts, getProductFiles } \ No newline at end of file