Skip to content

Commit

Permalink
feat: file preview
Browse files Browse the repository at this point in the history
  • Loading branch information
stonith404 committed Jan 31, 2023
1 parent 0a2b7b1 commit 91a6b3f
Show file tree
Hide file tree
Showing 10 changed files with 188 additions and 28 deletions.
17 changes: 12 additions & 5 deletions backend/src/file/file.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export class FileController {
const zip = this.fileService.getZip(shareId);
res.set({
"Content-Type": "application/zip",
"Content-Disposition": `attachment ; filename="pingvin-share-${shareId}.zip"`,
"Content-Disposition": contentDisposition(`pingvin-share-${shareId}.zip`),
});

return new StreamableFile(zip);
Expand All @@ -62,14 +62,21 @@ export class FileController {
async getFile(
@Res({ passthrough: true }) res: Response,
@Param("shareId") shareId: string,
@Param("fileId") fileId: string
@Param("fileId") fileId: string,
@Query("download") download = "true"
) {
const file = await this.fileService.get(shareId, fileId);
res.set({

const headers = {
"Content-Type": file.metaData.mimeType,
"Content-Length": file.metaData.size,
"Content-Disposition": contentDisposition(file.metaData.name),
});
};

if (download === "true") {
headers["Content-Disposition"] = contentDisposition(file.metaData.name);
}

res.set(headers);

return new StreamableFile(file.file);
}
Expand Down
14 changes: 14 additions & 0 deletions frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"cookies-next": "^2.1.1",
"file-saver": "^2.0.5",
"jose": "^4.11.2",
"mime-types": "^2.1.35",
"moment": "^2.29.4",
"next": "^13.1.2",
"next-cookies": "^2.0.3",
Expand All @@ -34,6 +35,7 @@
"yup": "^0.32.11"
},
"devDependencies": {
"@types/mime-types": "^2.1.1",
"@types/node": "18.11.18",
"@types/react": "18.0.26",
"@types/react-dom": "18.0.10",
Expand Down
13 changes: 13 additions & 0 deletions frontend/src/components/core/CenterLoader.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Center, Loader, Stack } from "@mantine/core";

const CenterLoader = () => {
return (
<Center style={{ height: "70vh" }}>
<Stack align="center" spacing={10}>
<Loader />
</Stack>
</Center>
);
};

export default CenterLoader;
34 changes: 21 additions & 13 deletions frontend/src/components/share/FileList.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import { ActionIcon, Loader, Skeleton, Table } from "@mantine/core";
import { TbCircleCheck, TbDownload } from "react-icons/tb";
import { ActionIcon, Group, Skeleton, Table } from "@mantine/core";
import mime from "mime-types";
import Link from "next/link";
import { TbDownload, TbEye } from "react-icons/tb";
import shareService from "../../services/share.service";

import { FileMetaData } from "../../types/File.type";
import { byteToHumanSizeString } from "../../utils/fileSize.util";

const FileList = ({
files,
shareId,
isLoading,
}: {
files?: any[];
files?: FileMetaData[];
shareId: string;
isLoading: boolean;
}) => {
Expand All @@ -28,15 +30,21 @@ const FileList = ({
: files!.map((file) => (
<tr key={file.name}>
<td>{file.name}</td>
<td>{byteToHumanSizeString(file.size)}</td>
<td>{byteToHumanSizeString(parseInt(file.size))}</td>
<td>
{file.uploadingState ? (
file.uploadingState != "finished" ? (
<Loader size={22} />
) : (
<TbCircleCheck color="green" size={22} />
)
) : (
<Group position="right">
{shareService.doesFileSupportPreview(file.name) && (
<ActionIcon
component={Link}
href={`/share/${shareId}/preview/${
file.id
}?type=${mime.contentType(file.name)}`}
target="_blank"
size={25}
>
<TbEye />
</ActionIcon>
)}
<ActionIcon
size={25}
onClick={async () => {
Expand All @@ -45,7 +53,7 @@ const FileList = ({
>
<TbDownload />
</ActionIcon>
)}
</Group>
</td>
</tr>
))}
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/pages/account/reverseShares.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
Button,
Center,
Group,
LoadingOverlay,
Stack,
Table,
Text,
Expand All @@ -18,6 +17,7 @@ import { useRouter } from "next/router";
import { useEffect, useState } from "react";
import { TbInfoCircle, TbLink, TbPlus, TbTrash } from "react-icons/tb";
import showShareLinkModal from "../../components/account/showShareLinkModal";
import CenterLoader from "../../components/core/CenterLoader";
import Meta from "../../components/Meta";
import showCreateReverseShareModal from "../../components/share/modals/showCreateReverseShareModal";
import useConfig from "../../hooks/config.hook";
Expand Down Expand Up @@ -50,7 +50,7 @@ const MyShares = () => {
if (!user) {
router.replace("/");
} else {
if (!reverseShares) return <LoadingOverlay visible />;
if (!reverseShares) return <CenterLoader />;
return (
<>
<Meta title="My shares" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import { Box, Group, Text, Title } from "@mantine/core";
import { useModals } from "@mantine/modals";
import { GetServerSidePropsContext } from "next";
import { useEffect, useState } from "react";
import Meta from "../../components/Meta";
import DownloadAllButton from "../../components/share/DownloadAllButton";
import FileList from "../../components/share/FileList";
import showEnterPasswordModal from "../../components/share/showEnterPasswordModal";
import showErrorModal from "../../components/share/showErrorModal";
import shareService from "../../services/share.service";
import { Share as ShareType } from "../../types/share.type";
import Meta from "../../../components/Meta";
import DownloadAllButton from "../../../components/share/DownloadAllButton";
import FileList from "../../../components/share/FileList";
import showEnterPasswordModal from "../../../components/share/showEnterPasswordModal";
import showErrorModal from "../../../components/share/showErrorModal";
import shareService from "../../../services/share.service";
import { Share as ShareType } from "../../../types/share.type";

export function getServerSideProps(context: GetServerSidePropsContext) {
return {
Expand Down
92 changes: 92 additions & 0 deletions frontend/src/pages/share/[shareId]/preview/[fileId].tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import { Center, Stack, Text, Title } from "@mantine/core";
import { GetServerSidePropsContext } from "next";
import { useState } from "react";

export function getServerSideProps(context: GetServerSidePropsContext) {
const { shareId, fileId } = context.params!;

const mimeType = context.query.type as string;

return {
props: { shareId, fileId, mimeType },
};
}

const UnSupportedFile = () => {
return (
<Center style={{ height: "70vh" }}>
<Stack align="center" spacing={10}>
<Title order={3}>Preview not supported</Title>
<Text>
A preview for thise file type is unsupported. Please download the file
to view it.
</Text>
</Stack>
</Center>
);
};

const FilePreview = ({
shareId,
fileId,
mimeType,
}: {
shareId: string;
fileId: string;
mimeType: string;
}) => {
const [isNotSupported, setIsNotSupported] = useState(false);

if (isNotSupported) return <UnSupportedFile />;

if (mimeType == "application/pdf") {
window.location.href = `/api/shares/${shareId}/files/${fileId}?download=false`;
return null;
} else if (mimeType.startsWith("video/")) {
return (
<video
width="100%"
controls
onError={() => {
setIsNotSupported(true);
}}
>
<source src={`/api/shares/${shareId}/files/${fileId}?download=false`} />
</video>
);
} else if (mimeType.startsWith("image/")) {
return (
// eslint-disable-next-line @next/next/no-img-element
<img
onError={() => {
setIsNotSupported(true);
}}
src={`/api/shares/${shareId}/files/${fileId}?download=false`}
alt={`${fileId}_preview`}
width="100%"
/>
);
} else if (mimeType.startsWith("audio/")) {
return (
<Center style={{ height: "70vh" }}>
<Stack align="center" spacing={10} style={{ width: "100%" }}>
<audio
controls
style={{ width: "100%" }}
onError={() => {
setIsNotSupported(true);
}}
>
<source
src={`/api/shares/${shareId}/files/${fileId}?download=false`}
/>
</audio>
</Stack>
</Center>
);
} else {
return <UnSupportedFile />;
}
};

export default FilePreview;
20 changes: 19 additions & 1 deletion frontend/src/services/share.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { setCookie } from "cookies-next";
import mime from "mime-types";
import { FileUploadResponse } from "../types/File.type";

import {
CreateShare,
MyReverseShare,
Expand Down Expand Up @@ -47,7 +49,22 @@ const getShareToken = async (id: string, password?: string) => {
};

const isShareIdAvailable = async (id: string): Promise<boolean> => {
return (await api.get(`shares/isShareIdAvailable/${id}`)).data.isAvailable;
return (await api.get(`/shares/isShareIdAvailable/${id}`)).data.isAvailable;
};

const doesFileSupportPreview = (fileName: string) => {
const mimeType = mime.contentType(fileName);

if (!mimeType) return false;

const supportedMimeTypes = [
mimeType.startsWith("video/"),
mimeType.startsWith("image/"),
mimeType.startsWith("audio/"),
mimeType == "application/pdf",
];

return supportedMimeTypes.some((isSupported) => isSupported);
};

const downloadFile = async (shareId: string, fileId: string) => {
Expand Down Expand Up @@ -114,6 +131,7 @@ export default {
get,
remove,
getMetaData,
doesFileSupportPreview,
getMyShares,
isShareIdAvailable,
downloadFile,
Expand Down
6 changes: 6 additions & 0 deletions frontend/src/types/File.type.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
export type FileUpload = File & { uploadingProgress: number };

export type FileUploadResponse = { id: string; name: string };

export type FileMetaData = {
id: string;
name: string;
size: string;
};

0 comments on commit 91a6b3f

Please sign in to comment.