feat: migrate local file cache to BunnyCDN storage API
This commit is contained in:
@@ -146,41 +146,63 @@ export const cachePrecedentDocument = async (req: Request, res: Response) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const documentId = String(item.documentId);
|
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)) {
|
let uploadedToCdn = false;
|
||||||
fs.mkdirSync(dir, { recursive: true });
|
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`);
|
// 2. Supabase DB'ye Metadata Kaydet (Lazy Caching & Meilisearch Indexing)
|
||||||
|
try {
|
||||||
// 1. Txt Olarak Kaydet (CDN / Sunucu depolama)
|
const { error } = await supabase
|
||||||
if (!fs.existsSync(filePath)) {
|
.from('cached_precedents')
|
||||||
fs.writeFileSync(filePath, content, 'utf8');
|
.upsert({
|
||||||
|
document_id: documentId,
|
||||||
// 2. Supabase DB'ye Metadata Kaydet (Lazy Caching & Meilisearch Indexing)
|
mahkeme: item.mahkeme || '',
|
||||||
try {
|
karar_no: item.karar_no || '',
|
||||||
const { error } = await supabase
|
esas_no: item.esas_no || '',
|
||||||
.from('cached_precedents')
|
tarih: item.tarih || '',
|
||||||
.upsert({
|
konu: item.konu || '',
|
||||||
document_id: documentId,
|
ozet: item.ozet || '',
|
||||||
mahkeme: item.mahkeme || '',
|
cdn_url: finalCdnUrl || null,
|
||||||
karar_no: item.karar_no || '',
|
created_at: new Date().toISOString()
|
||||||
esas_no: item.esas_no || '',
|
}, { onConflict: 'document_id' });
|
||||||
tarih: item.tarih || '',
|
|
||||||
konu: item.konu || '',
|
if (error) {
|
||||||
ozet: item.ozet || '',
|
console.error('[Cache] Supabase kayıt hatası:', error.message);
|
||||||
created_at: new Date().toISOString()
|
} else {
|
||||||
}, { onConflict: 'document_id' });
|
console.log(`[Cache] Başarıyla kaydedildi: ${documentId}`);
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
} catch (dbErr) {
|
||||||
|
console.error('[Cache] Supabase try-catch hatası:', dbErr);
|
||||||
}
|
}
|
||||||
|
|
||||||
res.json({ success: true, message: 'Ön belleğe alındı' });
|
res.json({ success: true, message: 'Ön belleğe alındı' });
|
||||||
|
|||||||
Reference in New Issue
Block a user