feat: Enhance card metadata handling, implement persistent Scryfall caching, and update pack generation logic for new booster structure.
Some checks failed
Build and Deploy / build (push) Failing after 55s

This commit is contained in:
2025-12-16 22:51:21 +01:00
parent e0d2424cba
commit ea24b5a206
7 changed files with 81 additions and 11 deletions

View File

@@ -28,7 +28,7 @@ const PORT = process.env.PORT || 3000;
app.use(express.json({ limit: '50mb' })); // Increase limit for large card lists
// Serve static images
// Serve static images (Nested)
app.use('/cards', express.static(path.join(__dirname, 'public/cards')));
// API Routes
@@ -51,9 +51,10 @@ app.post('/api/cards/cache', async (req: Request, res: Response) => {
return;
}
console.log(`Caching images for ${cards.length} cards...`);
const count = await cardService.cacheImages(cards);
res.json({ success: true, downloaded: count });
console.log(`Caching images and metadata for ${cards.length} cards...`);
const imgCount = await cardService.cacheImages(cards);
const metaCount = await cardService.cacheMetadata(cards);
res.json({ success: true, downloadedImages: imgCount, savedMetadata: metaCount });
} catch (err: any) {
console.error('Error in cache route:', err);
res.status(500).json({ error: err.message });

View File

@@ -8,9 +8,18 @@ const __dirname = path.dirname(__filename);
const CARDS_DIR = path.join(__dirname, '../public/cards');
export class CardService {
private imagesDir: string;
private metadataDir: string;
constructor() {
if (!fs.existsSync(CARDS_DIR)) {
fs.mkdirSync(CARDS_DIR, { recursive: true });
this.imagesDir = path.join(CARDS_DIR, 'images');
this.metadataDir = path.join(CARDS_DIR, 'metadata');
if (!fs.existsSync(this.imagesDir)) {
fs.mkdirSync(this.imagesDir, { recursive: true });
}
if (!fs.existsSync(this.metadataDir)) {
fs.mkdirSync(this.metadataDir, { recursive: true });
}
}
@@ -38,7 +47,7 @@ export class CardService {
if (!imageUrl) continue;
const filePath = path.join(CARDS_DIR, `${uuid}.jpg`);
const filePath = path.join(this.imagesDir, `${uuid}.jpg`);
if (fs.existsSync(filePath)) {
// Already cached
@@ -67,4 +76,21 @@ export class CardService {
return downloadedCount;
}
async cacheMetadata(cards: any[]): Promise<number> {
let cachedCount = 0;
for (const card of cards) {
if (!card.id) continue;
const filePath = path.join(this.metadataDir, `${card.id}.json`);
if (!fs.existsSync(filePath)) {
try {
fs.writeFileSync(filePath, JSON.stringify(card, null, 2));
cachedCount++;
} catch (e) {
console.error(`Failed to save metadata for ${card.id}`, e);
}
}
}
return cachedCount;
}
}