Files
Medios-Macina/Provider/zeroxzero.py

55 lines
1.8 KiB
Python
Raw Normal View History

2025-12-11 19:04:02 -08:00
from __future__ import annotations
import os
import sys
from typing import Any
2025-12-19 02:29:42 -08:00
from ProviderCore.base import Provider
2025-12-11 19:04:02 -08:00
from SYS.logger import log
2025-12-19 02:29:42 -08:00
class ZeroXZero(Provider):
2025-12-11 19:04:02 -08:00
"""File provider for 0x0.st."""
def upload(self, file_path: str, **kwargs: Any) -> str:
from API.HTTP import HTTPClient
2025-12-19 15:20:08 -08:00
from models import ProgressFileReader
2025-12-11 19:04:02 -08:00
if not os.path.exists(file_path):
raise FileNotFoundError(f"File not found: {file_path}")
try:
headers = {"User-Agent": "Medeia-Macina/1.0"}
with HTTPClient(headers=headers) as client:
with open(file_path, "rb") as handle:
2025-12-19 15:20:08 -08:00
try:
total = os.path.getsize(file_path)
except Exception:
total = None
wrapped = ProgressFileReader(handle, total_bytes=total, label="upload")
response = client.post("https://0x0.st", files={"file": wrapped})
2025-12-11 19:04:02 -08:00
if response.status_code == 200:
2025-12-19 03:25:52 -08:00
uploaded_url = response.text.strip()
try:
pipe_obj = kwargs.get("pipe_obj")
if pipe_obj is not None:
from Store import Store
2025-12-29 17:05:03 -08:00
Store(self.config, suppress_debug=True).try_add_url_for_pipe_object(
pipe_obj, uploaded_url
)
2025-12-19 03:25:52 -08:00
except Exception:
pass
return uploaded_url
2025-12-11 19:04:02 -08:00
raise Exception(f"Upload failed: {response.status_code} - {response.text}")
except Exception as exc:
log(f"[0x0] Upload error: {exc}", file=sys.stderr)
raise
def validate(self) -> bool:
return True