def validate_source_path(source_folder: str) -> None: source_folder_path = Path(source_folder) if not source_folder_path.exists(): raise InvalidArgument("given root path does not exist") if not source_folder_path.is_dir(): raise InvalidArgument("given root path is not a directory")
def get_files_to_serve(source_folder, extensions=None, recurse=False, root_folder=None): if extensions is None: extensions = get_default_extensions() if not root_folder: root_folder = source_folder p = Path(source_folder) if not p.exists(): raise InvalidArgument('given root path does not exist') if not p.is_dir(): raise InvalidArgument('given root path is not a directory') names = os.listdir(p) names.sort() dirs, nondirs = [], [] for name in names: full_path = Path(os.path.join(source_folder, name)) if os.path.isdir(full_path): dirs.append(full_path) else: nondirs.append(full_path) items = dirs + nondirs for item in items: item_path = str(item) if os.path.islink(item_path): continue if item.is_dir(): if not recurse: yield { 'rel_path': item_path[len(root_folder) + 1:], 'full_path': item_path, 'is_dir': True } else: for v in get_files_to_serve(Path(item_path), extensions, recurse, root_folder): yield v else: extension = get_file_extension_from_name(item_path) if extension in extensions: yield { 'rel_path': item_path[len(root_folder) + 1:], 'full_path': item_path, 'is_dir': False }
def _ssl_option(self, ssl): if self.scheme == b'https': if ssl is None: return SECURE_SSLCONTEXT if ssl is False: return INSECURE_SSLCONTEXT if isinstance(ssl, SSLContext): return ssl raise InvalidArgument('Invalid ssl argument, expected one of: ' '{None, False, True, instance of ssl.SSLContext}') if ssl: raise InvalidArgument('SSL argument specified for non-https scheme.') return None
def get_ssl_context( scheme: bytes, ssl: Union[None, bool, ssl.SSLContext]) -> Optional[ssl.SSLContext]: if scheme == b"https": if ssl is None or ssl is True: return SECURE_SSLCONTEXT if ssl is False: return INSECURE_SSLCONTEXT if isinstance(ssl, SSLContext): return ssl raise InvalidArgument("Invalid ssl argument, expected one of: " "None, False, True, instance of ssl.SSLContext.") if ssl: raise InvalidArgument("SSL argument specified for non-https scheme.") return None
def get_files_to_serve(source_folder, extensions=None, discovery=False, root_folder=None): if extensions is None: extensions = get_default_extensions() if not root_folder: root_folder = source_folder p = Path(source_folder) if not p.exists(): raise InvalidArgument('given root path does not exist') if not p.is_dir(): raise InvalidArgument('given root path is not a directory') items = [x for x in p.iterdir()] items.sort(reverse=True) for item in items: item_path = str(item) if os.path.islink(item_path): continue if item.is_dir(): if discovery: yield { 'rel_path': item_path[len(root_folder):], 'full_path': item_path, 'is_dir': True } for v in get_files_to_serve(Path(item_path), extensions, discovery, root_folder): yield v else: extension = get_file_extension_from_name(item_path) if extension in extensions: yield { 'rel_path': item_path[len(root_folder):], 'full_path': item_path }
def serve_files(router, source_folder, extensions=None, discovery=False, cache_time=10800): if extensions is None: extensions = get_default_extensions() source_folder_path = Path(source_folder) if not source_folder_path.exists(): raise InvalidArgument('given root path does not exist') if not source_folder_path.is_dir(): raise InvalidArgument('given root path is not a directory') route = Route( b'*', get_files_handler(source_folder, os.path.abspath(source_folder), discovery, cache_time, extensions)) router.add_route(b'GET', route) router.add_route(b'HEAD', route)