Skip to content

Commit

Permalink
Path fix (#55)
Browse files Browse the repository at this point in the history
* fix path issue

* removed unused files
  • Loading branch information
Cosmicoppai committed Jun 8, 2024
1 parent 5ac5e3e commit b930ca3
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 11 deletions.
1 change: 1 addition & 0 deletions backend/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
from .cleanup import remove_folder, remove_file
from .path_validator import validate_path
from .staticfiles import CustomStaticFiles
from .get_path import get_path
21 changes: 21 additions & 0 deletions backend/utils/get_path.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from os import environ, pathsep, path

_path_env = environ.get('PATH')

_path_dirs = _path_env.split(pathsep)


def get_path(binary_name: str, default_path: str) -> str:
for directory in _path_dirs:
print(directory)
binary_path = path.join(directory, binary_name)
if path.isfile(binary_path):
return binary_path
else:
continue

return default_path


if __name__ == "__main__":
print(get_path("ffmpeg", "./ffmpeg"))
4 changes: 2 additions & 2 deletions backend/utils/path_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
_WINDOWS_INVALID_ENDINGS = ". "
_LINUX_INVALID_CHAR = ["/", ]
_LINUX_INVALID_ENDINGS = ""
_MAC_INVALID_CHAR = [":", "/"]
_MAC_INVALID_ENDINGS = ":/"
_DARWIN_INVALID_CHAR = [":", "/"]
_DARWIN_INVALID_ENDINGS = ":/"


def validate_path(paths: List[str]) -> List[str]:
Expand Down
18 changes: 9 additions & 9 deletions backend/video/downloader/downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import aiohttp
import asyncio
import m3u8
import os
from os import path, makedirs
from Crypto.Cipher import AES
from multiprocessing import connection, Process, Pipe
import subprocess
Expand All @@ -13,7 +13,7 @@
from .msg_system import MsgSystem
from video.library import DBLibrary, Library
from time import perf_counter
from utils import DB, remove_folder
from utils import DB, remove_folder, get_path
import logging
from typing import List, Dict, Any, Tuple
from utils.headers import get_headers
Expand Down Expand Up @@ -115,14 +115,14 @@ def __init__(
self.SEGMENT_DIR: Path = Path(file_data["segment_dir"])
self.msg_system_in_pipe = msg_system_in_pipe
self._resume_code = resume_code or self.file_data["file_name"]
self.resume_file_path = os.path.join(self.SEGMENT_DIR, f"{self.RESUME_FILENAME}")
self.resume_file_path = path.join(self.SEGMENT_DIR, f"{self.RESUME_FILENAME}")
self._hooks = hooks
self.key: bytes | None = None
self.headers = headers
self.num_of_segments: int = 0

if not self.OUTPUT_LOC.exists():
os.makedirs(self.OUTPUT_LOC)
makedirs(self.OUTPUT_LOC)

# remove output_dir and seg_dir from file_data
del self.file_data["output_dir"]
Expand All @@ -146,7 +146,7 @@ async def _run(self):

def parse_resume_info(self) -> List[str]:

if os.path.isfile(self.resume_file_path):
if path.isfile(self.resume_file_path):
with open(self.resume_file_path) as file:
resume_info = _parse_resume_info(file.read())
logging.info(f"Resume data found for {self._resume_code}.")
Expand Down Expand Up @@ -367,7 +367,7 @@ def _merge_segments(self, input_file: str | Path, output_file: str | Path = None
output_file = self._output_file

# check if exe present in backend folder else fallback to default option
ffmpeg_loc = os.environ.get("ffmpeg", "ffmpeg")
ffmpeg_loc = get_path("ffmpeg", "./ffmpeg")

cmd = f'"{ffmpeg_loc}" -f concat -safe 0 -i "{input_file}" -c copy "{output_file}" -hide_banner -loglevel warning'

Expand All @@ -376,7 +376,7 @@ def _merge_segments(self, input_file: str | Path, output_file: str | Path = None
)
remove_folder(self.SEGMENT_DIR) # remove segments
logging.info("Merging completed")
return os.path.getsize(output_file)
return path.getsize(output_file)

def _write_concat_info(self, segment_count: int) -> int:
logging.info("merging started")
Expand Down Expand Up @@ -447,7 +447,7 @@ async def _run(self):
for segment_number, segment in segment_list:
await download_queue.put(
(
os.path.join(
path.join(
self.SEGMENT_DIR,
f"segment-{segment_number}{self.SEGMENT_EXTENSION}",
),
Expand Down Expand Up @@ -658,7 +658,7 @@ async def _schedule_download(cls, typ: str, _file_name: List[str], header: str,
manifest_file_path: Path = seg_dir.joinpath(f"{downloader.MANIFEST_FILE_NAME}{downloader.MANIFEST_FILE_EXTENSION}")

if not seg_dir.exists():
os.makedirs(seg_dir) # create seg directory
makedirs(seg_dir) # create seg directory
downloader.write_manifest(manifest_file_path, manifest)

"""
Expand Down

0 comments on commit b930ca3

Please sign in to comment.