fix: use globalThis.fetch for image upload on Android

expoFetch does not support the React Native FormData { uri, name, type }
pattern. Switch upload request to globalThis.fetch which handles it
correctly. Also propagate upload errors instead of swallowing them.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-05-16 21:18:29 +02:00
parent da31d47061
commit 69b94ab7c0
+10 -13
View File
@@ -77,33 +77,32 @@ export default function ComposeScreen() {
const removeImage = () => setImageUri(null); const removeImage = () => setImageUri(null);
const uploadImage = async (): Promise<PostizUploadResult | null> => { const uploadImage = async (): Promise<PostizUploadResult> => {
if (!imageUri) return null;
setUploading(true); setUploading(true);
try { try {
const formData = new FormData(); const formData = new FormData();
if (Platform.OS === "web") { if (Platform.OS === "web") {
const response = await expoFetch(imageUri); const response = await expoFetch(imageUri!);
const blob = await response.blob(); const blob = await response.blob();
formData.append("file", blob, "upload.jpg"); formData.append("file", blob, "upload.jpg");
} else { } else {
formData.append("file", { formData.append("file", {
uri: imageUri, uri: imageUri!,
name: "upload.jpg", name: "upload.jpg",
type: "image/jpeg", type: "image/jpeg",
} as unknown as Blob); } as unknown as Blob);
} }
const uploadRes = await expoFetch(`${baseUrl}/upload`, { // eslint-disable-next-line no-undef
const uploadRes = await globalThis.fetch(`${baseUrl}/upload`, {
method: "POST", method: "POST",
headers: { Authorization: apiKey }, headers: { Authorization: apiKey },
body: formData, body: formData,
}); });
const data = await uploadRes.json() as PostizUploadResult; if (!uploadRes.ok) {
return data; const raw = await uploadRes.text().catch(() => uploadRes.statusText);
} catch (e: unknown) { throw new Error(`Upload HTTP ${uploadRes.status}: ${raw.slice(0, 200)}`);
const msg = e instanceof Error ? e.message : String(e); }
Alert.alert("Upload Failed", `Could not upload image.\n${msg}`); return await uploadRes.json() as PostizUploadResult;
return null;
} finally { } finally {
setUploading(false); setUploading(false);
} }
@@ -125,10 +124,8 @@ export default function ComposeScreen() {
let media: Array<{ id: string; path: string }> = []; let media: Array<{ id: string; path: string }> = [];
if (imageUri) { if (imageUri) {
const uploaded = await uploadImage(); const uploaded = await uploadImage();
if (uploaded) {
media = [{ id: uploaded.id, path: uploaded.path }]; media = [{ id: uploaded.id, path: uploaded.path }];
} }
}
const payload = { const payload = {
type: postNow ? "now" : "schedule", type: postNow ? "now" : "schedule",
date: postNow ? new Date().toISOString() : scheduleDate.toISOString(), date: postNow ? new Date().toISOString() : scheduleDate.toISOString(),