digital ocean spaces/aws S3 integratoin

This commit is contained in:
Daniel
2023-07-01 17:41:39 +03:30
parent 9cf3b78ba1
commit 04a92dae37
20 changed files with 1330 additions and 78 deletions
+5 -16
View File
@@ -1,9 +1,8 @@
import type { NextApiRequest, NextApiResponse } from "next";
import { getServerSession } from "next-auth/next";
import { authOptions } from "pages/api/auth/[...nextauth]";
import path from "path";
import fs from "fs";
import getPermission from "@/lib/api/getPermission";
import readFile from "@/lib/api/storage/readFile";
export default async function Index(req: NextApiRequest, res: NextApiResponse) {
if (!req.query.params)
@@ -27,20 +26,10 @@ export default async function Index(req: NextApiRequest, res: NextApiResponse) {
.status(401)
.json({ response: "You don't have access to this collection." });
const requestedPath = `data/archives/${collectionId}/${linkId}`;
const filePath = path.join(process.cwd(), requestedPath);
const file = fs.existsSync(filePath)
? fs.readFileSync(filePath)
: "File not found, it's possible that the file you're looking for either doesn't exist or hasn't been created yet.";
if (!fs.existsSync(filePath))
res.setHeader("Content-Type", "text/plain").status(404);
else if (filePath.endsWith(".pdf"))
res.setHeader("Content-Type", "application/pdf").status(200);
else if (filePath.endsWith(".png"))
res.setHeader("Content-Type", "image/png").status(200);
const { file, contentType } = await readFile({
filePath: `archives/${collectionId}/${linkId}`,
});
res.setHeader("Content-Type", contentType).status(200);
return res.send(file);
}