diff --git a/app/api.py b/app/api.py index 9f4309c..aa3545d 100644 --- a/app/api.py +++ b/app/api.py @@ -330,31 +330,64 @@ def download_mod(): server_version = data['version'] base_path = f"./servers/mc-{username}/{'plugins' if server_type == 'paper' else 'mods'}" + os.makedirs(base_path, exist_ok=True) - downloaded = [] + downloaded_files = [] + visited_versions = set() def download_project(pid, version_id=None): if version_id: version_data = requests.get(f"https://api.modrinth.com/v2/version/{version_id}").json() else: all_versions = requests.get(f"https://api.modrinth.com/v2/project/{pid}/version").json() - version_data = next((v for v in all_versions if server_version in v['game_versions'] and server_type in v['loaders']), None) + version_data = next( + (v for v in all_versions + if server_version in v['game_versions'] + and server_type in v['loaders'] + and v.get('server_side') != 'unsupported'), + None) if not version_data: + print(f"[SKIP] No compatible version found for {pid}") return - file = version_data['files'][0] - file_path = f"{base_path}/{file['filename']}" - if file['filename'] not in downloaded: + if version_data["id"] in visited_versions: + return + visited_versions.add(version_data["id"]) + + if version_data.get('server_side') == 'unsupported': + print(f"[SKIP] {version_data.get('name', 'Nieznana nazwa')} is client-only") + return + + file = next((f for f in version_data['files'] if f.get('primary', True)), version_data['files'][0]) + file_path = os.path.join(base_path, file['filename']) + + if not os.path.exists(file_path): + print(f"[DOWNLOAD] {file['filename']}") with requests.get(file['url'], stream=True) as r: with open(file_path, 'wb') as f: for chunk in r.iter_content(chunk_size=8192): f.write(chunk) - downloaded.append(file['filename']) + + downloaded_files.append({ + "filename": file['filename'], + "name": version_data.get("name") or version_data.get("version_number") or "Nieznana wersja", + "project_id": version_data["project_id"], + "version_id": version_data["id"] + }) for dep in version_data.get('dependencies', []): if dep.get('version_id'): - download_project(dep['project_id'], dep['version_id']) + try: + download_project(dep['project_id'], dep['version_id']) + except Exception as e: + print(f"[ERROR] Failed to fetch dep version {dep['version_id']}: {e}") + elif dep.get('project_id'): + try: + download_project(dep['project_id']) + except Exception as e: + print(f"[ERROR] Failed to fetch dep project {dep['project_id']}: {e}") download_project(project_id) - return jsonify({"success": True, "downloaded": downloaded}) + return jsonify({"success": True, "downloaded": downloaded_files}) + diff --git a/app/static/js/mods.js b/app/static/js/mods.js index cd89b60..c6e5992 100644 --- a/app/static/js/mods.js +++ b/app/static/js/mods.js @@ -33,36 +33,36 @@ function searchMods() { } function installMod(projectId) { - fetch('/api/config') - .then(response => response.json()) - .then(configData => { - console.log(configData); - if (configData.success) { - const serverType = configData.type; - const serverVersion = configData.version; - if (serverType && serverVersion) { - fetch('/api/modrinth/download', { - method: 'POST', - headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify({ project_id: projectId, username, type: serverType, version: serverVersion }), - }) - .then(r => r.json()) - .then(data => { - if (data.success) { - alert(`Zainstalowano: ${data.file}`); - loadFileList(); - } else { - alert("Nie udało się zainstalować moda."); - } - }); - } else { - console.error('Server type or version not available'); - } + fetch('/api/config') + .then(response => response.json()) + .then(configData => { + if (configData.success) { + const serverType = configData.type; + const serverVersion = configData.version; + if (serverType && serverVersion) { + fetch('/api/modrinth/download', { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ project_id: projectId, username, type: serverType, version: serverVersion }), + }) + .then(r => r.json()) + .then(data => { + if (data.success) { + const modFiles = data.downloaded.map(mod => "- " + mod.filename).join("\n"); + alert(`Zainstalowano:\n${modFiles}`); + loadFileList(); + } else { + alert("Nie udało się zainstalować moda."); + } + }); } else { - console.error('Server config not found'); + console.error('Server type or version not available'); } - }) - .catch(err => { - console.error('Error fetching server config:', err); - }); + } else { + console.error('Server config not found'); + } + }) + .catch(err => { + console.error('Error fetching server config:', err); + }); } \ No newline at end of file