mirror of
https://github.com/bitsocialnet/5chan.git
synced 2026-08-03 07:41:04 +02:00
feat(media-hosting): add multi-provider configurable upload with fallback
Add Random/Preferred/None modes, preferred provider selection, and provider-order fallback. Catbox/Imgur/PostImages on Android via WebView; Electron CDP automation for Imgur/PostImages. Hide upload controls on web runtime.
This commit is contained in:
@@ -2,14 +2,17 @@ import { useCallback, useState } from 'react';
|
||||
import { Capacitor } from '@capacitor/core';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import FileUploader from '../plugins/file-uploader';
|
||||
import { uploadToCatbox } from '../lib/utils/catbox-utils';
|
||||
import { formatAggregatedError, formatPreferredModeError, type ProviderAttempt } from '../lib/media-hosting/error-format';
|
||||
import { getProviderOrder } from '../lib/media-hosting/provider-order';
|
||||
import { orchestrateElectronUpload } from '../lib/media-hosting/upload-orchestrator';
|
||||
import useMediaHostingStore from '../stores/use-media-hosting-store';
|
||||
|
||||
const FILE_SELECTION_CANCELLED_ERROR = 'File selection cancelled';
|
||||
|
||||
async function uploadByProvider(provider: string, file: File): Promise<string> {
|
||||
if (provider === 'catbox') return uploadToCatbox(file);
|
||||
throw new Error(`Unsupported media provider: ${provider}`);
|
||||
function getRuntime(): 'web' | 'electron' | 'android' {
|
||||
if (Capacitor.getPlatform() === 'android') return 'android';
|
||||
if (window.electronApi?.isElectron) return 'electron';
|
||||
return 'web';
|
||||
}
|
||||
|
||||
export interface UseFileUploadOptions {
|
||||
@@ -60,24 +63,25 @@ function selectFileViaInput(): Promise<File | null> {
|
||||
export function useFileUpload(options: UseFileUploadOptions) {
|
||||
const { t } = useTranslation();
|
||||
const { onUploadComplete } = options;
|
||||
const selectedProvider = useMediaHostingStore((s) => s.selectedProvider);
|
||||
const uploadMode = useMediaHostingStore((s) => s.uploadMode);
|
||||
const preferredProvider = useMediaHostingStore((s) => s.preferredProvider);
|
||||
const [isUploading, setIsUploading] = useState(false);
|
||||
const [uploadedFileName, setUploadedFileName] = useState<string | null>(null);
|
||||
|
||||
const handleUpload = useCallback(async () => {
|
||||
if (selectedProvider === 'none') return;
|
||||
if (selectedProvider !== 'catbox') return;
|
||||
if (uploadMode === 'none') return;
|
||||
|
||||
const runtime = getRuntime();
|
||||
const order = getProviderOrder({ mode: uploadMode, preferredProvider, runtime });
|
||||
|
||||
try {
|
||||
setIsUploading(true);
|
||||
setUploadedFileName(null);
|
||||
|
||||
if (Capacitor.getPlatform() === 'android') {
|
||||
const result = await FileUploader.pickAndUploadMedia();
|
||||
const result = await FileUploader.pickAndUploadMedia({ providerOrder: order });
|
||||
if (result.url) {
|
||||
if (result.fileName) {
|
||||
setUploadedFileName(result.fileName);
|
||||
}
|
||||
if (result.fileName) setUploadedFileName(result.fileName);
|
||||
onUploadComplete(result.url, result.fileName);
|
||||
}
|
||||
return;
|
||||
@@ -89,7 +93,7 @@ export function useFileUpload(options: UseFileUploadOptions) {
|
||||
throw new Error(FILE_SELECTION_CANCELLED_ERROR);
|
||||
}
|
||||
|
||||
const url = await uploadByProvider(selectedProvider, file);
|
||||
const url = await orchestrateElectronUpload(file, order);
|
||||
setUploadedFileName(file.name);
|
||||
onUploadComplete(url, file.name);
|
||||
return;
|
||||
@@ -98,13 +102,20 @@ export function useFileUpload(options: UseFileUploadOptions) {
|
||||
window.alert(t('upload_not_supported_web'));
|
||||
} catch (error) {
|
||||
const errorMessage = error instanceof Error ? error.message : String(error);
|
||||
if (errorMessage !== FILE_SELECTION_CANCELLED_ERROR) {
|
||||
if (errorMessage === FILE_SELECTION_CANCELLED_ERROR) return;
|
||||
|
||||
const err = error as Error & { attempts?: ProviderAttempt[] };
|
||||
if (err.attempts && err.attempts.length > 0) {
|
||||
window.alert(formatAggregatedError(err.attempts, t));
|
||||
} else if (uploadMode === 'preferred') {
|
||||
window.alert(formatPreferredModeError(errorMessage, t));
|
||||
} else {
|
||||
window.alert(`${t('upload_failed')}: ${errorMessage}`);
|
||||
}
|
||||
} finally {
|
||||
setIsUploading(false);
|
||||
}
|
||||
}, [onUploadComplete, t, selectedProvider]);
|
||||
}, [onUploadComplete, t, uploadMode, preferredProvider]);
|
||||
|
||||
return {
|
||||
isUploading,
|
||||
|
||||
Reference in New Issue
Block a user