1
0
mirror of https://github.com/stonith404/pingvin-share.git synced 2024-10-01 00:50:10 +02:00

Improve error handling on upload

This commit is contained in:
Elias Schneider 2022-04-28 16:01:50 +02:00
parent 5845659bab
commit 09645c6202
No known key found for this signature in database
GPG Key ID: D5EC1C72D93244FD
4 changed files with 86 additions and 52 deletions

View File

@ -15,6 +15,7 @@ import { useForm, yupResolver } from "@mantine/form";
import { useModals } from "@mantine/modals"; import { useModals } from "@mantine/modals";
import { ModalsContextProps } from "@mantine/modals/lib/context"; import { ModalsContextProps } from "@mantine/modals/lib/context";
import * as yup from "yup"; import * as yup from "yup";
import shareService from "../../services/share.service";
const showCreateUploadModal = ( const showCreateUploadModal = (
modals: ModalsContextProps, modals: ModalsContextProps,
@ -57,16 +58,20 @@ const Body = ({
return ( return (
<form <form
onSubmit={form.onSubmit((values) => { onSubmit={form.onSubmit(async (values) => {
modals.closeAll(); if (await shareService.isIdAlreadyInUse(values.link)) {
uploadCallback(values.link, parseInt(values.expiration), { form.setFieldError("link", "Link already in use.");
password: values.password, } else {
maxVisitors: values.maxVisitors, modals.closeAll();
}); uploadCallback(values.link, parseInt(values.expiration), {
password: values.password,
maxVisitors: values.maxVisitors,
});
}
})} })}
> >
<Group direction="column" grow> <Group direction="column" grow>
<Grid align="flex-end"> <Grid align={form.errors.link ? "center" : "flex-end"}>
<Col xs={9}> <Col xs={9}>
<TextInput <TextInput
variant="filled" variant="filled"

View File

@ -0,0 +1,19 @@
import type { NextApiRequest, NextApiResponse } from "next";
import { ShareDocument } from "../../../../types/Appwrite.type";
import awServer from "../../../../utils/appwriteServer.util";
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
const shareId = req.query.shareId as string;
let doesExists;
try {
await awServer.database.getDocument<ShareDocument>("shares", shareId);
doesExists = true;
} catch {
doesExists = false;
}
res.status(200).json({ exists: doesExists });
};
export default handler;

View File

@ -26,43 +26,54 @@ const Upload = () => {
security: { password?: string; maxVisitors?: number } security: { password?: string; maxVisitors?: number }
) => { ) => {
setisUploading(true); setisUploading(true);
try {
files.forEach((file) => {
file.uploadingState = "inProgress";
});
const bucketId = JSON.parse( const bucketId = JSON.parse(
( (
await aw.functions.createExecution( await aw.functions.createExecution(
"createShare", "createShare",
JSON.stringify({ id, security, expiration }), JSON.stringify({ id, security, expiration }),
false false
) )
).stdout ).stdout
).id; ).id;
for (let i = 0; i < files.length; i++) { for (let i = 0; i < files.length; i++) {
files[i].uploadingState = "inProgress"; setFiles([...files]);
setFiles([...files]); aw.storage.createFile(bucketId, "unique()", files[i]).then(
aw.storage.createFile(bucketId, "unique()", files[i]).then( async () => {
async () => { files[i].uploadingState = "finished";
files[i].uploadingState = "finished"; setFiles([...files]);
setFiles([...files]); if (!files.some((f) => f.uploadingState == "inProgress")) {
if (!files.some((f) => f.uploadingState == "inProgress")) { await aw.functions.createExecution(
await aw.functions.createExecution( "finishShare",
"finishShare", JSON.stringify({ id }),
JSON.stringify({ id }), false
false ),
), setisUploading(false);
setisUploading(false); showCompletedUploadModal(
showCompletedUploadModal( modals,
modals, `${window.location.origin}/share/${bucketId}`,
`${window.location.origin}/share/${bucketId}`, new Date(Date.now() + expiration * 60 * 1000).toLocaleString()
new Date(Date.now() + expiration * 60 * 1000).toLocaleString() );
); setFiles([]);
}
},
(error) => {
files[i].uploadingState = undefined;
throw error;
} }
}, );
(error) => { }
files[i].uploadingState = undefined; } catch (e: any) {
toast.error(error.message); setisUploading(false);
setisUploading(false); if (e.message) {
} toast.error(e.message);
); } else {
toast.error("An unknown error occurred.");
}
} }
}; };

View File

@ -2,18 +2,17 @@ import axios from "axios";
import { AppwriteFileWithPreview } from "../types/File.type"; import { AppwriteFileWithPreview } from "../types/File.type";
const get = async (shareId: string, password?: string) => { const get = async (shareId: string, password?: string) => {
return ( return (await axios.post(`/api/share/${shareId}`, { password }))
await axios.post(`/api/share/${shareId}`, { password }) .data as AppwriteFileWithPreview[];
).data as AppwriteFileWithPreview[]; };
const isIdAlreadyInUse = async (shareId: string) => {
return (await axios.get(`/api/share/${shareId}/exists`)).data
.exists as boolean;
}; };
const authenticateWithPassword = async (shareId: string, password?: string) => { const authenticateWithPassword = async (shareId: string, password?: string) => {
return ( return (await axios.post(`/api/share/${shareId}/enterPassword`, { password }))
await axios.post( .data as AppwriteFileWithPreview[];
`/api/share/${shareId}/enterPassword`,
{ password }
)
).data as AppwriteFileWithPreview[];
}; };
export default { get, authenticateWithPassword }; export default { get, authenticateWithPassword, isIdAlreadyInUse };