Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion streamrip/filepath_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,13 @@ def clean_filename(fn: str, restrict: bool = False) -> str:
return path


def clean_filepath(fn: str, restrict: bool = False) -> str:
def clean_filepath(fn: str, restrict: bool = False, max_length: int = 200) -> str:
path = str(sanitize_filepath(fn))
if restrict:
path = "".join(c for c in path if c in ALLOWED_CHARS)

# Truncate path to prevent filesystem issues
if len(path) > max_length:
path = path[:max_length].rstrip()

return path
4 changes: 3 additions & 1 deletion streamrip/media/album.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,9 @@ def _album_folder(self, parent: str, meta: AlbumMetadata) -> str:
parent = os.path.join(parent, self.client.source.capitalize())
formatter = config.filepaths.folder_format
folder = clean_filepath(
meta.format_folder_path(formatter), config.filepaths.restrict_characters
meta.format_folder_path(formatter),
config.filepaths.restrict_characters,
max_length=150, # Leave room for parent path and filename
)

return os.path.join(parent, folder)
30 changes: 28 additions & 2 deletions streamrip/media/track.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import asyncio
import logging
import os
import tempfile
from dataclasses import dataclass

from .. import converter
Expand Down Expand Up @@ -33,7 +34,17 @@ class Track(Media):

async def preprocess(self):
self._set_download_path()
os.makedirs(self.folder, exist_ok=True)
try:
os.makedirs(self.folder, exist_ok=True)
except OSError as e:
logger.error(f"Failed to create directory '{self.folder}': {e}")
# Try to create a shorter path as fallback
fallback_folder = os.path.join(tempfile.gettempdir(), "streamrip_fallback")
os.makedirs(fallback_folder, exist_ok=True)
self.folder = fallback_folder
self._set_download_path() # Recalculate with new folder
logger.warning(f"Using fallback directory: {self.folder}")

if self.is_single:
add_title(self.meta.title)

Expand Down Expand Up @@ -104,11 +115,26 @@ def _set_download_path(self):
if c.truncate_to > 0 and len(track_path) > c.truncate_to:
track_path = track_path[: c.truncate_to]

self.download_path = os.path.join(
# Construct the full path
full_path = os.path.join(
self.folder,
f"{track_path}.{self.downloadable.extension}",
)

# Check if the full path is too long and truncate if necessary
max_path_length = 250 # Leave some buffer for filesystem limits
if len(full_path) > max_path_length:
# Calculate how much we need to truncate the track_path
excess = len(full_path) - max_path_length
if len(track_path) > excess:
track_path = track_path[:-excess].rstrip()
full_path = os.path.join(
self.folder,
f"{track_path}.{self.downloadable.extension}",
)

self.download_path = full_path


@dataclass(slots=True)
class PendingTrack(Pending):
Expand Down
Loading