Statt nur Spinner: „Upload wird vorbereitet …“, dann Balken + Prozent aus expo-file-system File.upload onProgress (nativ seit 57.0.7 im 1.0.6-Build, daher OTA). Vorschau ist während des Uploads nicht mehr antippbar. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
53 lines
1.8 KiB
TypeScript
53 lines
1.8 KiB
TypeScript
import { File } from "expo-file-system";
|
|
|
|
/**
|
|
* PUTs a local picked-image file directly to the Supabase Storage presigned
|
|
* URL returned by `posts.getPresignedUrl` — mirrors web's XHR PUT in
|
|
* `src/lib/upload-xhr.ts`, using expo-file-system's `File.upload` instead
|
|
* since RN has no XHR-with-progress primitive by default. Content-Type must
|
|
* exactly match what was passed to `getPresignedUrl` or Supabase responds
|
|
* with 400 SignatureDoesNotMatch.
|
|
*/
|
|
export async function uploadImageToSupabase(uri: string, presignedUrl: string, mimeType: string): Promise<void> {
|
|
const file = new File(uri);
|
|
const result = await file.upload(presignedUrl, {
|
|
httpMethod: "PUT",
|
|
mimeType,
|
|
headers: { "Content-Type": mimeType },
|
|
});
|
|
|
|
if (result.status < 200 || result.status >= 300) {
|
|
throw new Error(`Upload fehlgeschlagen (Status ${result.status})`);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* PUTs a local video file directly to a Mux direct-upload URL (from
|
|
* `stories.createVideoUpload`). Separate function rather than reusing
|
|
* `uploadImageToSupabase` above — same `File.upload` primitive, but a
|
|
* different destination (Mux, not Supabase) and no signature-matching
|
|
* requirement on Content-Type, so keeping the working photo path untouched.
|
|
*/
|
|
export async function uploadFileToUrl(
|
|
uri: string,
|
|
uploadUrl: string,
|
|
mimeType: string,
|
|
onProgress?: (fraction: number) => void
|
|
): Promise<void> {
|
|
const file = new File(uri);
|
|
const result = await file.upload(uploadUrl, {
|
|
httpMethod: "PUT",
|
|
mimeType,
|
|
headers: { "Content-Type": mimeType },
|
|
onProgress: onProgress
|
|
? ({ bytesSent, totalBytes }) => {
|
|
if (totalBytes > 0) onProgress(Math.min(bytesSent / totalBytes, 1));
|
|
}
|
|
: undefined,
|
|
});
|
|
|
|
if (result.status < 200 || result.status >= 300) {
|
|
throw new Error(`Video-Upload fehlgeschlagen (Status ${result.status})`);
|
|
}
|
|
}
|