Skip to content

Commit

Permalink
Revert "Add file size limit for uploads and fix error handling in fil…
Browse files Browse the repository at this point in the history
…e upload (#1557)"

This reverts commit 8c2b227.
  • Loading branch information
mckaywrigley committed Mar 26, 2024
1 parent 60b8563 commit f2a9752
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 52 deletions.
3 changes: 0 additions & 3 deletions .env.local.example
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,3 @@ AZURE_EMBEDDINGS_NAME=
# General Configuration (Optional)
EMAIL_DOMAIN_WHITELIST=
EMAIL_WHITELIST=

# File size limit for uploads in bytes
NEXT_PUBLIC_USER_FILE_SIZE_LIMIT=10485760
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,4 @@ custom-prompts
sw.js
sw.js.map
workbox-*.js
workbox-*.js.map
workbox-*.js.map
30 changes: 2 additions & 28 deletions app/api/retrieval/process/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,39 +24,13 @@ export async function POST(req: Request) {

const formData = await req.formData()

const file = formData.get("file") as File
const file_id = formData.get("file_id") as string
const embeddingsProvider = formData.get("embeddingsProvider") as string

const { data: fileMetadata, error: metadataError } = await supabaseAdmin
.from("files")
.select("*")
.eq("id", file_id)
.single()

if (metadataError) {
throw new Error(
`Failed to retrieve file metadata: ${metadataError.message}`
)
}

if (!fileMetadata) {
throw new Error("File not found")
}

if (fileMetadata.user_id !== profile.user_id) {
throw new Error("Unauthorized")
}

const { data: file, error: fileError } = await supabaseAdmin.storage
.from("files")
.download(fileMetadata.file_path)

if (fileError)
throw new Error(`Failed to retrieve file: ${fileError.message}`)

const fileBuffer = Buffer.from(await file.arrayBuffer())
const blob = new Blob([fileBuffer])
const fileExtension = fileMetadata.name.split(".").pop()?.toLowerCase()
const fileExtension = file.name.split(".").pop()?.toLowerCase()

if (embeddingsProvider === "openai") {
if (profile.use_azure_openai) {
Expand Down
7 changes: 3 additions & 4 deletions components/chat/chat-hooks/use-select-file-handler.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,9 @@ export const useSelectFileHandler = () => {
)
)
}
} catch (error: any) {
toast.error("Failed to upload. " + error?.message, {
duration: 10000
})
} catch (error) {
toast.error("Failed to upload.")

setNewMessageImages(prev =>
prev.filter(img => img.messageId !== "temp")
)
Expand Down
10 changes: 1 addition & 9 deletions db/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,6 @@ export const createFile = async (
workspace_id: string,
embeddingsProvider: "openai" | "local"
) => {
let validFilename = fileRecord.name.replace(/[^a-z0-9.]/gi, "_").toLowerCase()
const extension = validFilename.split(".").pop()
const baseName = validFilename.substring(0, validFilename.lastIndexOf("."))
const maxBaseNameLength = 100 - (extension?.length || 0) - 1
if (baseName.length > maxBaseNameLength) {
validFilename = baseName.substring(0, maxBaseNameLength) + "." + extension
}
fileRecord.name = validFilename

const { data: createdFile, error } = await supabase
.from("files")
.insert([fileRecord])
Expand Down Expand Up @@ -127,6 +118,7 @@ export const createFile = async (
})

const formData = new FormData()
formData.append("file", file)
formData.append("file_id", createdFile.id)
formData.append("embeddingsProvider", embeddingsProvider)

Expand Down
10 changes: 3 additions & 7 deletions db/storage/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,10 @@ export const uploadFile = async (
file_id: string
}
) => {
const SIZE_LIMIT = parseInt(
process.env.NEXT_PUBLIC_USER_FILE_SIZE_LIMIT || "10000000"
)
const SIZE_LIMIT = 10000000 // 10MB

if (file.size > SIZE_LIMIT) {
throw new Error(
`File must be less than ${Math.floor(SIZE_LIMIT / 1000000)}MB`
)
throw new Error(`File must be less than ${SIZE_LIMIT / 1000000}MB`)
}

const filePath = `${payload.user_id}/${Buffer.from(payload.file_id).toString("base64")}`
Expand All @@ -28,6 +24,7 @@ export const uploadFile = async (
})

if (error) {
console.error(`Error uploading file with path: ${filePath}`, error)
throw new Error("Error uploading file")
}

Expand All @@ -49,7 +46,6 @@ export const getFileFromStorage = async (filePath: string) => {
.createSignedUrl(filePath, 60 * 60 * 24) // 24hrs

if (error) {
console.error(`Error uploading file with path: ${filePath}`, error)
throw new Error("Error downloading file")
}

Expand Down

0 comments on commit f2a9752

Please sign in to comment.