Skip to content

Commit e09dfc7

Browse files
committed
fix(ui/file-upload): Disable body parser to allow .gz files to be uploaded
stegano/next-http-proxy-middleware#33
1 parent 5079c8d commit e09dfc7

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed

components/FileUpload/FileUpload.tsx

+8-1
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,14 @@ export const FileUpload = () => {
2020

2121
const [mimeTypeFormDatas, setMimeTypeFormDatas] = useState<FileTypeOptionsState>({});
2222

23-
const { isLoading: isTypesLoading } = useGetFileTypes(); // Ensure types are prefetched to mime lookup works
23+
// Ensure types are prefetched to mime lookup works
24+
const { isLoading: isTypesLoading } = useGetFileTypes();
2425

2526
const uploadFiles = () => {
2627
files
2728
.filter((file) => !file.done)
2829
.forEach(async ({ file, rename, mimeType }, index) => {
30+
// Prepare the payload for the post request
2931
const data: DatasetPostBodyBody = {
3032
dataset_file: file,
3133
dataset_type: mimeType,
@@ -37,6 +39,7 @@ export const FileUpload = () => {
3739
};
3840

3941
try {
42+
// Can't use the mutate dataset hook here as it doesn't allow a onUploadProgress callback
4043
const response = await uploadDataset(data, {
4144
onUploadProgress: (progressEvent: ProgressEvent) => {
4245
const progress = Math.floor((progressEvent.loaded * 100) / progressEvent.total);
@@ -46,6 +49,8 @@ export const FileUpload = () => {
4649
setFiles(updatedFiles);
4750
},
4851
});
52+
53+
// A successful request will have a taskId
4954
if (response.task_id) {
5055
const updatedFiles = [...files];
5156
updatedFiles[index].taskId = response.task_id;
@@ -56,13 +61,15 @@ export const FileUpload = () => {
5661
if (error.isAxiosError) {
5762
const data = error.response?.data;
5863

64+
// Add the error to the array of files
5965
const updatedFiles = [...files];
6066
updatedFiles[index].errors.push({
6167
message: data?.error ?? 'Unknown error',
6268
code: (error.response?.status ?? '').toString(),
6369
});
6470
setFiles(updatedFiles);
6571
}
72+
throw err;
6673
}
6774
});
6875
};

components/Uploads/Dropzone.tsx

+8-2
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,14 @@ export const Dropzone: React.FC<DropzoneProps> = ({
5050
);
5151

5252
const patchedFileExtensions = allowedFileTypes ?? [];
53-
// TODO: allow gzipped files to be uploaded
54-
// patchedFileExtensions.push('.gz');
53+
54+
// Allow .gz files
55+
// 1. Browsers limit file selection in native OS file selector to single extensions
56+
// So .sdf.gz doesn't work but .gz specifies anything ending in .gz
57+
//
58+
// 2. This currently requires the body parser in the proxy to be disabled
59+
// https://github.com/stegano/next-http-proxy-middleware/issues/33
60+
patchedFileExtensions.push('.gz');
5561
const { getRootProps, getInputProps, isDragActive } = useDropzone({
5662
...dropzoneOptions,
5763
onDrop,

pages/api/dm-api/[...dm-proxy].ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,7 @@ export default withApiAuthRequired(async (req, res) => {
2828

2929
export const config = {
3030
api: {
31-
bodyParser: {
32-
sizeLimit: `${process.env.NEXT_PUBLIC_MAX_UPLOAD_SIZE ?? 25}mb`,
33-
},
31+
bodyParser: false,
3432
externalResolver: true, // Prevents noise created by proxy
3533
},
3634
};

0 commit comments

Comments
 (0)