mirror of
https://github.com/marcogll/TaxHacker_s23.git
synced 2026-01-13 21:35:19 +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:
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
|
||||
}
|
||||
Reference in New Issue
Block a user