def serve_from_filesystem(path: str) -> Any: """Serve any file from filesystem.""" try: absolute_path = f"/{path}" verify_file_path(absolute_path) return send_file(absolute_path, as_attachment=True, cache_timeout=0) except NotFoundException as err: return str(err), 404 except AccessDeniedException as err: return str(err), 403
def serve_from_filesystem( path: str, mimetype: Optional[str] = None, ) -> Response: """Serve any file from filesystem.""" verify_file_path(path) return send_file( path, mimetype=mimetype, as_attachment=False, cache_timeout=0, )
def get_directory_entries(data: Dict[str, Any], ) -> Dict[str, Any]: """Get directory entries.""" try: path = get_requested_path(data) verify_file_path(path) contents = get_non_hidden_directory_entries(path) contents = filter_requested_entries(contents, get_filter_value(data)) return { "path": path, "contents": sort_entries(contents), } except PermissionError as err: raise AccessDeniedException(err) except FileNotFoundError as err: raise NotFoundException(err) except NotADirectoryError as err: raise ClientErrorException(err)
def verify_restricted_path(self) -> None: """Check if path can be accessed.""" path = "/usr" with self.assertRaises(NotFoundException): verify_file_path(path)
def verify_non_existing_path(self) -> None: """Check if path can be accessed.""" path = "/some/non/existing/path" with self.assertRaises(NotFoundException): verify_file_path(path)
def verify_hidden_path(self) -> None: """Check if path can be accessed.""" path = "/home/user/.ssh/secret_key" with self.assertRaises(AccessDeniedException): verify_file_path(path)
def verify_root_path(self) -> None: """Check if path can be accessed.""" path = "/" with self.assertRaises(AccessDeniedException): verify_file_path(path)
def verify_file_path(self) -> None: """Check if path can be accessed.""" path = "/data" verify_file_path(path)