Files
backerup-website/frontend/lib/store.ts
T
Anders Böttcher 33e339bde6 Refactor database interactions and update footer text
- Removed community sharing and self-hosted features from the features list in page.tsx.
- Updated footer text in layout.tsx to remove the MIT License mention.
- Added @libsql/client dependency to package.json and bun.lock for database management.
- Implemented a new db.ts file to handle SQLite database connections and migrations.
- Refactored user and file management functions in users.ts and store.ts to utilize the new database client.
- Ensured legacy JSON data is imported into the new SQLite database structure on first run.
2026-05-12 16:35:10 +02:00

95 lines
2.7 KiB
TypeScript

import path from 'path'
import fs from 'fs/promises'
import { getDb, ensureReady } from './db'
export type FileType = 'backerup' | 'config'
export interface FileRecord {
token: string
originalName: string
displayName?: string
size: number
type: FileType
uploadedAt: string
description?: string
group?: string
containerName?: string
image?: string
}
const DATA_DIR = path.join(process.cwd(), 'data')
const UPLOADS_DIR = path.join(DATA_DIR, 'uploads')
type DbRow = Record<string, unknown>
function rowToRecord(row: DbRow): FileRecord {
return {
token: row.token as string,
originalName: row.original_name as string,
displayName: (row.display_name as string | null) ?? undefined,
size: row.size as number,
type: row.type as FileType,
uploadedAt: row.uploaded_at as string,
description: (row.description as string | null) ?? undefined,
group: (row.group_name as string | null) ?? undefined,
containerName: (row.container_name as string | null) ?? undefined,
image: (row.image as string | null) ?? undefined,
}
}
export async function readMeta(): Promise<FileRecord[]> {
await ensureReady()
const { rows } = await getDb().execute(
'SELECT * FROM files ORDER BY uploaded_at DESC',
)
return rows.map(rowToRecord)
}
export async function listGroups(): Promise<string[]> {
await ensureReady()
const { rows } = await getDb().execute(
'SELECT DISTINCT group_name FROM files WHERE group_name IS NOT NULL ORDER BY group_name',
)
return rows.map((r) => r.group_name as string)
}
export async function addFile(record: FileRecord, buffer: Buffer): Promise<void> {
await ensureReady()
await fs.mkdir(UPLOADS_DIR, { recursive: true })
const dir = path.join(UPLOADS_DIR, record.token)
await fs.mkdir(dir, { recursive: true })
await fs.writeFile(path.join(dir, record.originalName), buffer)
await getDb().execute({
sql: `INSERT INTO files
(token,original_name,display_name,size,type,uploaded_at,description,group_name,container_name,image)
VALUES (?,?,?,?,?,?,?,?,?,?)`,
args: [
record.token,
record.originalName,
record.displayName ?? null,
record.size,
record.type,
record.uploadedAt,
record.description ?? null,
record.group ?? null,
record.containerName ?? null,
record.image ?? null,
],
})
}
export async function getFile(token: string): Promise<FileRecord | undefined> {
await ensureReady()
const { rows } = await getDb().execute({
sql: 'SELECT * FROM files WHERE token = ?',
args: [token],
})
return rows[0] ? rowToRecord(rows[0]) : undefined
}
export function getUploadPath(token: string, originalName: string): string {
return path.join(UPLOADS_DIR, token, originalName)
}