2022-11-14 00:09:27 +00:00
|
|
|
import { Button } from "@nextui-org/react";
|
2022-11-14 00:45:20 +00:00
|
|
|
import moment from "moment/moment";
|
2022-11-14 00:09:27 +00:00
|
|
|
import Image from "next/image";
|
2022-11-14 00:45:20 +00:00
|
|
|
import Link from "next/link";
|
2022-11-14 00:09:27 +00:00
|
|
|
import { getFileInfo } from "src/utils/helpers/fileHelpers";
|
2022-11-14 00:45:20 +00:00
|
|
|
import { formatBytes } from "src/utils/helpers/stringHelpers";
|
|
|
|
import { downloadURI } from "src/utils/helpers/webUtils";
|
2022-11-13 22:45:51 +00:00
|
|
|
|
|
|
|
export default function File(props) {
|
|
|
|
const { isValidFile, fileData } = props;
|
|
|
|
const file = JSON.parse(fileData);
|
2022-11-14 00:45:20 +00:00
|
|
|
let {
|
|
|
|
fileId,
|
|
|
|
originalFileName,
|
|
|
|
uploadDate,
|
|
|
|
contentType,
|
|
|
|
fileUrl,
|
|
|
|
width,
|
|
|
|
height,
|
|
|
|
ext,
|
|
|
|
size,
|
|
|
|
} = file;
|
2022-11-14 00:09:27 +00:00
|
|
|
|
|
|
|
let toShow;
|
2022-11-14 00:45:20 +00:00
|
|
|
if (!isValidFile) {
|
2022-11-14 00:09:27 +00:00
|
|
|
toShow = (
|
2022-11-14 00:45:20 +00:00
|
|
|
<>
|
|
|
|
<h1 className="text-red-500 text-3xl mb-5">Invalid File</h1>
|
|
|
|
<Link href="/">
|
|
|
|
<Button auto className="bg-blue-600">
|
|
|
|
Go Home
|
|
|
|
</Button>
|
|
|
|
</Link>
|
|
|
|
</>
|
2022-11-14 00:09:27 +00:00
|
|
|
);
|
2022-11-14 00:45:20 +00:00
|
|
|
}
|
|
|
|
if (isValidFile) {
|
|
|
|
contentType = contentType.toLowerCase();
|
|
|
|
if (contentType.includes("image")) {
|
|
|
|
toShow = (
|
2022-11-14 03:31:42 +00:00
|
|
|
<Image
|
|
|
|
alt={fileId}
|
|
|
|
src={fileUrl}
|
|
|
|
width={width}
|
|
|
|
height={height}
|
|
|
|
unoptimized
|
|
|
|
></Image>
|
2022-11-14 00:45:20 +00:00
|
|
|
);
|
2022-11-14 04:02:47 +00:00
|
|
|
}
|
|
|
|
if (contentType.includes("video")) {
|
2022-11-14 00:45:20 +00:00
|
|
|
toShow = <video alt={fileId} src={fileUrl} controls></video>;
|
|
|
|
}
|
2022-11-14 00:09:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2022-11-14 00:45:20 +00:00
|
|
|
<div className="h-screen flex items-center justify-center">
|
|
|
|
<div className="flex flex-col text-center items-center justify-center">
|
|
|
|
{isValidFile ? (
|
|
|
|
<div className="mb-4">
|
|
|
|
<h1 className="font-bold text-lg">
|
|
|
|
{originalFileName} ({fileId}.{ext})
|
|
|
|
</h1>
|
|
|
|
<h3>{moment(uploadDate).format("MMMM Do YYYY, h:mm:ss a")}</h3>
|
|
|
|
<h3>{formatBytes(size)}</h3>
|
|
|
|
</div>
|
|
|
|
) : null}
|
2022-11-14 04:02:47 +00:00
|
|
|
<div className="md:w-4/6 md:h-4/6 sm:w-fit sm:h-fit">{toShow}</div>
|
|
|
|
<Button
|
|
|
|
auto
|
|
|
|
className="bg-blue-600 mt-5"
|
|
|
|
onPress={() => {
|
|
|
|
downloadURI(fileUrl, `${fileId}.${ext}`);
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
Download
|
|
|
|
</Button>
|
2022-11-14 00:45:20 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
2022-11-14 00:09:27 +00:00
|
|
|
);
|
2022-11-13 22:45:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function getServerSideProps(ctx) {
|
|
|
|
let { fileId } = ctx.query;
|
|
|
|
fileId = fileId.split(".")[0];
|
|
|
|
|
2022-11-14 00:09:27 +00:00
|
|
|
const file = await getFileInfo(fileId);
|
2022-11-13 22:45:51 +00:00
|
|
|
return {
|
|
|
|
props: {
|
|
|
|
isValidFile: file !== null,
|
|
|
|
fileData: JSON.stringify(file || []),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|