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:
@@ -77,33 +77,32 @@ export default function ComposeScreen() {
|
||||
|
||||
const removeImage = () => setImageUri(null);
|
||||
|
||||
const uploadImage = async (): Promise<PostizUploadResult | null> => {
|
||||
if (!imageUri) return null;
|
||||
const uploadImage = async (): Promise<PostizUploadResult> => {
|
||||
setUploading(true);
|
||||
try {
|
||||
const formData = new FormData();
|
||||
if (Platform.OS === "web") {
|
||||
const response = await expoFetch(imageUri);
|
||||
const response = await expoFetch(imageUri!);
|
||||
const blob = await response.blob();
|
||||
formData.append("file", blob, "upload.jpg");
|
||||
} else {
|
||||
formData.append("file", {
|
||||
uri: imageUri,
|
||||
uri: imageUri!,
|
||||
name: "upload.jpg",
|
||||
type: "image/jpeg",
|
||||
} 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",
|
||||
headers: { Authorization: apiKey },
|
||||
body: formData,
|
||||
});
|
||||
const data = await uploadRes.json() as PostizUploadResult;
|
||||
return data;
|
||||
} catch (e: unknown) {
|
||||
const msg = e instanceof Error ? e.message : String(e);
|
||||
Alert.alert("Upload Failed", `Could not upload image.\n${msg}`);
|
||||
return null;
|
||||
if (!uploadRes.ok) {
|
||||
const raw = await uploadRes.text().catch(() => uploadRes.statusText);
|
||||
throw new Error(`Upload HTTP ${uploadRes.status}: ${raw.slice(0, 200)}`);
|
||||
}
|
||||
return await uploadRes.json() as PostizUploadResult;
|
||||
} finally {
|
||||
setUploading(false);
|
||||
}
|
||||
@@ -125,9 +124,7 @@ export default function ComposeScreen() {
|
||||
let media: Array<{ id: string; path: string }> = [];
|
||||
if (imageUri) {
|
||||
const uploaded = await uploadImage();
|
||||
if (uploaded) {
|
||||
media = [{ id: uploaded.id, path: uploaded.path }];
|
||||
}
|
||||
media = [{ id: uploaded.id, path: uploaded.path }];
|
||||
}
|
||||
const payload = {
|
||||
type: postNow ? "now" : "schedule",
|
||||
|
||||
Reference in New Issue
Block a user