api and image proxy

This commit is contained in:
Russell2259
2024-01-03 18:46:27 +00:00
parent 44cfda5666
commit b2ef5a6fd4
6 changed files with 229 additions and 56 deletions
+169
View File
@@ -23,6 +23,175 @@ const swPaths = [
'/assets/js/offline.js'
];
app.get('/api/games', (req, res) => {
/**
* @type {Array.<{name: string, target: string, image: string, popular: boolean}>}
*/
const games = JSON.parse(fs.readFileSync(path.join(__dirname, '../static/assets/JSON/games.json')));
const gamesObject = {
popular: [],
all: []
};
for (let i = 0; i < games.length; i++) {
const game = games[i];
if (game.popular) gamesObject.popular.push({
name: game.name,
target: game.target,
image: `/api/games/${i + 1}/image`
});
gamesObject.all.push({
name: game.name,
target: game.target,
image: `/api/games/${i + 1}/image`
});
}
res.json(gamesObject);
});
app.get('/api/games/:id', async (req, res, next) => {
/**
* @type {Array.<{name: string, target: string, image: string, popular: boolean}>}
*/
const games = JSON.parse(fs.readFileSync(path.join(__dirname, '../static/assets/JSON/games.json')));
if (games[req.params.id - 1]) {
const game = games[req.params.id - 1];
game.image = `/api/games/${req.params.id}/image`;
game.popular = Boolean(game.popular);
res.json(game);
} else next();
});
app.get('/api/games/:id/image', async (req, res, next) => {
/**
* @type {Array.<{name: string, target: string, image: string, popular: boolean}>}
*/
const games = JSON.parse(fs.readFileSync(path.join(__dirname, '../static/assets/JSON/games.json')));
if (games[req.params.id - 1]) {
if (URL.canParse(games[req.params.id - 1].image)) {
const request = await fetch(games[req.params.id - 1].image);
const imageBuffer = Buffer.from(await request.arrayBuffer());
res.setHeader('content-type', request.headers.get('content-type'));
res.end(imageBuffer);
} else res.redirect(games[req.params.id - 1].image);
} else next();
});
app.get('/api/apps', (req, res) => {
/**
* @type {Array.<{name: string, target: string, image: string, popular: boolean}>}
*/
const apps = JSON.parse(fs.readFileSync(path.join(__dirname, '../static/assets/JSON/apps.json')));
const appsObject = [];
for (let i = 0; i < apps.length; i++) {
const app = apps[i];
appsObject.push({
name: app.name,
target: app.target,
image: `/api/apps/${i + 1}/image`
});
}
res.json(appsObject);
});
app.get('/api/apps/:id', async (req, res, next) => {
/**
* @type {Array.<{name: string, target: string, image: string, popular: boolean}>}
*/
const apps = JSON.parse(fs.readFileSync(path.join(__dirname, '../static/assets/JSON/apps.json')));
if (apps[req.params.id - 1]) {
const app = apps[req.params.id - 1];
app.image = `/api/apps/${req.params.id}/image`;
res.json(app);
} else next();
});
app.get('/api/apps/:id/image', async (req, res, next) => {
/**
* @type {Array.<{name: string, target: string, image: string, popular: boolean}>}
*/
const apps = JSON.parse(fs.readFileSync(path.join(__dirname, '../static/assets/JSON/apps.json')));
if (apps[req.params.id - 1]) {
if (URL.canParse(apps[req.params.id - 1].image)) {
const request = await fetch(apps[req.params.id - 1].image);
const imageBuffer = Buffer.from(await request.arrayBuffer());
res.setHeader('content-type', request.headers.get('content-type'));
res.end(imageBuffer);
} else res.redirect(apps[req.params.id - 1].image);
} else next();
});
app.get('/api/cheats', (req, res) => {
/**
* @type {Array.<{name: string, target: string, image: string, popular: boolean}>}
*/
const cheats = JSON.parse(fs.readFileSync(path.join(__dirname, '../static/assets/JSON/cheats.json')));
const cheatsObject = [];
for (let i = 0; i < cheats.length; i++) {
const cheat = cheats[i];
cheatsObject.push({
name: cheat.name,
target: cheat.target,
image: `/api/cheats/${i + 1}/image`
});
}
res.json(cheatsObject);
});
app.get('/api/cheats/:id', async (req, res, next) => {
/**
* @type {Array.<{name: string, target: string, image: string, popular: boolean}>}
*/
const cheats = JSON.parse(fs.readFileSync(path.join(__dirname, '../static/assets/JSON/cheats.json')));
if (cheats[req.params.id - 1]) {
const cheat = cheats[req.params.id - 1];
cheat.image = `/api/cheats/${req.params.id}/image`;
res.json(cheat);
} else next();
});
app.get('/api/cheats/:id/image', async (req, res, next) => {
/**
* @type {Array.<{name: string, target: string, image: string, popular: boolean}>}
*/
const cheats = JSON.parse(fs.readFileSync(path.join(__dirname, '../static/assets/JSON/cheats.json')));
if (cheats[req.params.id - 1]) {
if (URL.canParse(cheats[req.params.id - 1].image)) {
const request = await fetch(cheats[req.params.id - 1].image);
const imageBuffer = Buffer.from(await request.arrayBuffer());
res.setHeader('content-type', request.headers.get('content-type'));
res.end(imageBuffer);
} else res.redirect(cheats[req.params.id - 1].image);
} else next();
});
app.get('/cdn/3kh0/*', cors({
origin: false
}), async (req, res, next) => {