Download src/hooks/useFileDownload.ts from felix-fx/fx-1: direct link, hf CLI and curl.
- Browser
- Download file 4.23 kB
-
https://huggingface.co/felix-fx/fx-1/resolve/main/src/hooks/useFileDownload.ts
- Command line
-
hf download hf://felix-fx/fx-1/src/hooks/useFileDownload.ts
-
curl -L -o useFileDownload.ts https://huggingface.co/felix-fx/fx-1/resolve/main/src/hooks/useFileDownload.ts
4.23 kB
| import { useState } from 'react'; | |
| import { toast } from 'sonner'; | |
| import { supabase } from '@/integrations/supabase/client'; | |
| // الحصول على الامتداد الصحيح حسب المنصة | |
| const getCorrectExtension = (platform: string, originalExtension: string): string => { | |
| // إذا كانت المنصة Bedrock، تأكد من أن الامتداد صحيح | |
| if (platform === 'bedrock') { | |
| // إذا كان الامتداد .jar (خطأ شائع)، حوله للامتداد الصحيح | |
| if (originalExtension === 'jar') { | |
| return 'mcaddon'; // الامتداد الافتراضي لـ Bedrock | |
| } | |
| // الامتدادات الصحيحة لـ Bedrock | |
| const bedrockExtensions = ['mcaddon', 'mcpack', 'mcworld', 'mctemplate', 'zip']; | |
| if (!bedrockExtensions.includes(originalExtension)) { | |
| return 'mcaddon'; | |
| } | |
| } | |
| // إذا كانت المنصة Java | |
| if (platform === 'java') { | |
| const javaExtensions = ['jar', 'zip']; | |
| if (!javaExtensions.includes(originalExtension)) { | |
| return 'jar'; | |
| } | |
| } | |
| return originalExtension; | |
| }; | |
| // إنشاء اسم الملف الصحيح | |
| const createCorrectFilename = (title: string, platform: string, originalExtension: string): string => { | |
| // تنظيف اسم الملف | |
| const cleanTitle = title | |
| .replace(/[^a-zA-Z0-9\u0600-\u06FF\s-_]/g, '') | |
| .replace(/\s+/g, '-') | |
| .trim() || 'addon'; | |
| const correctExtension = getCorrectExtension(platform, originalExtension); | |
| return `${cleanTitle}.${correctExtension}`; | |
| }; | |
| export const useFileDownload = () => { | |
| const [downloading, setDownloading] = useState(false); | |
| const downloadFile = async ( | |
| fileUrl: string, | |
| title: string, | |
| platform: string, | |
| fileType: string, | |
| modId?: string, | |
| userId?: string | null | |
| ) => { | |
| setDownloading(true); | |
| try { | |
| // تسجيل التحميل في قاعدة البيانات | |
| if (modId) { | |
| await supabase.from('download_history').insert({ | |
| mod_id: modId, | |
| user_id: userId || null, | |
| }); | |
| // تحديث عداد التحميلات | |
| const { data: mod } = await supabase | |
| .from('mods') | |
| .select('downloads') | |
| .eq('id', modId) | |
| .single(); | |
| if (mod) { | |
| await supabase | |
| .from('mods') | |
| .update({ downloads: (mod.downloads || 0) + 1 }) | |
| .eq('id', modId); | |
| } | |
| } | |
| // جلب الملف | |
| const response = await fetch(fileUrl); | |
| if (!response.ok) throw new Error('فشل في جلب الملف'); | |
| const blob = await response.blob(); | |
| // إنشاء اسم الملف الصحيح | |
| const correctFilename = createCorrectFilename(title, platform, fileType); | |
| // تحديد نوع MIME الصحيح | |
| let mimeType = 'application/octet-stream'; | |
| const extension = correctFilename.split('.').pop()?.toLowerCase(); | |
| if (extension === 'jar') { | |
| mimeType = 'application/java-archive'; | |
| } else if (extension === 'zip') { | |
| mimeType = 'application/zip'; | |
| } else if (['mcaddon', 'mcpack', 'mcworld', 'mctemplate'].includes(extension || '')) { | |
| mimeType = 'application/octet-stream'; | |
| } | |
| // إنشاء Blob جديد بالنوع الصحيح | |
| const correctBlob = new Blob([blob], { type: mimeType }); | |
| // إنشاء رابط التحميل | |
| const downloadUrl = URL.createObjectURL(correctBlob); | |
| const link = document.createElement('a'); | |
| link.href = downloadUrl; | |
| link.download = correctFilename; | |
| // تنفيذ التحميل | |
| document.body.appendChild(link); | |
| link.click(); | |
| document.body.removeChild(link); | |
| // تنظيف | |
| URL.revokeObjectURL(downloadUrl); | |
| toast.success(`تم بدء تحميل: ${correctFilename} 🎉`); | |
| return true; | |
| } catch (error) { | |
| console.error('Download error:', error); | |
| toast.error('فشل في تحميل الملف'); | |
| return false; | |
| } finally { | |
| setDownloading(false); | |
| } | |
| }; | |
| return { | |
| downloading, | |
| downloadFile | |
| }; | |
| }; | |
| export default useFileDownload; | |