Mounts directories as static files for serving through the web server.
DirectoryMounter
Mounts directories for static file serving with instance-level state. Uses Starlette’s StaticFiles middleware for efficient serving with proper MIME types, caching headers, and range request support.
DirectoryMounter
def DirectoryMounter():
Mounts directories for static file serving with instance-level state.
DirectoryMounter.mount
def mount( app, # FastHTML/Starlette application instance directories:List, # List of directory paths to mount)->None:
Mount directories to app for static file serving.
DirectoryMounter.get_url
def get_url( file_path:str, # Full path to the file)->Optional: # URL to access the file, or None if not in a mounted directory
Get URL for a file based on mounted directories.
DirectoryMounter.is_mounted
def is_mounted( directory:str, # Directory path to check)->bool: # True if the directory is mounted
Check if a directory is currently mounted.
DirectoryMounter.get_mounted_directories
def get_mounted_directories()->List: # List of mounted directory paths
Get list of currently mounted directories.
DirectoryMounter.unmount_all
def unmount_all()->None:
Remove all mounts from this instance.
DirectoryMounter.create_url_getter
def create_url_getter()->callable: # Function that converts path to URL
Create a URL getter function for use with gallery components.
# Test prefix generationmounter = DirectoryMounter()prefix = mounter._generate_prefix("/some/path")assert prefix.startswith("mg_static_")assertlen(prefix) ==len("mg_static_") +8# 8 char hash# Same path should give same prefixprefix2 = mounter._generate_prefix("/some/path")assert prefix == prefix2# Different path should give different prefixprefix3 = mounter._generate_prefix("/other/path")assert prefix3 != prefixprint("Prefix generation tests passed!")
Prefix generation tests passed!
# Test mounting directorieswith tempfile.TemporaryDirectory() as tmpdir:# Create test structure subdir = Path(tmpdir) /"media" subdir.mkdir() test_file = subdir /"video.mp4" test_file.write_text("test content") app = MockApp() mounter = DirectoryMounter()# Mount directory mounter.mount(app, [str(subdir)])# Verify mount was added to app routesassertlen(app.routes) ==1assertisinstance(app.routes[0], Mount)assert app.routes[0].path.startswith("/mg_static_")# Verify internal stateassert mounter.is_mounted(str(subdir))assertstr(subdir) in mounter.get_mounted_directories()# Test get_url url = mounter.get_url(str(test_file))assert url isnotNoneassert url.startswith("/mg_static_")assert"video.mp4"in url# File not in mount should return None other_file = Path(tmpdir) /"other.txt" other_file.write_text("other")assert mounter.get_url(str(other_file)) isNoneprint("Mount and URL tests passed!")
Mount and URL tests passed!
# Test multiple mounts and unmount_allwith tempfile.TemporaryDirectory() as tmpdir: dir1 = Path(tmpdir) /"dir1" dir2 = Path(tmpdir) /"dir2" dir1.mkdir() dir2.mkdir() app = MockApp() mounter = DirectoryMounter()# Mount multiple directories mounter.mount(app, [str(dir1), str(dir2)])assertlen(app.routes) ==2assertlen(mounter.get_mounted_directories()) ==2# Unmount all mounter.unmount_all()assertlen(app.routes) ==0assertlen(mounter.get_mounted_directories()) ==0print("Multiple mounts and unmount tests passed!")
Multiple mounts and unmount tests passed!
# Test remounting clears old mountswith tempfile.TemporaryDirectory() as tmpdir: dir1 = Path(tmpdir) /"dir1" dir2 = Path(tmpdir) /"dir2" dir1.mkdir() dir2.mkdir() app = MockApp() mounter = DirectoryMounter()# First mount mounter.mount(app, [str(dir1)])assertlen(app.routes) ==1assert mounter.is_mounted(str(dir1))# Remount with different directory - should clear old mount mounter.mount(app, [str(dir2)])assertlen(app.routes) ==1# Old mount removed, new one addedassertnot mounter.is_mounted(str(dir1))assert mounter.is_mounted(str(dir2))print("Remount cleanup tests passed!")
Remount cleanup tests passed!
# Test URL generation for nested fileswith tempfile.TemporaryDirectory() as tmpdir:# Create nested structure nested = Path(tmpdir) /"media"/"videos"/"2024" nested.mkdir(parents=True) test_file = nested /"clip.mp4" test_file.write_text("test") app = MockApp() mounter = DirectoryMounter() mounter.mount(app, [str(Path(tmpdir) /"media")])# URL should include full relative path url = mounter.get_url(str(test_file))assert url isnotNoneassert"videos/2024/clip.mp4"in urlprint("Nested file URL tests passed!")
Nested file URL tests passed!
# Test create_url_getterwith tempfile.TemporaryDirectory() as tmpdir: subdir = Path(tmpdir) /"media" subdir.mkdir() (subdir /"video.mp4").write_text("test") app = MockApp() mounter = DirectoryMounter() mounter.mount(app, [str(subdir)])# Create getter function get_url = mounter.create_url_getter()# Test getter url = get_url(str(subdir /"video.mp4"))assert url isnotNoneassert"video.mp4"in url# Test file not in mountassert get_url("/some/other/path.txt") isNoneprint("create_url_getter tests passed!")