Skip to content

Commit

Permalink
No avisa quan un fitxer és massa gran (#82)
Browse files Browse the repository at this point in the history
  • Loading branch information
PauMatas committed Apr 4, 2024
1 parent 36e77a6 commit 6e68051
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 11 deletions.
9 changes: 8 additions & 1 deletion next.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
/** @type {import('next').NextConfig} */
const nextConfig = {
images: {
domains: ["uploadthing.com", "lh3.googleusercontent.com"],
remotePatterns: [
{
hostname: "*.googleusercontent.com",
},
{
hostname: "uploadthing.com",
},
],
},
experimental: {
appDir: true,
Expand Down
8 changes: 6 additions & 2 deletions src/app/api/uploadthing/core.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { createUploadthing, type FileRouter } from "uploadthing/next"
import { UploadThingError } from "uploadthing/server"
import { getToken } from "next-auth/jwt"
import { MAX_FILE_COUNT, MAX_FILE_SIZE_MB } from "@/config"

const f = createUploadthing()

Expand All @@ -14,8 +15,11 @@ export const ourFileRouter = {
.onUploadComplete(async ({}) => {}),

fileUploader: f({
pdf: { maxFileCount: 10, maxFileSize: "32MB" },
text: { maxFileCount: 10, maxFileSize: "32MB" },
pdf: { maxFileCount: MAX_FILE_COUNT, maxFileSize: `${MAX_FILE_SIZE_MB}MB` },
text: {
maxFileCount: MAX_FILE_COUNT,
maxFileSize: `${MAX_FILE_SIZE_MB}MB`,
},
})
.middleware(async ({ req }) => {
const user = await getToken({ req })
Expand Down
32 changes: 26 additions & 6 deletions src/components/Form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { uploadFiles } from "@/lib/uploadthing"
import Fireworks from "react-canvas-confetti/dist/presets/fireworks"
import { MultiFileDropzone } from "@/components/MultiFileDropzone"
import { Textarea } from "./ui/Textarea"
import { MAX_FILE_COUNT, MAX_FILE_SIZE_MB } from "@/config"

const formSchema = z.object({
pdf: z.array(z.any()),
Expand Down Expand Up @@ -223,11 +224,26 @@ export function ProfileForm({
<FormItem>
<FormLabel>Fitxers PDF</FormLabel>
<FormControl>
<div className="grid w-full max-w-sm items-center gap-1.5">
<div className="w-full max-w-sm items-center gap-1.5">
<MultiFileDropzone
value={field.value}
onChange={(acceptedFiles) => {
field.onChange(acceptedFiles)
if (
acceptedFiles.length > MAX_FILE_COUNT ||
acceptedFiles.some(
(file) => file.size >= MAX_FILE_SIZE_MB * 10 ** 6,
)
) {
toast({
variant: "destructive",
title: "Fitxers no vàlids",
description:
"Només pots pujar fins a 10 fitxers de menys de 32MB cada un.",
})
field.onChange([])
} else {
field.onChange(acceptedFiles)
}
}}
/>
</div>
Expand All @@ -250,7 +266,7 @@ export function ProfileForm({
<Input
placeholder="WhoIsGraf?"
{...field}
className="mb-0"
className="my-2"
/>
</FormControl>
<FormMessage />
Expand All @@ -265,7 +281,7 @@ export function ProfileForm({
<FormControl>
<Textarea
placeholder="Descripció (opcional)"
className="resize-none"
className="resize-none my-2"
{...field}
/>
</FormControl>
Expand Down Expand Up @@ -372,10 +388,14 @@ export function ProfileForm({
<FormItem>
<FormLabel>Email</FormLabel>
<FormControl>
<Input placeholder="Gràcies per ajudar" {...field} />
<Input placeholder="Uploader" {...field} />
</FormControl>
<FormDescription>
Email de l&apos;autor/a dels apunts
Aquest camp t&apos;apareix ja que ets admin i tens permís
per a compartir apunts en nom d&apos;algú altre. Escriu
&quot;Uploader&quot; si els comparteixes en nom teu.
<br />
Gràcies per ajudar!
</FormDescription>
<FormMessage />
</FormItem>
Expand Down
4 changes: 2 additions & 2 deletions src/components/ui/Textarea.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ const Textarea = React.forwardRef<HTMLTextAreaElement, TextareaProps>(
<textarea
className={cn(
"flex min-h-[80px] w-full rounded-md border border-input bg-background px-3 py-2 text-sm ring-offset-background placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50",
className
className,
)}
ref={ref}
{...props}
/>
)
}
},
)
Textarea.displayName = "Textarea"

Expand Down
3 changes: 3 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
export const INFINITE_SCROLL_PAGINATION_RESULTS = 4
export const GCED_START = 2017

export const MAX_FILE_COUNT = 10
export const MAX_FILE_SIZE_MB = 32

0 comments on commit 6e68051

Please sign in to comment.