From 2ca9dacb8b4f12a1b2e6de3b983fe04fc4360c04 Mon Sep 17 00:00:00 2001 From: enrilinux Date: Sun, 19 Jul 2026 21:52:02 +0200 Subject: [PATCH] fixed app.py --- .gitea/workflows/publish-package.yml | 53 ++++++++++++++-------- app/main.py | 68 +++++++++++++++++++--------- 2 files changed, 82 insertions(+), 39 deletions(-) diff --git a/.gitea/workflows/publish-package.yml b/.gitea/workflows/publish-package.yml index 2a5decb..de7070d 100644 --- a/.gitea/workflows/publish-package.yml +++ b/.gitea/workflows/publish-package.yml @@ -1,36 +1,53 @@ -name: build-and-push-docker +# .gitea/workflows/build.yaml +name: Build and Push Docker Image +# Il workflow viene attivato su push on: push: branches: - - main + - 'main' + - 'master' + +# Questa sezione è fondamentale per evitare build quando non necessario +# Gitea ignorerà automaticamente i commit che contengono queste stringhe [citation:10] +# Aggiungi "[skip ci]" al tuo messaggio di commit per saltare la build jobs: - build: + build-and-push: runs-on: ubuntu-latest - container: - image: docker:24-git - steps: - - name: Checkout codice + - name: Checkout code uses: actions/checkout@v4 - - name: Login al registry Gitea - uses: docker/login-action@v3 - with: - registry: gitea.enrilinux.ovh - username: ${{ secrets.REGISTRY_USER }} - password: ${{ secrets.REGISTRY_PASSWORD }} + # Questi due step preparano l'ambiente per build multi-architettura + - name: Set up QEMU + uses: docker/setup-qemu-action@v3 - - name: Setup Docker Buildx + - name: Set up Docker Buildx uses: docker/setup-buildx-action@v3 - - name: Build e push immagine + - name: Log in to Gitea Container Registry + uses: docker/login-action@v3 + with: + # IMPORTANTE: Sostituisci con il dominio del tuo Gitea (es. gitea.example.com) + registry: gitea.enrilinux.ovh + username: ${{ secrets.GITEA_USERNAME }} + password: ${{ secrets.GITEA_TOKEN }} + + # Estrae il nome del repository e il tag (commit SHA) per l'immagine + - name: Extract metadata + id: meta + run: | + echo "REPO_NAME=$(echo ${GITHUB_REPOSITORY} | awk -F'/' '{print $2}')" >> $GITHUB_OUTPUT + echo "SHORT_SHA=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT + + - name: Build and push Docker image uses: docker/build-push-action@v5 with: context: . push: true + # IMPORTANTE: Sostituisci "gitea.example.com" con il dominio del tuo Gitea + # e "username" con il tuo nome utente o organizzazione tags: | - gitea.enrilinux.ovh/enrilinux/webdlp:latest - gitea.enrilinux.ovh/enrilinux/webdlp:${{ github.sha }} - + gitea.enrilinux.ovh/${{ github.repository_owner }}/${{ steps.meta.outputs.REPO_NAME }}:latest + gitea.enrilinux.ovh/${{ github.repository_owner }}/${{ steps.meta.outputs.REPO_NAME }}:${{ steps.meta.outputs.SHORT_SHA }} diff --git a/app/main.py b/app/main.py index b9e2687..7f4df42 100644 --- a/app/main.py +++ b/app/main.py @@ -1,6 +1,5 @@ from fastapi import FastAPI, Request, Form from fastapi.responses import HTMLResponse, FileResponse, JSONResponse -from fastapi.templating import Jinja2Templates import os import json from datetime import datetime @@ -9,10 +8,9 @@ from app.downloader import download_video app = FastAPI() -templates = Jinja2Templates(directory="app/templates") - DOWNLOAD_DIR = "downloads" HISTORY_FILE = "history.json" +LOG_FILE = "data/app.log" os.makedirs(DOWNLOAD_DIR, exist_ok=True) os.makedirs("data", exist_ok=True) @@ -21,43 +19,71 @@ if not os.path.exists(HISTORY_FILE): with open(HISTORY_FILE, "w") as f: json.dump([], f) +if not os.path.exists(LOG_FILE): + with open(LOG_FILE, "w") as f: + f.write("") + def write_log(message): with open(LOG_FILE, "a") as f: - f.write(f"{datetime.now().strftime('%H:%M:%S')} - {message}\n") + f.write(f"{datetime.now().strftime('%H:%M:%S')} - {message}\n") @app.get("/", response_class=HTMLResponse) async def home(request: Request): - return templates.TemplateResponse("index.html", {"request": request}) + # Legge il file HTML direttamente + try: + with open("/app/app/templates/index.html", "r") as f: + html_content = f.read() + return HTMLResponse(content=html_content) + except Exception as e: + return HTMLResponse(content=f"

Errore

{str(e)}

", status_code=500) + + +@app.get("/test") +async def test(): + return {"status": "ok", "message": "Server funzionante"} @app.post("/download") async def download(url: str = Form(...), mode: str = Form(...)): - path = download_video(url, mode) + try: + path = download_video(url, mode) + write_log(f"Downloaded: {url} -> {path}") - # salva nella cronologia - with open(HISTORY_FILE, "r") as f: - history = json.load(f) + with open(HISTORY_FILE, "r") as f: + history = json.load(f) - history.append({ - "url": url, - "mode": mode, - "path": path, - "time": datetime.now().strftime("%Y-%m-%d %H:%M") - }) + history.append({ + "url": url, + "mode": mode, + "path": path, + "time": datetime.now().strftime("%Y-%m-%d %H:%M") + }) - with open(HISTORY_FILE, "w") as f: - json.dump(history, f, indent=2) + with open(HISTORY_FILE, "w") as f: + json.dump(history, f, indent=2) - return {"path": path} + return {"path": path} + except Exception as e: + write_log(f"Error: {str(e)}") + return JSONResponse(status_code=500, content={"error": str(e)}) @app.get("/history") async def history(): - with open(HISTORY_FILE) as f: - return JSONResponse(json.load(f)) + try: + with open(HISTORY_FILE) as f: + return JSONResponse(json.load(f)) + except Exception as e: + return JSONResponse(status_code=500, content={"error": str(e)}) @app.get("/file") async def file(path: str): - return FileResponse(path) + try: + safe_path = os.path.join(DOWNLOAD_DIR, os.path.basename(path)) + if not os.path.exists(safe_path): + return JSONResponse(status_code=404, content={"error": "File not found"}) + return FileResponse(safe_path) + except Exception as e: + return JSONResponse(status_code=500, content={"error": str(e)})