File size: 7,608 Bytes
e06ee4f
 
 
 
 
 
5480f29
e06ee4f
 
5480f29
e06ee4f
 
5480f29
 
 
e06ee4f
 
5480f29
 
 
 
 
 
e06ee4f
5480f29
e06ee4f
 
 
 
 
5480f29
e06ee4f
 
 
5480f29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e06ee4f
 
 
5480f29
e06ee4f
5480f29
 
 
e06ee4f
 
 
5480f29
e06ee4f
5480f29
e06ee4f
 
 
 
 
 
 
 
 
 
5480f29
e06ee4f
5480f29
 
 
 
 
 
 
 
 
 
 
 
 
 
e06ee4f
5480f29
e06ee4f
5480f29
e06ee4f
5480f29
e06ee4f
 
5480f29
e06ee4f
5480f29
 
 
e06ee4f
 
 
5480f29
 
 
 
 
 
 
 
 
 
 
 
 
e06ee4f
5480f29
e06ee4f
 
5480f29
e06ee4f
 
 
 
 
 
5480f29
e06ee4f
 
 
 
 
 
 
 
 
5480f29
e06ee4f
 
 
 
 
5480f29
 
e06ee4f
 
5480f29
 
e06ee4f
5480f29
 
 
 
 
 
 
e06ee4f
 
5480f29
e06ee4f
5480f29
e06ee4f
 
5480f29
 
 
 
e06ee4f
5480f29
 
 
e06ee4f
5480f29
 
 
 
 
 
 
e06ee4f
5480f29
 
e06ee4f
 
 
5480f29
 
 
 
 
e06ee4f
 
 
 
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
import os
import logging
from fastapi import FastAPI, HTTPException, Request
from fastapi.responses import StreamingResponse, RedirectResponse
from fastapi.middleware.cors import CORSMiddleware
import httpx
from urllib.parse import urlparse, unquote
from PIL import Image
import io
import re

app = FastAPI(
    title="Universal Image Proxy",
    description="Proxy de imagens para qualquer aplicação com tratamento robusto de erros",
    version="2.1.0"
)

# Configuração de logs
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger("image-proxy")

# CORS completo para acesso universal
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_methods=["GET"],
    allow_headers=["*"],
    expose_headers=["*"]
)

# Configurações otimizadas para Hugging Face Spaces
TIMEOUT = 10.0
MAX_SIZE = 3 * 1024 * 1024  # 3MB
MAX_DIMENSION = 2000
MAX_REDIRECTS = 5

# Lista de domínios problemáticos que precisam de tratamento especial
PROBLEMATIC_DOMAINS = [
    "static.wikia.nocookie.net",
    "tmsimg.com",
    "espncdn.com"
]

def clean_url(url: str) -> str:
    """Remove caracteres problemáticos e decodifica URL"""
    # Decodificar URL codificada
    cleaned = unquote(url)
    
    # Remover parâmetros de cache para alguns domínios
    if any(domain in cleaned for domain in PROBLEMATIC_DOMAINS):
        cleaned = re.sub(r'(\?|&)(cb|revision|__cf_chl_[^=]+)=[^&]+', '', cleaned)
        cleaned = re.sub(r'\?.*', '', cleaned)
    
    return cleaned

@app.get("/")
async def home():
    """Página inicial com instruções simplificadas"""
    return {
        "service": "Universal Image Proxy",
        "status": "active",
        "endpoint": "/proxy-image/?url=URL_DA_IMAGEM",
        "parameters": {
            "w": "Largura desejada (opcional)",
            "h": "Altura desejada (opcional)",
            "q": "Qualidade (1-100, padrão 85)"
        },
        "example": "/proxy-image/?url=https://example.com/photo.jpg&w=300&q=85"
    }

@app.get("/proxy-image/")
async def proxy_image(
    request: Request,
    url: str,
    w: int = None,
    h: int = None,
    q: int = 85
):
    """Endpoint principal do proxy de imagens com tratamento robusto de erros"""
    try:
        # Limpar e validar URL
        cleaned_url = clean_url(url)
        parsed = urlparse(cleaned_url)
        
        if not parsed.scheme or not parsed.netloc or parsed.scheme not in ("http", "https"):
            raise HTTPException(400, "URL inválida")
        
        # Headers para evitar bloqueio
        headers = {
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36",
            "Accept": "image/*,*/*;q=0.8",
            "Referer": f"{parsed.scheme}://{parsed.netloc}/"
        }
        
        # Buscar a imagem
        async with httpx.AsyncClient(follow_redirects=True, max_redirects=MAX_REDIRECTS) as client:
            response = await client.get(
                cleaned_url,
                headers=headers,
                timeout=TIMEOUT
            )
            
            # Tratamento de erros HTTP
            if response.status_code != 200:
                error_msg = f"Origem retornou erro {response.status_code}"
                logger.warning(f"{error_msg} para: {cleaned_url}")
                raise HTTPException(response.status_code, error_msg)
            
            content_type = response.headers.get("Content-Type", "")
            if not content_type.startswith("image/"):
                error_msg = f"Tipo de conteúdo inválido: {content_type}"
                logger.warning(f"{error_msg} - URL: {cleaned_url}")
                raise HTTPException(400, error_msg)
                
            # Verificar tamanho
            if len(response.content) > MAX_SIZE:
                error_msg = f"Imagem muito grande ({len(response.content)} bytes)"
                logger.warning(f"{error_msg} - URL: {cleaned_url}")
                raise HTTPException(400, error_msg)
                
            # Processar imagem
            img_bytes = response.content
            content_type = response.headers.get("Content-Type", "image/jpeg")
            
            # Aplicar transformações se solicitado
            if w or h or q != 85:
                try:
                    with Image.open(io.BytesIO(img_bytes)) as img:
                        # Limitar dimensões máximas
                        if w and w > MAX_DIMENSION:
                            w = MAX_DIMENSION
                        if h and h > MAX_DIMENSION:
                            h = MAX_DIMENSION
                            
                        # Preservar proporções
                        if w and not h:
                            h = int(w * img.height / img.width)
                        elif h and not w:
                            w = int(h * img.width / img.height)
                        
                        # Redimensionar se necessário
                        if w or h:
                            img = img.resize((w or img.width, h or img.height))
                        
                        # Converter para JPEG (exceto PNGs com transparência)
                        output_format = "JPEG"
                        if img.format == "PNG" and img.mode == "RGBA":
                            output_format = "PNG"
                        
                        output = io.BytesIO()
                        img.save(output, format=output_format, quality=q, optimize=True)
                        img_bytes = output.getvalue()
                        content_type = f"image/{output_format.lower()}"
                except Exception as img_error:
                    logger.error(f"Erro no processamento: {str(img_error)} - URL: {cleaned_url}")
                    # Manter a imagem original se falhar no processamento
            
            # Headers de resposta
            headers = {
                "Cache-Control": "public, max-age=86400",
                "Access-Control-Allow-Origin": "*",
                "X-Image-Proxy": "processed" if (w or h or q != 85) else "original",
                "X-Original-URL": cleaned_url
            }
            
            return StreamingResponse(
                io.BytesIO(img_bytes),
                media_type=content_type,
                headers=headers
            )
            
    except httpx.HTTPStatusError as e:
        logger.error(f"Erro HTTP {e.response.status_code} para: {cleaned_url}")
        raise HTTPException(e.response.status_code, f"Erro na origem: {e.response.status_code}")
    
    except httpx.TimeoutException:
        logger.error(f"Timeout para: {cleaned_url}")
        raise HTTPException(504, "Tempo de requisição excedido")
    
    except httpx.RequestError as e:
        logger.error(f"Erro de conexão: {str(e)} - URL: {cleaned_url}")
        raise HTTPException(502, f"Erro de conexão: {str(e)}")
    
    except HTTPException:
        # Re-lançar exceções HTTP já tratadas
        raise
    
    except Exception as e:
        logger.exception(f"Erro inesperado: {str(e)} - URL: {cleaned_url}")
        raise HTTPException(500, f"Erro interno: {str(e)}")

@app.get("/favicon.ico")
async def favicon():
    return RedirectResponse(
        "https://www.stremio.com/favicon.ico",
        status_code=301,
        headers={"Cache-Control": "public, max-age=31536000"}
    )

# Função para compatibilidade com Hugging Face Spaces
def get_app():
    return app