generated from azures04/Base-REST-API
MVP
This commit is contained in:
@@ -3,7 +3,16 @@ const path = require("node:path")
|
||||
const crypto = require("node:crypto")
|
||||
const { DefaultError } = require("../errors/errors")
|
||||
const { pipeline } = require("node:stream/promises")
|
||||
const gameDataPath = path.join(process.cwd(), "data", "products")
|
||||
const productsDataPath = path.join(process.cwd(), "data", "products")
|
||||
|
||||
fs.promises.exists = async function exists(path) {
|
||||
try {
|
||||
await fs.promises.access(path)
|
||||
return true
|
||||
} catch {
|
||||
return false
|
||||
}
|
||||
};
|
||||
|
||||
function normalizePath($path) {
|
||||
return $path.split(path.win32.sep).join(path.posix.sep)
|
||||
@@ -21,13 +30,15 @@ async function getFileSha1(filePath) {
|
||||
}
|
||||
}
|
||||
|
||||
async function getGameFiles() {
|
||||
const files = await fs.promises.readdir(gameDataPath, { recursive: true })
|
||||
const gameFilesIndex = { root: [] }
|
||||
async function getProductFiles(productName, platform) {
|
||||
const finalPath = path.join(productsDataPath, productName, platform)
|
||||
const files = await fs.promises.readdir(finalPath, { recursive: true })
|
||||
const productFilesIndex = { root: [] }
|
||||
for (const file of files) {
|
||||
const filePath = path.join(gameDataPath, file)
|
||||
const filePath = path.join(finalPath, file)
|
||||
const fileMetadata = await fs.promises.stat(filePath)
|
||||
if (!fileMetadata.isDirectory()) {
|
||||
|
||||
if (!fileMetadata.isDirectory() && !filePath.endsWith(".brikcfg")) {
|
||||
const normalizedFilePath = normalizePath(file)
|
||||
const artifact = {
|
||||
path: normalizedFilePath,
|
||||
@@ -41,15 +52,15 @@ async function getGameFiles() {
|
||||
},
|
||||
name: path.parse(normalizedFilePath).base
|
||||
}
|
||||
gameFilesIndex.root.push(downloadObject)
|
||||
productFilesIndex.root.push(downloadObject)
|
||||
}
|
||||
}
|
||||
return gameFilesIndex
|
||||
return productFilesIndex
|
||||
}
|
||||
|
||||
async function getFile(basePath) {
|
||||
async function getFile(basePath, productName, productPlatform) {
|
||||
try {
|
||||
const fixedPath = path.join(gameDataPath, decodeURI(basePath))
|
||||
const fixedPath = path.join(productsDataPath, productName, productPlatform, decodeURI(basePath))
|
||||
const fileMetadata = await fs.promises.stat(fixedPath)
|
||||
if (fileMetadata.isDirectory()) {
|
||||
throw new DefaultError(409, "Can't download a directory", "", "NotDownloadableException")
|
||||
@@ -65,8 +76,31 @@ async function getFile(basePath) {
|
||||
}
|
||||
}
|
||||
|
||||
async function canAccess(productName, productPlatform) {
|
||||
const fixedPath = path.join(productsDataPath, productName, productPlatform, ".brikcfg")
|
||||
const fileState = await fs.promises.exists(fixedPath, fs.constants.F_OK)
|
||||
if (!fileState) {
|
||||
return { code: 200 }
|
||||
}
|
||||
|
||||
try {
|
||||
const brikConfig = JSON.parse(await fs.promises.readFile(fixedPath))
|
||||
if (!brikConfig.canAccess) {
|
||||
throw new DefaultError(403, "Forbidden", "Locked resource.", "InsuffisentPrivilegeException")
|
||||
}
|
||||
return { code: 200 }
|
||||
} catch (error) {
|
||||
if (!error instanceof DefaultError) {
|
||||
throw new DefaultError(500, "Please contact the maintener", "CONFIGURATION", "0x00")
|
||||
}
|
||||
throw error
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getFile,
|
||||
canAccess,
|
||||
getFileSha1,
|
||||
getGameFiles
|
||||
getProductFiles
|
||||
}
|
||||
Reference in New Issue
Block a user