Вынес uploader_backend в отдельный сущность
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -176,4 +176,5 @@ cython_debug/
|
||||
|
||||
|
||||
# My dirs and files
|
||||
trash_holder/
|
||||
trash_holder/
|
||||
media/
|
||||
11
app/api/v1/endpoints/media.py
Normal file
11
app/api/v1/endpoints/media.py
Normal 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()
|
||||
@@ -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"])
|
||||
|
||||
@@ -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
5
app/core/uploader.py
Normal file
@@ -0,0 +1,5 @@
|
||||
# from app.utils.uploader import S3ChunkUploadBackend Используется для подключения S3
|
||||
from app.utils.uploader import DiskChunkUploadBackend
|
||||
|
||||
|
||||
uploader_backend = DiskChunkUploadBackend()
|
||||
@@ -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)]
|
||||
|
||||
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user