ps2_artwork_api/api.js
2025-04-22 20:35:28 +02:00

60 lines
1.6 KiB
JavaScript

const express = require('express');
const fs = require('fs');
const path = require('path');
const https = require('https');
const app = express();
const os = require('os');
const PORT = 7722;
const data = fs.readFileSync('db/PS2_LIST.txt', 'utf-8');
const gameMap = {};
const sslOptions = {
key: fs.readFileSync(path.join(os.homedir(), 'certs', 'privkey.pem')),
cert: fs.readFileSync(path.join(os.homedir(), 'certs', 'cert.pem')),
ca: fs.readFileSync(path.join(os.homedir(), 'certs', 'fullchain.pem'))
};
data.split('\n').forEach(line => {
const match = line.match(/^(\S+)\s+(.*)$/);
if (match) {
const [_, id, title] = match;
gameMap[id] = title.trim();
}
});
function normalizeId(input) {
const cleaned = input.toUpperCase().replace(/[_\.]/g, ''); // remove _ and .
const match = cleaned.match(/^([A-Z]+)(\d+)$/);
if (!match) return input.toUpperCase(); // fallback
const [, prefix, digits] = match;
return `${prefix}-${digits}`;
}
app.get('/search/:id', (req, res) => {
const normalizedId = normalizeId(req.params.id);
const title = gameMap[normalizedId];
if (title) {
res.json({ id: normalizedId, title });
} else {
res.status(404).json({ error: 'Game ID not found' });
}
});
app.use('/art', express.static(path.join(__dirname, 'db/PS2')));
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'index.html'));
});
app.get('/contact', (req, res) => {
res.sendFile(path.join(__dirname, 'contact.html'));
});
https.createServer(sslOptions, app).listen(PORT, () => {
console.log(`Server running at https://localhost:${PORT}`);
});