Skip to content

Commit

Permalink
Merge pull request #103 from imperial/fix-volume-size-errors
Browse files Browse the repository at this point in the history
fix: volume size errors
  • Loading branch information
alexanderbira authored Sep 5, 2024
2 parents a645626 + 4672bfc commit 5bb11e1
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
25 changes: 22 additions & 3 deletions components/forms/FormWithAction.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"use client"

import { MAX_FILE_SIZE } from "@/lib/constants"
import { FormPassBackState, ServerSideFormHandler } from "@/lib/types"

import { ErrorCallout } from "../Callouts"
Expand Down Expand Up @@ -50,16 +51,34 @@ export function FormWithAction({
const [isPending, startTransition] = useTransition()
const wrappedAction = useCallback(
(prevState: FormPassBackState, formData: FormData): Promise<FormPassBackState> => {
return new Promise(async (resolve, reject) => {
return new Promise(async (resolve, _reject) => {
startTransition(async () => {
try {
// Check if the total size of the files is too large
let totalSize = 0
formData.forEach(value => {
if (value instanceof File) {
totalSize += value.size
}
})
if (totalSize >= MAX_FILE_SIZE) {
resolve({
status: "error",
message: `The total size of the files you are trying to upload is too large (total is over ~${Math.round(MAX_FILE_SIZE / 1_000_000)}MB). Please try again with smaller files, or fewer files at once.`,
})
return
}

const res = await action(prevState, formData)
if (res.status === "success") {
onSuccess?.(res)
}
resolve(res)
} catch (e) {
reject(e)
} catch (e: any) {
resolve({
status: "error",
message: `An unexpected error occurred: ${e.message}. This may be related to the nginx configuration. Please contact us if this error persists.`,
})
}
})
})
Expand Down
3 changes: 3 additions & 0 deletions lib/constants.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
// Timezone that is used to create events
export const TIMEZONE = "Europe/London"

// The maximum file size in bytes
export const MAX_FILE_SIZE = 1_000_000 - 1
3 changes: 1 addition & 2 deletions lib/files/saveFile.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { MAX_FILE_SIZE } from "@/lib/constants"
import { FileCategory } from "@/lib/types"

import { promises as fs } from "fs"
import { join } from "path"
import path from "path"

const MAX_FILE_SIZE = 1000 * 1000 - 1 // Under 1MB

// Must be the same as the allowed file types in the file viewer
const allowedImageTypes = ["image/png", "image/jpeg", "image/gif", "image/svg+xml"]
const allowedDocumentTypes = ["application/pdf", "text/plain"]
Expand Down

0 comments on commit 5bb11e1

Please sign in to comment.