Spaces:
Sleeping
Sleeping
| from flask import Flask, request, send_file, jsonify | |
| import subprocess, os, shutil, uuid, base64 | |
| app = Flask(__name__) | |
| def add_cors(response): | |
| response.headers['Access-Control-Allow-Origin'] = '*' | |
| response.headers['Access-Control-Allow-Methods'] = 'POST, GET, OPTIONS' | |
| response.headers['Access-Control-Allow-Headers'] = 'Content-Type' | |
| return response | |
| def index(): | |
| return jsonify({"status": "HtStudio APK Builder çalışıyor 🚀"}) | |
| def build(): | |
| if request.method == "OPTIONS": | |
| return jsonify({}), 200 | |
| data = request.get_json(force=True) | |
| html = data.get("html", "") | |
| name = data.get("name", "HtApp").strip().replace(" ", "_") | |
| pkg = data.get("pkg", "com.htstudio.app").strip() | |
| min_sdk = data.get("minSdk", "21") | |
| logo_b64 = data.get("logo", "") | |
| manifest = data.get("manifest", "") | |
| if not html.strip(): | |
| return jsonify({"error": "HTML boş"}), 400 | |
| job_id = str(uuid.uuid4())[:8] | |
| work_dir = f"/tmp/build_{job_id}" | |
| os.makedirs(work_dir, exist_ok=True) | |
| try: | |
| # HTML | |
| with open(f"{work_dir}/index.html", "w", encoding="utf-8") as f: | |
| f.write(html) | |
| # Logo — dosyaya yaz, argüman olarak geçirme | |
| has_logo = "0" | |
| if logo_b64 and logo_b64.strip(): | |
| try: | |
| if ',' in logo_b64: | |
| logo_b64 = logo_b64.split(',')[1] | |
| os.makedirs(f"{work_dir}/res/mipmap-hdpi", exist_ok=True) | |
| with open(f"{work_dir}/res/mipmap-hdpi/ic_launcher.png", "wb") as f: | |
| f.write(base64.b64decode(logo_b64)) | |
| has_logo = "1" | |
| except: | |
| has_logo = "0" | |
| # Özel manifest | |
| if manifest.strip(): | |
| with open(f"{work_dir}/custom_manifest.xml", "w", encoding="utf-8") as f: | |
| f.write(manifest) | |
| result = subprocess.run( | |
| ["bash", "/app/build_apk.sh", work_dir, name, pkg, | |
| "/opt/debug.keystore", min_sdk, has_logo], | |
| capture_output=True, text=True, timeout=120 | |
| ) | |
| print("=== STDOUT ==="); print(result.stdout) | |
| print("=== STDERR ==="); print(result.stderr) | |
| if result.returncode != 0: | |
| return jsonify({"error": result.stderr[-1000:] or result.stdout[-1000:]}), 500 | |
| apk_path = f"{work_dir}/output.apk" | |
| if not os.path.exists(apk_path): | |
| return jsonify({"error": "APK dosyası oluşturulamadı"}), 500 | |
| return send_file( | |
| apk_path, | |
| as_attachment=True, | |
| download_name=f"{name}_HtStudio.apk", | |
| mimetype="application/vnd.android.package-archive" | |
| ) | |
| except Exception as e: | |
| return jsonify({"error": str(e)}), 500 | |
| finally: | |
| shutil.rmtree(work_dir, ignore_errors=True) | |
| if __name__ == "__main__": | |
| app.run(host="0.0.0.0", port=7860) | |