Download src/hooks/useFileUpload.ts from felix-fx/fx-craft: direct link, hf CLI and curl.
- Browser
- Download file 5.01 kB
-
https://huggingface.co/spaces/felix-fx/fx-craft/resolve/main/src/hooks/useFileUpload.ts
- Command line
-
hf download hf://spaces/felix-fx/fx-craft/src/hooks/useFileUpload.ts
-
curl -L -o useFileUpload.ts https://huggingface.co/spaces/felix-fx/fx-craft/resolve/main/src/hooks/useFileUpload.ts
5.01 kB
| import { useState } from 'react'; | |
| import { useAuth } from '@/contexts/AuthContext'; | |
| import { supabase } from '@/integrations/supabase/client'; | |
| import { toast } from 'sonner'; | |
| interface UploadProgress { | |
| loaded: number; | |
| total: number; | |
| percentage: number; | |
| } | |
| interface UploadResult { | |
| url: string; | |
| path: string; | |
| size: number; | |
| } | |
| interface ModUploadData { | |
| title: string; | |
| description: string; | |
| category: string; | |
| platform: 'java' | 'bedrock' | 'both'; | |
| minecraft_version: string; | |
| mod_version: string; | |
| tags: string[]; | |
| file: File; | |
| thumbnail?: File; | |
| screenshots?: File[]; | |
| } | |
| export const useFileUpload = () => { | |
| const { user } = useAuth(); | |
| const [uploading, setUploading] = useState(false); | |
| const [progress, setProgress] = useState<UploadProgress>({ loaded: 0, total: 0, percentage: 0 }); | |
| const getFileType = (file: File): string => { | |
| const extension = file.name.split('.').pop()?.toLowerCase() || ''; | |
| const typeMap: Record<string, string> = { | |
| 'jar': 'jar', | |
| 'zip': 'zip', | |
| 'mcaddon': 'mcaddon', | |
| 'mcpack': 'mcpack', | |
| 'mcworld': 'mcworld', | |
| 'mctemplate': 'mctemplate', | |
| }; | |
| return typeMap[extension] || 'zip'; | |
| }; | |
| const uploadFile = async ( | |
| file: File, | |
| bucket: string, | |
| _folder: string | |
| ): Promise<UploadResult> => { | |
| if (!user) throw new Error('يجب تسجيل الدخول أولاً'); | |
| const fileExt = file.name.split('.').pop(); | |
| const uniqueId = typeof crypto.randomUUID === 'function' | |
| ? crypto.randomUUID() | |
| : Math.random().toString(36).substring(2) + Date.now().toString(36); | |
| const fileName = `${user.id}/${Date.now()}-${uniqueId}.${fileExt}`; | |
| const { data, error } = await supabase.storage | |
| .from(bucket) | |
| .upload(fileName, file, { | |
| cacheControl: '3600', | |
| upsert: false, | |
| }); | |
| if (error) throw error; | |
| const { data: urlData } = supabase.storage | |
| .from(bucket) | |
| .getPublicUrl(data.path); | |
| return { | |
| url: urlData.publicUrl, | |
| path: data.path, | |
| size: file.size, | |
| }; | |
| }; | |
| const uploadMod = async (modData: ModUploadData): Promise<string> => { | |
| if (!user) { | |
| toast.error('يجب تسجيل الدخول لرفع الإضافات'); | |
| throw new Error('Not authenticated'); | |
| } | |
| setUploading(true); | |
| setProgress({ loaded: 0, total: 100, percentage: 0 }); | |
| try { | |
| toast.loading('جاري رفع الملف...', { id: 'upload' }); | |
| setProgress({ loaded: 20, total: 100, percentage: 20 }); | |
| const modFile = await uploadFile(modData.file, 'mods', user.id); | |
| let thumbnailUrl = null; | |
| if (modData.thumbnail) { | |
| setProgress({ loaded: 40, total: 100, percentage: 40 }); | |
| const thumbnail = await uploadFile(modData.thumbnail, 'thumbnails', user.id); | |
| thumbnailUrl = thumbnail.url; | |
| } | |
| const screenshotUrls: string[] = []; | |
| if (modData.screenshots && modData.screenshots.length > 0) { | |
| setProgress({ loaded: 60, total: 100, percentage: 60 }); | |
| for (const screenshot of modData.screenshots) { | |
| const uploaded = await uploadFile(screenshot, 'screenshots', user.id); | |
| screenshotUrls.push(uploaded.url); | |
| } | |
| } | |
| setProgress({ loaded: 80, total: 100, percentage: 80 }); | |
| const { data: mod, error } = await supabase | |
| .from('mods') | |
| .insert({ | |
| user_id: user.id, | |
| title: modData.title, | |
| description: modData.description, | |
| category: modData.category, | |
| platform: modData.platform, | |
| minecraft_version: modData.minecraft_version, | |
| mod_version: modData.mod_version, | |
| tags: modData.tags, | |
| file_url: modFile.url, | |
| file_type: getFileType(modData.file), | |
| file_size: modFile.size, | |
| thumbnail_url: thumbnailUrl, | |
| screenshots: screenshotUrls, | |
| is_approved: false, | |
| }) | |
| .select('id') | |
| .single(); | |
| if (error) throw error; | |
| const { count: modsCount } = await supabase | |
| .from('mods') | |
| .select('id', { count: 'exact', head: true }) | |
| .eq('user_id', user.id); | |
| if (modsCount !== null) { | |
| await supabase | |
| .from('profiles') | |
| .update({ total_uploads: modsCount }) | |
| .eq('user_id', user.id); | |
| } | |
| setProgress({ loaded: 100, total: 100, percentage: 100 }); | |
| toast.success('تم رفع الإضافة بنجاح! 🎉', { id: 'upload' }); | |
| return mod.id; | |
| } catch (error: any) { | |
| console.error('Upload error:', error); | |
| toast.error(error.message || 'فشل في رفع الإضافة', { id: 'upload' }); | |
| throw error; | |
| } finally { | |
| setUploading(false); | |
| } | |
| }; | |
| const deleteFile = async (bucket: string, path: string) => { | |
| const { error } = await supabase.storage.from(bucket).remove([path]); | |
| if (error) throw error; | |
| }; | |
| return { | |
| uploadMod, | |
| uploadFile, | |
| deleteFile, | |
| uploading, | |
| progress, | |
| }; | |
| }; | |