initial commit

This commit is contained in:
Elias Schneider
2022-04-25 15:15:17 +02:00
commit 61be87b72a
72 changed files with 11502 additions and 0 deletions

View File

@@ -0,0 +1,9 @@
import { Appwrite } from "appwrite";
// SDK for client side (browser)
const aw = new Appwrite();
aw.setEndpoint("http://localhost:86/v1")
.setProject("pingvin-share");
export default aw;

View File

@@ -0,0 +1,17 @@
import sdk from "node-appwrite";
// SDK for server side (api)
const client = new sdk.Client();
client
.setEndpoint(process.env["APPWRITE_HOST"] as string)
.setProject("pingvin-share")
.setKey(process.env["APPWRITE_FUNCTION_API_KEY"] as string);
const awServer = {
user: new sdk.Users(client),
storage: new sdk.Storage(client),
database: new sdk.Database(client),
};
export default awServer;

17
src/utils/auth.util.ts Normal file
View File

@@ -0,0 +1,17 @@
import { createContext } from "react";
import aw from "./appwrite.util";
const isSignedIn = async() => {
try {
await aw.account.get();
return true;
} catch {
return false;
}
};
export const IsSignedInContext = createContext(false);
export default {
isSignedIn,
};

View File

@@ -0,0 +1,6 @@
import { createContext, Dispatch, SetStateAction } from "react";
export const GlobalLoadingContext = createContext<{
isLoading: boolean;
setIsLoading: Dispatch<SetStateAction<boolean>>;
}>({ isLoading: false, setIsLoading: () => {} });

View File

@@ -0,0 +1,6 @@
export function bytesToSize(bytes: number) {
const sizes = ["B", "KB", "MB", "GB", "TB"];
if (bytes == 0) return "0 Byte";
const i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)).toString());
return (bytes / Math.pow(1024, i)).toFixed(1).toString() + " " + sizes[i];
}

View File

@@ -0,0 +1,35 @@
import { scryptSync } from "crypto";
import { SecurityDocument, ShareDocument } from "../../types/Appwrite.type";
import awServer from "../appwriteServer.util";
export const hashPassword = (password: string, salt: string) => {
return scryptSync(password, salt, 64).toString("hex");
};
export const checkSecurity = async (
shareId: string,
hashedPassword?: string
) => {
const shareDocument = await awServer.database.getDocument<ShareDocument>(
"shares",
shareId
);
if (!shareDocument.securityID) return;
await awServer.database
.getDocument<SecurityDocument>("shareSecurity", shareDocument.securityID)
.then((securityDocument) => {
if (securityDocument.maxVisitors) {
if (shareDocument.visitorCount > securityDocument.maxVisitors) {
throw "visitor_limit_exceeded";
}
}
if (securityDocument.password) {
if (!hashedPassword) throw "password_required";
if (hashedPassword !== securityDocument.password) {
throw "wrong_password";
}
}
});
return { hashedPassword };
};

26
src/utils/toast.util.tsx Normal file
View File

@@ -0,0 +1,26 @@
import { showNotification } from "@mantine/notifications";
import { Check, X } from "tabler-icons-react";
const error = (message: string) =>
showNotification({
icon: <X />,
color: "red",
radius: "md",
title: "Error",
message: message,
});
const success = (message: string) =>
showNotification({
icon: <Check />,
color: "green",
radius: "md",
title: "Success",
message: message,
});
const toast = {
error,
success,
};
export default toast;