Skip to content

msflib.utils

Part of the msflib core package.

msflib.utils

uploads

StorageBlob

Bases: BaseModel

Result of a successful upload: URL plus stable locator parts for delete/re-sign.

StorageBase(settings: SettingsBase)

Bases: ABC

upload(file: UploadFile, parent_folder: str, storage_bucket: str, filename: str) -> str

Upload file to storage provider and return the public URL/path string.

upload_blob(file: UploadFile, parent_folder: str, storage_bucket: str, filename: str) -> StorageBlob abstractmethod

Upload file to storage provider and return structured blob metadata.

head_blob(blob: StorageBlob) -> dict[str, Any]

Return remote metadata: size_bytes, content_type, etag (values may be None).

read_blob_bytes(blob: StorageBlob, *, timeout: int = 15) -> bytes abstractmethod

Read and return raw bytes for the provided blob.

truncate_filename(filename: str | None, *, max_name_length: int = 100) -> str

Normalize upload filename and cap length while preserving extension when possible.

validate_upload(file: UploadFile, *, max_bytes: int | None = None, allowed_content_types: Sequence[str] | None = None, allowed_content_prefixes: Sequence[str] | None = None) -> None

Raise HTTPException 413/415 if upload violates size or content-type rules.

save_file(file: UploadFile, parent_folder: str, storage_bucket: str, filename: str, settings: SettingsBase, storage_method: StorageMethod = StorageMethod.file) -> str

Upload and return the public URL or path string.

read_blob_bytes(blob: StorageBlob, settings: SettingsBase, *, timeout: int = 15) -> bytes

Read blob bytes through the configured storage backend.

utils

ensure_unique_slug(base_slug: str, *, exists: Callable[[str], bool]) -> str

Return base_slug, or the first base_slug-2, base_slug-3, ... variant for which exists(candidate) is False.

Shared by every ModelAction.create that assigns a unique slug on insert -- each passes its own row lookup as exists rather than duplicating this suffix-retry loop.