Refactor file upload to use SDK instead of XMLHttpRequest
Some checks failed
Build and Push Docker Images / changes (push) Successful in 4s
Build and Push Docker Images / build-backend (push) Successful in 50s
Build and Push Docker Images / build-frontend (push) Failing after 47s

Replaced manual XMLHttpRequest implementation with the SDK's `uploadFile` method for handling file uploads to presigned URLs. This simplifies the code, leverages built-in features like progress tracking, and improves error handling. Added token extraction and upload progress updates to maintain functionality.
This commit is contained in:
2025-03-14 00:05:26 +01:00
parent 0cf5498762
commit b47dbaeb0b
2 changed files with 45 additions and 18 deletions

View File

@@ -17,7 +17,8 @@ logger.info(f"Starting app!!!")
app = FastAPI(
title=settings.PROJECT_NAME,
version=settings.VERSION,
openapi_url=f"{settings.API_VERSION_STR}/openapi.json"
openapi_url=f"{settings.API_VERSION_STR}/openapi.json",
debug=True,
)
# Set up CORS middleware

View File

@@ -1,6 +1,10 @@
import { useState, useCallback, useEffect } from "react";
import { useMutation } from "@tanstack/react-query";
import { generatePresignedUrl, PresignedUrlResponse } from "@/client";
import {
generatePresignedUrl,
uploadFile as uploadFileSdk,
PresignedUrlResponse,
} from "@/client";
export interface FileUploadState {
file?: File;
@@ -58,26 +62,48 @@ export function usePresignedUpload(options: UsePresignedUploadOptions = {}) {
const uploadFileToPresignedUrl = useCallback(
async (file: File, uploadUrl: string): Promise<void> => {
return new Promise<void>((resolve, reject) => {
const xhr = new XMLHttpRequest();
try {
// Use the SDK's uploadFile method instead of direct XMLHttpRequest
xhr.upload.onprogress = ({ loaded, total }) => {
const progress = total ? Math.round((loaded / total) * 100) : 0;
setUploadState((prev) => ({ ...prev, progress }));
};
// Create form data for the file
const formData = new FormData();
formData.append("file", file);
xhr.onload = () => {
if (xhr.status >= 200 && xhr.status < 300) {
resolve();
} else {
reject(new Error(`Upload failed: ${xhr.status} ${xhr.statusText}`));
// Get the token from the upload URL
// Assuming the uploadUrl format has a token part after the last '/'
const token = uploadUrl.split("/").pop();
if (!token) {
reject(new Error("Invalid upload URL - missing token"));
return;
}
};
xhr.onerror = () =>
reject(new Error("Network error during file upload."));
xhr.open("PUT", uploadUrl);
xhr.setRequestHeader("Content-Type", file.type);
xhr.send(file);
// Create a progress tracker
const onProgress = (progressEvent: any) => {
const progress =
progressEvent.loaded && progressEvent.total
? Math.round((progressEvent.loaded / progressEvent.total) * 100)
: 0;
setUploadState((prev) => ({ ...prev, progress }));
};
// Call the SDK's upload method
uploadFileSdk({
path: { token },
body: { file },
onUploadProgress: onProgress,
})
.then(() => {
resolve();
})
.catch((error: any) => {
reject(
new Error(`Upload failed: ${error.message || "Unknown error"}`),
);
});
} catch (error) {
reject(new Error(`Network error during file upload: ${error}`));
}
});
},
[],