Skip to content

Commit 7e6c061

Browse files
committed
split out create_directory_mapping
1 parent 31e6468 commit 7e6c061

File tree

2 files changed

+56
-37
lines changed

2 files changed

+56
-37
lines changed

dlna.py

Lines changed: 3 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
handle_get_protocol_info,
2020
parse_avi_duration,
2121
parse_mp4_duration,
22+
create_directory_mapping,
2223
)
2324
from http.server import BaseHTTPRequestHandler
2425
from urllib.parse import unquote, urlparse, quote
@@ -1400,42 +1401,8 @@ def handle_browse_request(self, soap_data):
14001401

14011402
def _create_directory_mapping(self):
14021403
"""Create a mapping between directory paths and IDs"""
1403-
# This is a simple mapping system that assigns numeric IDs to each path
1404-
# Root (0) and Media directory (1) are already assigned
1405-
mapping = {
1406-
"0": "", # Root
1407-
"1": "", # Media directory
1408-
}
1409-
1410-
# Start ID counter from 2 (0 and 1 are reserved)
1411-
id_counter = 2
1412-
1413-
# Helper function to scan directories recursively
1414-
def scan_dir(dir_path, relative_path=""):
1415-
nonlocal id_counter
1416-
1417-
try:
1418-
for item in os.listdir(dir_path):
1419-
item_path = os.path.join(dir_path, item)
1420-
item_rel_path = (
1421-
os.path.join(relative_path, item) if relative_path else item
1422-
)
1423-
1424-
# Assign an ID to this path
1425-
mapping[str(id_counter)] = item_rel_path
1426-
mapping[item_rel_path] = str(id_counter)
1427-
id_counter += 1
1428-
1429-
# Recursively scan subdirectories
1430-
if os.path.isdir(item_path):
1431-
scan_dir(item_path, item_rel_path)
1432-
except Exception as e:
1433-
print(f"Error scanning directory {dir_path}: {e}")
1434-
1435-
# Start scanning from the media directory
1436-
scan_dir(self.server_instance.media_directory)
1437-
1438-
return mapping
1404+
# Use the helper function from helpers.py
1405+
return create_directory_mapping(self.server_instance.media_directory)
14391406

14401407
def _get_id_for_path(self, path):
14411408
"""Get the ID for a specific path"""

helpers.py

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import os
22
import struct
33
import traceback
4-
from constants import SERVER_MANUFACTURER, SERVER_VERSION, SERVER_AGENT
4+
from constants import (
5+
SERVER_MANUFACTURER,
6+
SERVER_VERSION,
7+
SERVER_AGENT,
8+
is_supported_media_file,
9+
)
510

611

712
def is_safe_path(base_dir, requested_path):
@@ -37,6 +42,53 @@ def is_safe_path(base_dir, requested_path):
3742
return not rel_path.startswith(os.pardir) and not os.path.isabs(rel_path)
3843

3944

45+
def create_directory_mapping(media_directory):
46+
"""Create a mapping between directory paths and IDs
47+
48+
Args:
49+
media_directory: The base media directory to scan
50+
51+
Returns:
52+
dict: A mapping between path IDs and paths
53+
"""
54+
# This is a simple mapping system that assigns numeric IDs to each path
55+
# Root (0) and Media directory (1) are already assigned
56+
mapping = {
57+
"0": "", # Root
58+
"1": "", # Media directory
59+
}
60+
61+
# Start ID counter from 2 (0 and 1 are reserved)
62+
id_counter = 2
63+
64+
# Helper function to scan directories recursively
65+
def scan_dir(dir_path, relative_path=""):
66+
nonlocal id_counter
67+
68+
try:
69+
for item in os.listdir(dir_path):
70+
item_path = os.path.join(dir_path, item)
71+
item_rel_path = (
72+
os.path.join(relative_path, item) if relative_path else item
73+
)
74+
75+
# Assign an ID to this path
76+
mapping[str(id_counter)] = item_rel_path
77+
mapping[item_rel_path] = str(id_counter)
78+
id_counter += 1
79+
80+
# Recursively scan subdirectories
81+
if os.path.isdir(item_path):
82+
scan_dir(item_path, item_rel_path)
83+
except Exception as e:
84+
print(f"Error scanning directory {dir_path}: {e}")
85+
86+
# Start scanning from the media directory
87+
scan_dir(media_directory)
88+
89+
return mapping
90+
91+
4092
def send_device_description(self):
4193
"""Send UPnP device description XML"""
4294
device_xml = f"""<?xml version="1.0" encoding="utf-8"?>

0 commit comments

Comments
 (0)