Skip to content

Commit

Permalink
fix: ensure uploaded files are valid
Browse files Browse the repository at this point in the history
  • Loading branch information
ctrlc03 committed Jul 4, 2024
1 parent 86228a0 commit f86d43f
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions src/hooks/useMetadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,21 @@ export function useMetadata<T>(metadataPtr?: string): UseTRPCQueryResult<T, unkn
export function useUploadMetadata(): UseMutationResult<{ url: string }, DefaultError, Record<string, unknown> | File> {
return useMutation({
mutationFn: async (data: Record<string, unknown> | File) => {
const formData = new FormData();

let uploadData;
if (!(data instanceof File)) {
const blob = new Blob([JSON.stringify(data)], {
type: "application/json",
});

formData.append("file", new File([blob], "metadata.json"));
uploadData = new File([blob], "metadata.json");
} else {
formData.append("file", data);
uploadData = data;
}

return fetch(`/api/blob?filename=${data instanceof File ? data.name : "metadata.json"}`, {
return fetch(`/api/blob?filename=${uploadData.name}`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: formData,
body: uploadData,
}).then(async (r) => {
if (!r.ok) {
throw new Error("Network error");
Expand Down

0 comments on commit f86d43f

Please sign in to comment.