diff --git a/src/controllers/precedent.controller.ts b/src/controllers/precedent.controller.ts index 5c1a7b5..0574dfa 100644 --- a/src/controllers/precedent.controller.ts +++ b/src/controllers/precedent.controller.ts @@ -146,41 +146,63 @@ 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 { - const { error } = await supabase - .from('cached_precedents') - .upsert({ - document_id: documentId, - mahkeme: item.mahkeme || '', - karar_no: item.karar_no || '', - esas_no: item.esas_no || '', - tarih: item.tarih || '', - konu: item.konu || '', - ozet: item.ozet || '', - created_at: new Date().toISOString() - }, { onConflict: 'document_id' }); - - if (error) { - console.error('[Cache] Supabase kayıt hatası:', error.message); - } else { - console.log(`[Cache] Başarıyla kaydedildi: ${documentId}`); - } - } catch (dbErr) { - console.error('[Cache] Supabase try-catch hatası:', dbErr); + // 2. Supabase DB'ye Metadata Kaydet (Lazy Caching & Meilisearch Indexing) + try { + const { error } = await supabase + .from('cached_precedents') + .upsert({ + document_id: documentId, + mahkeme: item.mahkeme || '', + karar_no: item.karar_no || '', + esas_no: item.esas_no || '', + tarih: item.tarih || '', + konu: item.konu || '', + ozet: item.ozet || '', + cdn_url: finalCdnUrl || null, + created_at: new Date().toISOString() + }, { onConflict: 'document_id' }); + + if (error) { + console.error('[Cache] Supabase kayıt hatası:', error.message); + } else { + console.log(`[Cache] Başarıyla kaydedildi: ${documentId}`); } + } catch (dbErr) { + console.error('[Cache] Supabase try-catch hatası:', dbErr); } res.json({ success: true, message: 'Ön belleğe alındı' });