File size: 6,889 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// أنواع ملفات ماينكرافت المدعومة حسب المنصة
export interface FileTypeInfo {
  extension: string;
  name: string;
  description: string;
  platform: 'java' | 'bedrock' | 'both';
  mimeTypes: string[];
}

export const FILE_TYPES: FileTypeInfo[] = [
  // Java Edition
  {
    extension: '.jar',
    name: 'Java Mod',
    description: 'ملف مود جافا (يتطلب Forge أو Fabric)',
    platform: 'java',
    mimeTypes: ['application/java-archive', 'application/x-java-archive'],
  },
  {
    extension: '.zip',
    name: 'Resource Pack / Data Pack',
    description: 'حزمة موارد أو حزمة بيانات',
    platform: 'both',
    mimeTypes: ['application/zip', 'application/x-zip-compressed'],
  },
  // Bedrock Edition
  {
    extension: '.mcaddon',
    name: 'Bedrock Add-On',
    description: 'إضافة بيدروك كاملة (سلوك + موارد)',
    platform: 'bedrock',
    mimeTypes: ['application/octet-stream'],
  },
  {
    extension: '.mcpack',
    name: 'Bedrock Pack',
    description: 'حزمة موارد أو سلوك لبيدروك',
    platform: 'bedrock',
    mimeTypes: ['application/octet-stream'],
  },
  {
    extension: '.mcworld',
    name: 'Bedrock World',
    description: 'خريطة/عالم ماينكرافت بيدروك',
    platform: 'bedrock',
    mimeTypes: ['application/octet-stream'],
  },
  {
    extension: '.mctemplate',
    name: 'Bedrock Template',
    description: 'قالب عالم بيدروك',
    platform: 'bedrock',
    mimeTypes: ['application/octet-stream'],
  },
  {
    extension: '.mcfunction',
    name: 'Function File',
    description: 'ملف وظائف ماينكرافت',
    platform: 'both',
    mimeTypes: ['text/plain'],
  },
];

// الحصول على امتدادات الملفات حسب المنصة
export const getAcceptedExtensions = (platform: 'java' | 'bedrock' | 'both'): string[] => {
  if (platform === 'both') {
    return FILE_TYPES.map(ft => ft.extension);
  }
  
  return FILE_TYPES
    .filter(ft => ft.platform === platform || ft.platform === 'both')
    .map(ft => ft.extension);
};

// الحصول على نوع الملف من الامتداد
export const getFileTypeFromExtension = (filename: string): FileTypeInfo | undefined => {
  const extension = filename.toLowerCase().substring(filename.lastIndexOf('.'));
  return FILE_TYPES.find(ft => ft.extension === extension);
};

// التحقق من توافق الملف مع المنصة
export const isFileCompatibleWithPlatform = (filename: string, platform: 'java' | 'bedrock' | 'both'): boolean => {
  const fileType = getFileTypeFromExtension(filename);
  if (!fileType) return false;
  
  if (platform === 'both') return true;
  if (fileType.platform === 'both') return true;
  
  return fileType.platform === platform;
};

// الحصول على نوع الملف للتخزين في قاعدة البيانات
export const getFileTypeForStorage = (filename: string): string => {
  const extension = filename.toLowerCase().substring(filename.lastIndexOf('.'));
  // إزالة النقطة من البداية
  return extension.replace('.', '');
};

// التحقق من صحة الملف
export const validateFile = (file: File, platform: 'java' | 'bedrock' | 'both'): {
  valid: boolean;
  error?: string;
  fileType?: FileTypeInfo;
} => {
  const maxSize = 100 * 1024 * 1024; // 100 MB
  
  if (file.size > maxSize) {
    return {
      valid: false,
      error: 'حجم الملف كبير جداً. الحد الأقصى 100 ميجابايت',
    };
  }
  
  const fileType = getFileTypeFromExtension(file.name);
  if (!fileType) {
    return {
      valid: false,
      error: 'نوع الملف غير مدعوم. الأنواع المدعومة: ' + FILE_TYPES.map(ft => ft.extension).join(', '),
    };
  }
  
  if (!isFileCompatibleWithPlatform(file.name, platform)) {
    const compatibleTypes = getAcceptedExtensions(platform);
    return {
      valid: false,
      error: `هذا النوع من الملفات غير متوافق مع منصة ${platform === 'java' ? 'جافا' : 'بيدروك'}. الأنواع المتوافقة: ${compatibleTypes.join(', ')}`,
    };
  }
  
  return {
    valid: true,
    fileType,
  };
};

// عرض حجم الملف بشكل مقروء
export const formatFileSize = (bytes: number): string => {
  if (bytes === 0) return '0 Bytes';
  const k = 1024;
  const sizes = ['Bytes', 'KB', 'MB', 'GB'];
  const i = Math.floor(Math.log(bytes) / Math.log(k));
  return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
};

// الحصول على أيقونة نوع الملف
export const getFileTypeIcon = (extension: string): string => {
  const iconMap: Record<string, string> = {
    '.jar': '☕',
    '.zip': '📦',
    '.mcaddon': '🎮',
    '.mcpack': '📋',
    '.mcworld': '🌍',
    '.mctemplate': '📄',
    '.mcfunction': '⚙️',
  };
  return iconMap[extension] || '📁';
};

// الحصول على لون نوع الملف
export const getFileTypeColor = (extension: string): string => {
  const colorMap: Record<string, string> = {
    '.jar': 'text-orange-500',
    '.zip': 'text-blue-500',
    '.mcaddon': 'text-green-500',
    '.mcpack': 'text-purple-500',
    '.mcworld': 'text-cyan-500',
    '.mctemplate': 'text-yellow-500',
    '.mcfunction': 'text-gray-500',
  };
  return colorMap[extension] || 'text-muted-foreground';
};

// الحصول على الامتداد الصحيح حسب المنصة
export const getCorrectFileExtension = (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;
};

// إنشاء اسم الملف الصحيح للتحميل
export 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 = getCorrectFileExtension(platform, originalExtension);
  return `${cleanTitle}.${correctExtension}`;
};