示例#1
0
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
示例#2
0
 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,
     )
示例#3
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)
示例#4
0
 def verify_restricted_path(self) -> None:
     """Check if path can be accessed."""
     path = "/usr"
     with self.assertRaises(NotFoundException):
         verify_file_path(path)
示例#5
0
 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)
示例#6
0
 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)
示例#7
0
 def verify_root_path(self) -> None:
     """Check if path can be accessed."""
     path = "/"
     with self.assertRaises(AccessDeniedException):
         verify_file_path(path)
示例#8
0
 def verify_file_path(self) -> None:
     """Check if path can be accessed."""
     path = "/data"
     verify_file_path(path)