respect user limits
This commit is contained in:
parent
4b56653181
commit
d4e4b32783
9 changed files with 51 additions and 14 deletions
|
@ -4,7 +4,7 @@
|
|||
"private": true,
|
||||
"scripts": {
|
||||
"dev": "vite dev",
|
||||
"build": "vite build",
|
||||
"build": "BODY_SIZE_LIMIT=10737418240 vite build",
|
||||
"preview": "vite preview",
|
||||
"lint": "prettier --check .",
|
||||
"format": "prettier --write ."
|
||||
|
|
12
src/app.d.ts
vendored
12
src/app.d.ts
vendored
|
@ -1,4 +1,12 @@
|
|||
import type { User } from '@prisma/client';
|
||||
import type { Role } from '@prisma/client';
|
||||
|
||||
export interface UserSafe {
|
||||
id: number;
|
||||
username: string;
|
||||
email: string;
|
||||
maxUploadMB: number;
|
||||
role: Role;
|
||||
}
|
||||
|
||||
declare global {
|
||||
namespace App {
|
||||
|
@ -7,7 +15,7 @@ declare global {
|
|||
message: string;
|
||||
}
|
||||
interface Locals {
|
||||
user: User;
|
||||
user: UserSafe;
|
||||
}
|
||||
interface PageData {}
|
||||
// interface PageState {}
|
||||
|
|
|
@ -14,13 +14,15 @@ const PUBLIC_RESOURCES = [
|
|||
/** @type {import('@sveltejs/kit').Handle} */
|
||||
export async function handle({ event, resolve }) {
|
||||
const { cookies, locals } = event;
|
||||
const session = await getSession(cookies.get(COOKIE));
|
||||
const session = await getSession(cookies.get(COOKIE) || '');
|
||||
|
||||
if (session && session.user) {
|
||||
locals.user = {
|
||||
id: session.user.id,
|
||||
username: session.user.username,
|
||||
email: session.user.email
|
||||
email: session.user.email,
|
||||
maxUploadMB: session.user.maxUploadMB,
|
||||
role: session.user.role
|
||||
};
|
||||
} else {
|
||||
if (event.route.id) {
|
||||
|
|
|
@ -1,7 +1,8 @@
|
|||
<script lang="ts">
|
||||
import { bytesToHumanReadable } from '$lib/index';
|
||||
import { fileProgress } from '$lib/stores';
|
||||
import { fileProgress, user } from '$lib/stores';
|
||||
import { CircleAlert, X } from 'lucide-svelte';
|
||||
import { get } from 'svelte/store';
|
||||
import { fade } from 'svelte/transition';
|
||||
|
||||
export let file: File;
|
||||
|
@ -41,12 +42,16 @@
|
|||
{/if}
|
||||
|
||||
<div class="flex gap-0.5">
|
||||
{#if file.size > 104857600}
|
||||
{#if file.size > get(user).maxUploadMB * 1048576}
|
||||
<p class="font-bold text-red">
|
||||
<CircleAlert class="w-4 h-4"></CircleAlert>
|
||||
</p>
|
||||
{/if}
|
||||
<p class="text-xs my-auto {file.size > 104857600 ? 'text-red font-bold' : 'text-overlay1'}">
|
||||
<p
|
||||
class="text-xs my-auto {file.size > get(user).maxUploadMB * 1048576
|
||||
? 'text-red font-bold'
|
||||
: 'text-overlay1'}"
|
||||
>
|
||||
{bytesToHumanReadable(file.size)}
|
||||
</p>
|
||||
</div>
|
||||
|
|
|
@ -1,7 +1,8 @@
|
|||
import { writable, type Writable } from 'svelte/store';
|
||||
import type { UserSafe } from '../app';
|
||||
|
||||
export const darkMode = writable();
|
||||
export const user = writable();
|
||||
export const user: Writable<UserSafe> = writable();
|
||||
|
||||
// too lazy to do types for this
|
||||
export const fileProgress: Writable<{ [key: string]: any }> = writable({});
|
||||
|
|
|
@ -67,8 +67,10 @@
|
|||
<div class="w-[23rem] h-[calc(100vh-4.5rem)] flex mx-auto">
|
||||
<div class="flex flex-col gap-2 my-auto w-full">
|
||||
<div>
|
||||
<h1 class="text-2xl font-bold">Welcome, {$page.data.user.username}.</h1>
|
||||
<p class="text-overlay1">Your max upload size is <span class="font-bold">100 MB</span>.</p>
|
||||
<h1 class="text-2xl font-bold">Welcome, {data.user.username}.</h1>
|
||||
<p class="text-overlay1">
|
||||
Your max upload size is <span class="font-bold">{data.user.maxUploadMB} MB</span>.
|
||||
</p>
|
||||
</div>
|
||||
<div class="flex flex-col gap-2 p-2 mx-auto w-full rounded-lg shadow-lg bg-crust">
|
||||
{#key fileMap}
|
||||
|
|
10
src/routes/(landing)/+page.server.ts
Normal file
10
src/routes/(landing)/+page.server.ts
Normal file
|
@ -0,0 +1,10 @@
|
|||
/** @type {import("@sveltejs/kit").ServerLoad} */
|
||||
export function load({ locals, fetch }) {
|
||||
const statistics = fetch('/api/statistics').then((response) => response.json());
|
||||
|
||||
return {
|
||||
streamed: {
|
||||
statistics
|
||||
}
|
||||
};
|
||||
}
|
|
@ -12,6 +12,8 @@
|
|||
import LoginForm from '$lib/components/Forms/LoginForm.svelte';
|
||||
import RegisterForm from '$lib/components/Forms/RegisterForm.svelte';
|
||||
|
||||
export let data;
|
||||
|
||||
let state = writable('/landing');
|
||||
|
||||
if ($page.url.hash.replace('#', '') !== '') {
|
||||
|
@ -31,7 +33,12 @@
|
|||
<div class="flex flex-col space-y-1.5">
|
||||
<div class="text-text fill-text">
|
||||
<Logo />
|
||||
<p>Currently hosting <strong>0</strong> files.</p>
|
||||
{#await data.streamed.statistics}
|
||||
<p>Currently hosting <strong>...</strong> files.</p>
|
||||
{:then stats}
|
||||
<p>Currently hosting <strong>{stats.files}</strong> files.</p>
|
||||
{/await}
|
||||
|
||||
<p class="italic">The best file uploader <strong>ever!!!</strong></p>
|
||||
</div>
|
||||
|
||||
|
|
|
@ -14,9 +14,11 @@ export const POST = async ({ request, cookies }) => {
|
|||
|
||||
const data = await request.formData();
|
||||
const file = data.get('file') as File;
|
||||
let id = generateId(undefined, 10);
|
||||
|
||||
console.log(id);
|
||||
if (Math.floor(file.size / (1024 * 1024)) > user.maxUploadMB)
|
||||
return error(413, { status: 413, message: 'Content Too Large' });
|
||||
|
||||
let id = generateId(undefined, 10);
|
||||
|
||||
const object = await minio
|
||||
.putObject(
|
||||
|
|
Loading…
Reference in a new issue