File size: 3,259 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
<?php
// /api/upload.php
require_once __DIR__ . '/utils/Config.php';
require_once __DIR__ . '/utils/Response.php';
require_once __DIR__ . '/services/FileValidator.php';
require_once __DIR__ . '/services/ModrinthService.php';
require_once __DIR__ . '/services/SupabaseService.php';

// تحميل الإعدادات
Config::load(__DIR__ . '/../.env');

// التأكد من أن الطلب POST
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
    Response::error('يُسمح بطلبات POST فقط.', 405);
}

// استلام البيانات من FormData
$title = $_POST['addonTitle'] ?? null;
$description = $_POST['shortDescription'] ?? null;
$gameVersions = isset($_POST['gameVersions']) ? json_decode($_POST['gameVersions'], true) : [];
$projectType = $_POST['projectType'] ?? 'mod';
$loaderType = $_POST['loaderType'] ?? 'fabric'; // افتراضي لـ Java
$file = $_FILES['addonFile'] ?? null;

// التحقق الأولي
if (!$title || !$description || empty($gameVersions) || !$file) {
    Response::error('يرجى إكمال جميع الحقول المطلوبة.');
}

try {
    // 1. التحقق من الملف
    $validator = new FileValidator();
    $basicCheck = $validator->validateBasic($file);
    if (!$basicCheck['valid']) {
        Response::error($basicCheck['error']);
    }

    $contentCheck = $validator->validateContent($file['tmp_name'], $basicCheck['extension']);
    if (!$contentCheck['valid']) {
        Response::error($contentCheck['error']);
    }

    // 2. إنشاء مشروع على Modrinth
    $modrinth = new ModrinthService(Config::get('MODRINTH_TOKEN'));
    $project = $modrinth->createProject([
        'title' => $title,
        'description' => $description,
        'project_type' => $projectType,
        'categories' => [$loaderType] // يمكن إضافة المزيد حسب الحاجة
    ]);

    $modrinthProjectId = $project['id'];
    $modrinthSlug = $project['slug'];

    // 3. رفع النسخة الأولى للملف
    $version = $modrinth->createVersion($modrinthProjectId, [
        'name' => $title . ' v1.0.0',
        'version_number' => '1.0.0',
        'game_versions' => $gameVersions,
        'loaders' => [$loaderType]
    ], $file['tmp_name']);

    // 4. حفظ البيانات في Supabase
    $supabase = new SupabaseService(
        Config::get('SUPABASE_URL'),
        Config::get('SUPABASE_SERVICE_KEY')
    );

    $supabaseData = [
        'title' => $title,
        'description' => $description,
        'modrinth_id' => $modrinthProjectId,
        'modrinth_slug' => $modrinthSlug,
        'project_type' => $projectType,
        'upload_status' => 'unlisted',
        'file_size' => $file['size'],
        'minecraft_versions' => $gameVersions
    ];

    $saved = $supabase->saveAddon($supabaseData);

    // نجاح!
    Response::success([
        'message' => 'تم الرفع والربط بنجاح!',
        'modrinth_url' => "https://modrinth.com/{$projectType}/{$modrinthSlug}",
        'project_id' => $modrinthProjectId
    ]);

} catch (Exception $e) {
    // تسجيل الخطأ في السجل (اختياري)
    error_log($e->getMessage());
    Response::error('حدث خطأ أثناء المعالجة: ' . $e->getMessage(), 500);
}