Вынес uploader_backend в отдельный сущность

This commit is contained in:
Viner Abubakirov
2026-02-23 19:32:33 +05:00
parent 03567b0c74
commit a1200efb82
7 changed files with 42 additions and 9 deletions

3
.gitignore vendored
View File

@@ -176,4 +176,5 @@ cython_debug/
# My dirs and files
trash_holder/
trash_holder/
media/

View File

@@ -0,0 +1,11 @@
from fastapi import APIRouter
from app.services import Files
router = APIRouter()
@router.get("/")
def get_files() -> list[str]:
return Files.list()

View File

@@ -1,6 +1,7 @@
from fastapi import APIRouter
from app.api.v1.endpoints import youtube
from app.api.v1.endpoints import tasks
from app.api.v1.endpoints import media
router = APIRouter()
@@ -8,3 +9,4 @@ router = APIRouter()
router.include_router(youtube.router, prefix="/youtube", tags=["YouTube"])
router.include_router(tasks.router, prefix="/tasks", tags=["Tasks"])
router.include_router(media.router, prefix="/media", tags=["Media"])

View File

@@ -20,6 +20,8 @@ class Settings(BaseSettings):
model_config = SettingsConfigDict(env_file=BASE_DIR / ".env")
MEDIA_DIR: Path = BASE_DIR / "media"
settings = Settings()

5
app/core/uploader.py Normal file
View File

@@ -0,0 +1,5 @@
# from app.utils.uploader import S3ChunkUploadBackend Используется для подключения S3
from app.utils.uploader import DiskChunkUploadBackend
uploader_backend = DiskChunkUploadBackend()

View File

@@ -1,13 +1,22 @@
import os
from app.core.config import settings
from app.core.uploader import uploader_backend
from app.utils.youtube import YtDlpManager
from app.utils.uploader import S3ChunkUploadBackend
from app.schemas import DownloadRequest, DownloadResponse
class YouTubeService:
@staticmethod
def download(data: DownloadRequest):
s3 = S3ChunkUploadBackend("")
manager = YtDlpManager(str(data.url), s3)
s3.key_prefix = f"{manager.id}@{data.quality}@"
manager = YtDlpManager(str(data.url), uploader_backend)
uploader_backend.key_prefix = f"{manager.id}@{data.quality}@"
video_url = manager.download_video(data.quality)
audio_url = manager.download_audio()
return DownloadResponse(video=video_url, audio=audio_url)
class Files:
@staticmethod
def list():
return [f for f in os.listdir(settings.MEDIA_DIR) if os.path.isfile(f)]

View File

@@ -26,13 +26,15 @@ class ChunkUploadBackend(ABC):
class DiskChunkUploadBackend(ChunkUploadBackend):
def __init__(self, base_path: str):
self.base_path = base_path
def __init__(self, key_prefix: str = ""):
self.base_path = str(settings.MEDIA_DIR)
os.makedirs(self.base_path, exist_ok=True)
self.key_prefix = key_prefix
self._file = None
def start(self, filename):
self._file = open(os.path.join(self.base_path, filename), "wb")
name = f"{self.key_prefix}{filename}"
self._file = open(os.path.join(self.base_path, name), "wb")
def upload_chunk(self, chunk: bytes):
self._file.write(chunk)
@@ -41,7 +43,8 @@ class DiskChunkUploadBackend(ChunkUploadBackend):
if self._file is None:
return
self._file.close()
return os.path.join(self.base_path, self._file.name)
relative_path = settings.MEDIA_DIR.relative_to(settings.BASE_DIR)
return os.path.join(relative_path, self._file.name)
def abort(self):
if self._file: