feat: improve media handling

added local range requests, fix mime type detection, improve uploading using FormData, correctly use mime type allow list, added previews for audio, pdf and text
This commit is contained in:
dswbx
2025-09-29 14:55:19 +02:00
parent 0d74625270
commit 55082e9d0e
14 changed files with 255 additions and 26 deletions

View File

@@ -42,7 +42,10 @@ export type DropzoneRenderProps = {
showPlaceholder: boolean;
onClick?: (file: { path: string }) => void;
footer?: ReactNode;
dropzoneProps: Pick<DropzoneProps, "maxItems" | "placeholder" | "autoUpload" | "flow">;
dropzoneProps: Pick<
DropzoneProps,
"maxItems" | "placeholder" | "autoUpload" | "flow" | "allowedMimeTypes"
>;
};
export type DropzoneProps = {
@@ -151,6 +154,7 @@ export function Dropzone({
const setIsOver = useStore(store, (state) => state.setIsOver);
const uploading = useStore(store, (state) => state.uploading);
const setFileState = useStore(store, (state) => state.setFileState);
const overrideFile = useStore(store, (state) => state.overrideFile);
const removeFile = useStore(store, (state) => state.removeFile);
const inputRef = useRef<HTMLInputElement>(null);
@@ -359,7 +363,7 @@ export function Dropzone({
state: "uploaded",
};
setFileState(file.path, newState.state);
overrideFile(file.path, newState);
resolve({ ...response, ...file, ...newState });
} catch (e) {
setFileState(file.path, "uploaded", 1);
@@ -382,7 +386,9 @@ export function Dropzone({
};
xhr.setRequestHeader("Accept", "application/json");
xhr.send(file.body);
const formData = new FormData();
formData.append("file", file.body);
xhr.send(formData);
});
}
@@ -411,7 +417,9 @@ export function Dropzone({
const openFileInput = useCallback(() => inputRef.current?.click(), [inputRef]);
const showPlaceholder = useMemo(
() =>
Boolean(placeholder?.show === true || !maxItems || (maxItems && files.length < maxItems)),
Boolean(
placeholder?.show !== false && (!maxItems || (maxItems && files.length < maxItems)),
),
[placeholder, maxItems, files.length],
);
@@ -437,11 +445,12 @@ export function Dropzone({
placeholder,
autoUpload,
flow,
allowedMimeTypes,
},
onClick,
footer,
}),
[maxItems, flow, placeholder, autoUpload, footer],
[maxItems, files.length, flow, placeholder, autoUpload, footer, allowedMimeTypes],
) as unknown as DropzoneRenderProps;
return (