mirror of
https://github.com/marcogll/TaxHacker_s23.git
synced 2026-01-13 13:25:18 +00:00
(squash) init
feat: filters, settings, backups fix: ts compile errors feat: new dashboard, webp previews and settings feat: use webp for pdfs feat: use webp fix: analyze resets old data fix: switch to corsproxy fix: switch to free cors fix: max upload limit fix: currency conversion feat: transaction export fix: currency conversion feat: refactor settings actions feat: new loader feat: README + LICENSE doc: update readme doc: update readme doc: update readme doc: update screenshots ci: bump prisma
This commit is contained in:
21
app/settings/backups/actions.ts
Normal file
21
app/settings/backups/actions.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
"use server"
|
||||
|
||||
import { DATABASE_FILE } from "@/lib/db"
|
||||
import fs from "fs"
|
||||
|
||||
export async function restoreBackupAction(prevState: any, formData: FormData) {
|
||||
const file = formData.get("file") as File
|
||||
if (!file) {
|
||||
return { success: false, error: "No file provided" }
|
||||
}
|
||||
|
||||
try {
|
||||
const fileBuffer = await file.arrayBuffer()
|
||||
const fileData = Buffer.from(fileBuffer)
|
||||
fs.writeFileSync(DATABASE_FILE, fileData)
|
||||
} catch (error) {
|
||||
return { success: false, error: "Failed to restore backup" }
|
||||
}
|
||||
|
||||
return { success: true }
|
||||
}
|
||||
18
app/settings/backups/database/route.ts
Normal file
18
app/settings/backups/database/route.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import { DATABASE_FILE } from "@/lib/db"
|
||||
import fs from "fs"
|
||||
import { NextResponse } from "next/server"
|
||||
|
||||
export async function GET(request: Request) {
|
||||
try {
|
||||
const file = fs.readFileSync(DATABASE_FILE)
|
||||
return new NextResponse(file, {
|
||||
headers: {
|
||||
"Content-Type": "application/octet-stream",
|
||||
"Content-Disposition": `attachment; filename="database.sqlite"`,
|
||||
},
|
||||
})
|
||||
} catch (error) {
|
||||
console.error("Error exporting database:", error)
|
||||
return new NextResponse("Internal Server Error", { status: 500 })
|
||||
}
|
||||
}
|
||||
52
app/settings/backups/files/route.ts
Normal file
52
app/settings/backups/files/route.ts
Normal file
@@ -0,0 +1,52 @@
|
||||
import { FILE_UPLOAD_PATH } from "@/lib/files"
|
||||
import fs, { readdirSync } from "fs"
|
||||
import JSZip from "jszip"
|
||||
import { NextResponse } from "next/server"
|
||||
import path from "path"
|
||||
|
||||
export async function GET(request: Request) {
|
||||
try {
|
||||
const zip = new JSZip()
|
||||
const folder = zip.folder("uploads")
|
||||
if (!folder) {
|
||||
console.error("Failed to create zip folder")
|
||||
return new NextResponse("Internal Server Error", { status: 500 })
|
||||
}
|
||||
|
||||
const files = getAllFilePaths(FILE_UPLOAD_PATH)
|
||||
files.forEach((file) => {
|
||||
folder.file(file.replace(FILE_UPLOAD_PATH, ""), fs.readFileSync(file))
|
||||
})
|
||||
const archive = await zip.generateAsync({ type: "blob" })
|
||||
|
||||
return new NextResponse(archive, {
|
||||
headers: {
|
||||
"Content-Type": "application/octet-stream",
|
||||
"Content-Disposition": `attachment; filename="uploads.zip"`,
|
||||
},
|
||||
})
|
||||
} catch (error) {
|
||||
console.error("Error exporting database:", error)
|
||||
return new NextResponse("Internal Server Error", { status: 500 })
|
||||
}
|
||||
}
|
||||
|
||||
function getAllFilePaths(dirPath: string): string[] {
|
||||
let filePaths: string[] = []
|
||||
|
||||
function readDirectory(currentPath: string) {
|
||||
const entries = readdirSync(currentPath, { withFileTypes: true })
|
||||
|
||||
for (const entry of entries) {
|
||||
const fullPath = path.join(currentPath, entry.name)
|
||||
if (entry.isDirectory()) {
|
||||
readDirectory(fullPath)
|
||||
} else {
|
||||
filePaths.push(fullPath)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
readDirectory(dirPath)
|
||||
return filePaths
|
||||
}
|
||||
58
app/settings/backups/page.tsx
Normal file
58
app/settings/backups/page.tsx
Normal file
@@ -0,0 +1,58 @@
|
||||
"use client"
|
||||
|
||||
import { Button } from "@/components/ui/button"
|
||||
import { Card } from "@/components/ui/card"
|
||||
import { Download, Loader2 } from "lucide-react"
|
||||
import Link from "next/link"
|
||||
import { useActionState } from "react"
|
||||
import { restoreBackupAction } from "./actions"
|
||||
|
||||
export default function BackupSettingsPage() {
|
||||
const [restoreState, restoreBackup, restorePending] = useActionState(restoreBackupAction, null)
|
||||
|
||||
return (
|
||||
<div className="container flex flex-col gap-4">
|
||||
<div className="flex flex-col gap-4">
|
||||
<h1 className="text-2xl font-bold">Download backup</h1>
|
||||
<div className="flex flex-row gap-4">
|
||||
<Link href="/settings/backups/database">
|
||||
<Button>
|
||||
<Download /> Download database.sqlite
|
||||
</Button>
|
||||
</Link>
|
||||
<Link href="/settings/backups/files">
|
||||
<Button>
|
||||
<Download /> Download files archive
|
||||
</Button>
|
||||
</Link>
|
||||
</div>
|
||||
<div className="text-sm text-muted-foreground">
|
||||
You can use any SQLite client to view the database.sqlite file contents
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<Card className="flex flex-col gap-4 mt-24 p-5 bg-red-100 max-w-xl">
|
||||
<h2 className="text-xl font-semibold">Restore database from backup</h2>
|
||||
<div className="text-sm text-muted-foreground">
|
||||
Warning: This will overwrite your current database and destroy all the data! Don't forget to download backup
|
||||
first.
|
||||
</div>
|
||||
<form action={restoreBackup}>
|
||||
<label>
|
||||
<input type="file" name="file" />
|
||||
</label>
|
||||
<Button type="submit" variant="destructive" disabled={restorePending}>
|
||||
{restorePending ? (
|
||||
<>
|
||||
<Loader2 className="animate-spin" /> Uploading new database...
|
||||
</>
|
||||
) : (
|
||||
"Restore"
|
||||
)}
|
||||
</Button>
|
||||
</form>
|
||||
{restoreState?.error && <p className="text-red-500">{restoreState.error}</p>}
|
||||
</Card>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
24
app/settings/backups/restore/route.ts
Normal file
24
app/settings/backups/restore/route.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
import { DATABASE_FILE } from "@/lib/db"
|
||||
import fs from "fs"
|
||||
import { NextResponse } from "next/server"
|
||||
|
||||
export async function POST(request: Request) {
|
||||
try {
|
||||
const formData = await request.formData()
|
||||
const file = formData.get("file") as File
|
||||
|
||||
if (!file) {
|
||||
return new NextResponse("No file provided", { status: 400 })
|
||||
}
|
||||
|
||||
const fileBuffer = await file.arrayBuffer()
|
||||
const fileData = Buffer.from(fileBuffer)
|
||||
|
||||
fs.writeFileSync(DATABASE_FILE, fileData)
|
||||
|
||||
return new NextResponse("File restored", { status: 200 })
|
||||
} catch (error) {
|
||||
console.error("Error restoring from backup:", error)
|
||||
return new NextResponse("Internal Server Error", { status: 500 })
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user