fix tmp handling

This commit is contained in:
pacnpal
2024-11-14 22:03:56 +00:00
parent 296d80875d
commit 5eeba4fa05
2 changed files with 26 additions and 15 deletions

2
.gitignore vendored
View File

@@ -1,4 +1,4 @@
.DS_Store .DS_Store
.DS_Store .DS_Store
videoarchiver/__pycache__ videoarchiver/__pycache__
videoarchiver/tmp/

View File

@@ -34,13 +34,18 @@ class DownloadError(FFmpegError):
@contextlib.contextmanager @contextlib.contextmanager
def temp_path_context(): def temp_path_context():
"""Context manager for temporary path creation and cleanup""" """Context manager for temporary path creation and cleanup"""
temp_dir = tempfile.mkdtemp(prefix="ffmpeg_") # Create a unique temporary directory within videoarchiver/tmp
tmp_base = Path("videoarchiver/tmp")
tmp_base.mkdir(parents=True, exist_ok=True)
temp_dir = tmp_base / f"ffmpeg_{int(time.time())}_{os.getpid()}"
temp_dir.mkdir(parents=True, exist_ok=True)
try: try:
os.chmod(temp_dir, stat.S_IRWXU) os.chmod(str(temp_dir), stat.S_IRWXU)
yield temp_dir yield str(temp_dir)
finally: finally:
try: try:
shutil.rmtree(temp_dir, ignore_errors=True) shutil.rmtree(str(temp_dir), ignore_errors=True)
except Exception as e: except Exception as e:
logger.error(f"Error cleaning up temp directory {temp_dir}: {e}") logger.error(f"Error cleaning up temp directory {temp_dir}: {e}")
@@ -82,7 +87,19 @@ class FFmpegManager:
RETRY_DELAY = 1 # seconds RETRY_DELAY = 1 # seconds
def __init__(self): def __init__(self):
self.base_path = Path(__file__).parent / "bin" # Set up tmp directory path
self.tmp_path = Path("videoarchiver/tmp")
self.tmp_path.mkdir(parents=True, exist_ok=True)
if platform.system() != "Windows":
self.tmp_path.chmod(0o755)
# Use XDG_DATA_HOME or fallback to ~/.local/share for Linux/macOS
if platform.system() in ["Linux", "Darwin"]:
xdg_data = os.environ.get("XDG_DATA_HOME", os.path.expanduser("~/.local/share"))
self.base_path = Path(xdg_data) / "red-discordbot" / "cogs" / "VideoArchiver" / "bin"
else: # Windows
appdata = os.environ.get("APPDATA", os.path.expanduser("~/AppData/Roaming"))
self.base_path = Path(appdata) / "Red-DiscordBot" / "cogs" / "VideoArchiver" / "bin"
# Create bin directory with proper permissions if it doesn't exist # Create bin directory with proper permissions if it doesn't exist
try: try:
@@ -135,8 +152,8 @@ class FFmpegManager:
if self.system != "Windows": if self.system != "Windows":
try: try:
current_mode = self.ffmpeg_path.stat().st_mode current_mode = self.ffmpeg_path.stat().st_mode
# Add execute permission for user (100), maintaining existing permissions # Add execute permission for user (700)
new_mode = current_mode | stat.S_IXUSR | stat.S_IRUSR | stat.S_IWUSR new_mode = current_mode | stat.S_IRWXU
self.ffmpeg_path.chmod(new_mode) self.ffmpeg_path.chmod(new_mode)
logger.info(f"Set FFmpeg permissions to: {oct(new_mode)[-3:]}") logger.info(f"Set FFmpeg permissions to: {oct(new_mode)[-3:]}")
except Exception as e: except Exception as e:
@@ -333,12 +350,6 @@ class FFmpegManager:
test_cmd = [ test_cmd = [
str(self.ffmpeg_path), str(self.ffmpeg_path),
"-f", "-f",
"lavfi",
"-i",
"testsrc=duration=1:size=1280x720:rate=30",
"-c:v",
"h264_amf",
"-f",
"null", "null",
"-", "-",
] ]