feat: migrate local file cache to BunnyCDN storage API

This commit is contained in:
mstfyldz
2026-08-12 10:35:44 +03:00
parent 437f973d6f
commit b86aa54378
+32 -10
View File
@@ -146,17 +146,39 @@ export const cachePrecedentDocument = async (req: Request, res: Response) => {
}
const documentId = String(item.documentId);
const dir = path.join(process.cwd(), 'uploads', 'precedents');
// 1. BunnyCDN'e Yükle
const bunnyZone = process.env.BUNNY_STORAGE_ZONE || '';
const bunnyKey = process.env.BUNNY_STORAGE_API_KEY || '';
const bunnyCdnUrl = process.env.BUNNY_CDN_URL || '';
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
let uploadedToCdn = false;
let finalCdnUrl = '';
if (bunnyZone && bunnyKey) {
try {
const uploadUrl = `https://storage.bunnycdn.com/${bunnyZone}/precedents/${documentId}.txt`;
const response = await fetch(uploadUrl, {
method: 'PUT',
headers: {
'AccessKey': bunnyKey,
'Content-Type': 'text/plain; charset=utf-8'
},
body: content
});
if (response.ok) {
uploadedToCdn = true;
finalCdnUrl = bunnyCdnUrl ? `${bunnyCdnUrl}/precedents/${documentId}.txt` : uploadUrl;
console.log(`[Cache] BunnyCDN'e yüklendi: ${documentId}`);
} else {
console.error(`[Cache] BunnyCDN Yükleme Hatası: ${response.statusText}`);
}
} catch (err) {
console.error('[Cache] BunnyCDN Bağlantı Hatası:', err);
}
} else {
console.warn('[Cache] BunnyCDN ayarları eksik. Sadece DB kaydı yapılacak.');
}
const filePath = path.join(dir, `${documentId}.txt`);
// 1. Txt Olarak Kaydet (CDN / Sunucu depolama)
if (!fs.existsSync(filePath)) {
fs.writeFileSync(filePath, content, 'utf8');
// 2. Supabase DB'ye Metadata Kaydet (Lazy Caching & Meilisearch Indexing)
try {
@@ -170,6 +192,7 @@ export const cachePrecedentDocument = async (req: Request, res: Response) => {
tarih: item.tarih || '',
konu: item.konu || '',
ozet: item.ozet || '',
cdn_url: finalCdnUrl || null,
created_at: new Date().toISOString()
}, { onConflict: 'document_id' });
@@ -181,7 +204,6 @@ export const cachePrecedentDocument = async (req: Request, res: Response) => {
} catch (dbErr) {
console.error('[Cache] Supabase try-catch hatası:', dbErr);
}
}
res.json({ success: true, message: 'Ön belleğe alındı' });
} catch (error: any) {