File size: 4,232 Bytes
6711fd5 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 | 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;
|