Video file size check

This commit is contained in:
Taras Terletskyi 2022-06-11 23:34:07 +03:00
parent a8042c5cc2
commit a4702c4f2d
5 changed files with 22 additions and 13 deletions

View file

@ -57,10 +57,11 @@ If your URL can't be downloaded for some reason, you will see this
## API
By default, API service will run on your `localhost` and `1984` port.
API endpoint documentations lives at `http://127.0.0.1:1984/docs`.
| Endpoint | Method| Description|
|---|---|---|
| `/status` | `GET` | Get API healthcheck status, usually response is `{"message": "OK"}` |
| `/status` | `GET` | Get API healthcheck status, usually response is `{"status": "OK"}` |
| `/v1/yt-dlp` | `GET` | Get latest and currently installed `yt-dlp` version |
|`/v1/tasks/?include_meta=False&status=DONE`| `GET` | Get all tasks with filtering options like to include large file metadata and by task status: `PENDING`, `PROCESSING`, `FAILED` and `DONE`. |
| `/v1/tasks/f828714a-5c50-45de-87c0-3b51b7e04039?include_meta=True` | `GET` | Get info about task by ID |

View file

@ -2,4 +2,4 @@ from yt_shared.schemas.base import RealBaseModel
class HealthcheckSchema(RealBaseModel):
message: str = 'OK'
status: str = 'OK'

View file

@ -12,7 +12,11 @@ from core.exceptions import InvalidBodyError
from core.tasks.abstract import AbstractTask
from core.tasks.upload import UploadTask
from core.utils import bold
from yt_shared.config import TMP_DOWNLOAD_PATH, UPLOAD_VIDEO_FILE
from yt_shared.config import (
MAX_UPLOAD_VIDEO_FILE_SIZE,
TMP_DOWNLOAD_PATH,
UPLOAD_VIDEO_FILE,
)
from yt_shared.emoji import SUCCESS_EMOJI
from yt_shared.rabbit import get_rabbitmq
from yt_shared.rabbit.rabbit_config import ERROR_QUEUE, SUCCESS_QUEUE
@ -84,24 +88,26 @@ class SuccessResultWorkerTask(AbstractResultWorkerTask):
async def _process_body(self, body: SuccessPayload) -> None:
process_coros = [self._send_success_text(body)]
if self._eligible_for_upload(body):
filepath: str = os.path.join(TMP_DOWNLOAD_PATH, body.filename)
if self._eligible_for_upload(filepath):
process_coros.append(self._create_upload_task(body))
await asyncio.gather(*process_coros)
self._cleanup(body.filename)
else:
self._log.warning('File %s will not be uploaded to Telegram', body.filename)
await asyncio.gather(*process_coros, return_exceptions=True)
self._cleanup(filepath)
def _cleanup(self, filename: str) -> None:
filepath = os.path.join(TMP_DOWNLOAD_PATH, filename)
def _cleanup(self, filepath: str) -> None:
try:
os.remove(filepath)
except Exception:
self._log.exception('Failed to remove "%s" during cleanup', filepath)
@staticmethod
def _eligible_for_upload(body: SuccessPayload) -> bool:
# TODO: Also validate file size.
if UPLOAD_VIDEO_FILE:
return True
return False
def _eligible_for_upload(filepath: str) -> bool:
return (
UPLOAD_VIDEO_FILE
and os.stat(filepath).st_size <= MAX_UPLOAD_VIDEO_FILE_SIZE
)
async def _create_upload_task(self, body: SuccessPayload) -> None:
"""Upload video to Telegram chat."""

View file

@ -22,3 +22,4 @@ TMP_DOWNLOAD_PATH=/tmp/download_tmpfs
STORAGE_PATH=/filestorage
SAVE_VIDEO_FILE=True
UPLOAD_VIDEO_FILE=False
MAX_UPLOAD_VIDEO_FILE_SIZE=2147483648

View file

@ -37,3 +37,4 @@ YTDLP_VERSION_CHECK_INTERVAL = int(os.getenv('YTDLP_VERSION_CHECK_INTERVAL', 864
SAVE_VIDEO_FILE = get_env_bool(os.getenv('SAVE_VIDEO_FILE', True))
UPLOAD_VIDEO_FILE = get_env_bool(os.getenv('UPLOAD_VIDEO_FILE', True))
MAX_UPLOAD_VIDEO_FILE_SIZE = int(os.getenv('MAX_UPLOAD_VIDEO_FILE_SIZE', 2147483648))