feat: add cloud connections and update backups
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import { requireAuth } from '@/lib/auth'
|
||||
import { readConfig } from '@/lib/config'
|
||||
import { getCloudConnection } from '@/lib/appDb'
|
||||
import { createDbDumpBuffer, uploadToDropbox, uploadToGoogleDriveOAuth, uploadToGCS, backupFilename } from '@/lib/backup'
|
||||
import pool from '@/lib/appDb'
|
||||
|
||||
@@ -8,47 +9,56 @@ export async function POST(req: NextRequest) {
|
||||
const authErr = await requireAuth(req)
|
||||
if (authErr) return authErr
|
||||
|
||||
const { db_id, cloud_type, credentials, gdrive_folder_id } = await req.json()
|
||||
|
||||
if (!credentials?.trim()) return NextResponse.json({ ok: false, error: 'Credentials boş olamaz.' })
|
||||
const body = await req.json()
|
||||
const { db_id, connection_id, cloud_type: inlineType, credentials: inlineCredentials, gdrive_folder_id } = body
|
||||
|
||||
const config = await readConfig()
|
||||
const db = config.databases.find(d => d.id === db_id)
|
||||
if (!db) return NextResponse.json({ ok: false, error: 'Veritabanı bulunamadı.' })
|
||||
|
||||
const filename = backupFilename(db.name)
|
||||
// Bağlantı kaynağı: global connection veya inline
|
||||
let type: string, credentials: string, target: string
|
||||
if (connection_id) {
|
||||
const conn = await getCloudConnection(connection_id)
|
||||
if (!conn) return NextResponse.json({ ok: false, error: 'Cloud bağlantısı bulunamadı.' })
|
||||
type = conn.type
|
||||
credentials = conn.credentials
|
||||
target = gdrive_folder_id || conn.default_target || ''
|
||||
} else {
|
||||
if (!inlineCredentials?.trim()) return NextResponse.json({ ok: false, error: 'Credentials boş.' })
|
||||
type = inlineType
|
||||
credentials = inlineCredentials
|
||||
target = gdrive_folder_id || ''
|
||||
}
|
||||
|
||||
const filename = type === 'gdrive' ? backupFilename() : backupFilename(db.name)
|
||||
|
||||
try {
|
||||
const buffer = await createDbDumpBuffer(db)
|
||||
|
||||
if (cloud_type === 'dropbox') {
|
||||
if (type === 'dropbox') {
|
||||
await uploadToDropbox(credentials.trim(), db.name, filename, buffer)
|
||||
} else if (cloud_type === 'gcs') {
|
||||
await uploadToGCS(credentials, gdrive_folder_id, db.name, filename, buffer)
|
||||
} else if (cloud_type === 'gdrive') {
|
||||
await uploadToGoogleDriveOAuth(credentials, filename, buffer, gdrive_folder_id || undefined)
|
||||
} else if (type === 'gcs') {
|
||||
await uploadToGCS(credentials, target, db.name, filename, buffer)
|
||||
} else if (type === 'gdrive') {
|
||||
await uploadToGoogleDriveOAuth(credentials, db.name, filename, buffer, target || undefined)
|
||||
} else {
|
||||
return NextResponse.json({ ok: false, error: 'Desteklenmeyen cloud türü.' })
|
||||
}
|
||||
|
||||
await pool.query(
|
||||
`INSERT INTO backup_logs (db_id, status, message, file_size) VALUES ($1, $2, $3, $4)`,
|
||||
[db_id, 'success', `Manuel yedek — ${cloud_type}`, buffer.length]
|
||||
[db_id, 'success', `Manuel yedek — ${type}`, buffer.length]
|
||||
)
|
||||
|
||||
const sizeStr = buffer.length > 1024 * 1024
|
||||
? `${(buffer.length / (1024 * 1024)).toFixed(2)} MB`
|
||||
: `${(buffer.length / 1024).toFixed(1)} KB`
|
||||
|
||||
return NextResponse.json({
|
||||
ok: true,
|
||||
detail: `✓ Yedek yüklendi — ${db.name}/${filename} (${sizeStr})`,
|
||||
})
|
||||
const pathStr = type === 'gdrive' ? `${db.name}/${filename}` : filename
|
||||
return NextResponse.json({ ok: true, detail: `✓ Yüklendi — ${pathStr} (${sizeStr})` })
|
||||
} catch (e: any) {
|
||||
await pool.query(
|
||||
`INSERT INTO backup_logs (db_id, status, message, file_size) VALUES ($1, $2, $3, $4)`,
|
||||
[db_id, 'error', e.message, 0]
|
||||
).catch(() => {})
|
||||
await pool.query(`INSERT INTO backup_logs (db_id, status, message, file_size) VALUES ($1, $2, $3, $4)`, [db_id, 'error', e.message, 0]).catch(() => {})
|
||||
return NextResponse.json({ ok: false, error: e.message })
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user