mirror of
https://github.com/swissmakers/swiss-datashare.git
synced 2026-04-21 06:03:13 +02:00
initial commit
This commit is contained in:
9
src/utils/appwrite.util.ts
Normal file
9
src/utils/appwrite.util.ts
Normal 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;
|
||||
17
src/utils/appwriteServer.util.ts
Normal file
17
src/utils/appwriteServer.util.ts
Normal 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
17
src/utils/auth.util.ts
Normal 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,
|
||||
};
|
||||
6
src/utils/loading.util.ts
Normal file
6
src/utils/loading.util.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
import { createContext, Dispatch, SetStateAction } from "react";
|
||||
|
||||
export const GlobalLoadingContext = createContext<{
|
||||
isLoading: boolean;
|
||||
setIsLoading: Dispatch<SetStateAction<boolean>>;
|
||||
}>({ isLoading: false, setIsLoading: () => {} });
|
||||
6
src/utils/math/byteToSize.util.ts
Normal file
6
src/utils/math/byteToSize.util.ts
Normal 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];
|
||||
}
|
||||
35
src/utils/shares/security.util.ts
Normal file
35
src/utils/shares/security.util.ts
Normal 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
26
src/utils/toast.util.tsx
Normal 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;
|
||||
Reference in New Issue
Block a user