Fix: Automatic multi-directory video path resolution for torrent in-browser preview streaming
This commit is contained in:
@@ -427,6 +427,91 @@ def ensure_smb_mounted():
|
||||
return False, f"Auto-mount error: {str(e)}"
|
||||
return os.path.exists(DEFAULT_SMB_MOUNT), "Mount status checked"
|
||||
|
||||
# ==================== Video Streaming Helper (Range 206) ====================
|
||||
def resolve_stream_video_path(requested_path):
|
||||
if not requested_path:
|
||||
return None
|
||||
ensure_smb_mounted()
|
||||
|
||||
# 1. Direct path check
|
||||
if os.path.isfile(requested_path):
|
||||
return requested_path
|
||||
|
||||
basename = os.path.basename(requested_path)
|
||||
stem = os.path.splitext(basename)[0]
|
||||
|
||||
candidate_paths = [
|
||||
requested_path.replace('/isolation/videos/', '/mnt/isolation/videos/'),
|
||||
requested_path.replace('/isolation/', '/mnt/isolation/'),
|
||||
requested_path.replace('/downloads/', '/mnt/isolation/videos/unsorted/'),
|
||||
requested_path.replace('/downloads/', '/mnt/isolation/videos/videodownloader/'),
|
||||
requested_path.replace('/downloads/', '/mnt/isolation/videos/'),
|
||||
requested_path.replace('/downloads/', '/mnt/isolation/incomplete/'),
|
||||
requested_path.replace('/downloads/', '/mnt/isolation/downloads/'),
|
||||
os.path.join('/mnt/isolation/videos/unsorted', basename),
|
||||
os.path.join('/mnt/isolation/videos/videodownloader', basename),
|
||||
os.path.join('/mnt/isolation/incomplete', basename),
|
||||
os.path.join('/mnt/isolation/downloads', basename)
|
||||
]
|
||||
|
||||
video_exts = ('.mp4', '.mkv', '.avi', '.mov', '.webm', '.ts', '.m4v')
|
||||
|
||||
for p in candidate_paths:
|
||||
if os.path.isfile(p):
|
||||
return p
|
||||
elif os.path.isdir(p):
|
||||
try:
|
||||
best_file = None
|
||||
best_size = -1
|
||||
for root, _, files in os.walk(p):
|
||||
for f in files:
|
||||
if f.lower().endswith(video_exts):
|
||||
full = os.path.join(root, f)
|
||||
sz = os.path.getsize(full)
|
||||
if sz > best_size:
|
||||
best_size = sz
|
||||
best_file = full
|
||||
if best_file:
|
||||
return best_file
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# 2. Check top-level directories
|
||||
try:
|
||||
search_dirs = [
|
||||
'/mnt/isolation/videos/unsorted',
|
||||
'/mnt/isolation/videos/videodownloader',
|
||||
'/mnt/isolation/incomplete',
|
||||
'/mnt/isolation/downloads'
|
||||
]
|
||||
for s_dir in search_dirs:
|
||||
if os.path.exists(s_dir) and os.path.isdir(s_dir):
|
||||
target = os.path.join(s_dir, basename)
|
||||
if os.path.isfile(target):
|
||||
return target
|
||||
for entry in os.scandir(s_dir):
|
||||
if entry.is_file() and (entry.name == basename or os.path.splitext(entry.name)[0] == stem):
|
||||
return entry.path
|
||||
elif entry.is_dir():
|
||||
sub_target = os.path.join(entry.path, basename)
|
||||
if os.path.isfile(sub_target):
|
||||
return sub_target
|
||||
except Exception as e:
|
||||
print(f"Error scanning directories in streaming resolver: {e}")
|
||||
|
||||
# 3. Fallback search inside performer folders in /mnt/isolation/videos
|
||||
try:
|
||||
if os.path.exists('/mnt/isolation/videos'):
|
||||
for p_entry in os.scandir('/mnt/isolation/videos'):
|
||||
if p_entry.is_dir():
|
||||
candidate = os.path.join(p_entry.path, basename)
|
||||
if os.path.isfile(candidate):
|
||||
return candidate
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
return None
|
||||
|
||||
class RobustProxyHandler(http.server.SimpleHTTPRequestHandler):
|
||||
def send_cors_headers(self):
|
||||
origin = self.headers.get('Origin')
|
||||
@@ -699,11 +784,13 @@ class RobustProxyHandler(http.server.SimpleHTTPRequestHandler):
|
||||
self.wfile.write(json.dumps({"error": f"Failed batch saving to DB: {str(e)}"}).encode('utf-8'))
|
||||
|
||||
# ==================== Video Streaming Handlers (Range 206) ====================
|
||||
def handle_stream_video(self, head_only=False):
|
||||
def handle_stream_video(self):
|
||||
head_only = False
|
||||
parsed = urllib.parse.urlparse(self.path)
|
||||
params = urllib.parse.parse_qs(parsed.query)
|
||||
file_path = params.get('path', [''])[0]
|
||||
raw_path = params.get('path', [''])[0]
|
||||
|
||||
file_path = resolve_stream_video_path(raw_path)
|
||||
if not file_path or not os.path.exists(file_path) or not os.path.isfile(file_path):
|
||||
self.send_response(404)
|
||||
self.send_cors_headers()
|
||||
|
||||
Reference in New Issue
Block a user