Release v0.1.3

This commit is contained in:
2026-06-26 01:39:18 +02:00
parent 189e950589
commit a7895ad394
9 changed files with 356 additions and 91 deletions

View File

@@ -66,7 +66,7 @@ def _files_router(context: ModuleContext):
manifest = ModuleManifest(
id="files",
name="Files",
version="1.0.0",
version="0.1.2",
dependencies=("access",),
optional_dependencies=(),
permissions=PERMISSIONS,
@@ -91,4 +91,3 @@ manifest = ModuleManifest(
def get_manifest() -> ModuleManifest:
return manifest

View File

@@ -16,6 +16,8 @@ from govoplan_core.auth.dependencies import ApiPrincipal, has_scope, require_sco
from govoplan_core.core.optional import reraise_unless_missing_package
from govoplan_files.backend.schemas import (
ArchiveRequest,
BulkFileShareRequest,
BulkFileShareResponse,
BulkDeleteRequest,
BulkDeleteResponse,
ConflictResolutionRequest,
@@ -57,6 +59,7 @@ from govoplan_files.backend.storage.files import (
list_assets_for_user,
read_asset_bytes,
share_file,
share_files,
soft_delete_assets,
)
from govoplan_files.backend.storage.folders import create_folder, list_folders_for_user, soft_delete_folder
@@ -547,6 +550,48 @@ def create_share(
raise _http_error(exc) from exc
@router.post("/bulk-shares", response_model=BulkFileShareResponse)
def create_bulk_shares(
payload: BulkFileShareRequest,
session: Session = Depends(get_session),
principal: ApiPrincipal = Depends(require_scope("files:file:share")),
):
try:
file_ids = list(dict.fromkeys(payload.file_ids))
assets = [
get_asset_for_user(session, tenant_id=principal.tenant_id, user_id=principal.user.id, asset_id=file_id, require_write=True, is_admin=_is_admin(principal))
for file_id in file_ids
]
shares = share_files(
session,
tenant_id=principal.tenant_id,
assets=assets,
target_type=payload.target_type,
target_id=payload.target_id,
permission=payload.permission,
user_id=principal.user.id,
)
session.commit()
return BulkFileShareResponse(
shared_count=len(shares),
shares=[
FileShareResponse(
id=share.id,
target_type=share.target_type,
target_id=share.target_id,
permission=share.permission,
created_at=share.created_at.isoformat(),
revoked_at=share.revoked_at.isoformat() if share.revoked_at else None,
)
for share in shares
],
)
except FileStorageError as exc:
session.rollback()
raise _http_error(exc) from exc
@router.post("/bulk-rename", response_model=RenameResponse)
def bulk_rename(
payload: RenameRequest,

View File

@@ -113,6 +113,18 @@ class FileShareRequest(BaseModel):
permission: Literal["read", "write", "manage"] = "read"
class BulkFileShareRequest(BaseModel):
file_ids: list[str] = Field(default_factory=list, max_length=1000)
target_type: Literal["user", "group", "campaign", "tenant"]
target_id: str
permission: Literal["read", "write", "manage"] = "read"
class BulkFileShareResponse(BaseModel):
shares: list[FileShareResponse]
shared_count: int
class RenameRequest(BaseModel):
file_ids: list[str] = Field(default_factory=list)
folder_paths: list[str] = Field(default_factory=list)

View File

@@ -336,6 +336,79 @@ def share_file(
return share
def share_files(
session: Session,
*,
tenant_id: str,
assets: Iterable[FileAsset],
target_type: str,
target_id: str,
permission: str,
user_id: str,
) -> list[FileShare]:
target_type = target_type.lower().strip()
permission = permission.lower().strip()
if target_type not in {"user", "group", "campaign", "tenant"}:
raise FileStorageError("Unsupported share target")
if permission not in {"read", "write", "manage"}:
raise FileStorageError("Unsupported file permission")
if target_type == "user":
target_user = session.get(User, target_id)
if not target_user or target_user.tenant_id != tenant_id or not target_user.is_active:
raise FileStorageError("User not found")
if target_type == "group":
group = session.get(Group, target_id)
if not group or group.tenant_id != tenant_id or not group.is_active:
raise FileStorageError("Group not found")
if target_type == "tenant":
tenant = session.get(Tenant, target_id)
if target_id != tenant_id or not tenant or not tenant.is_active:
raise FileStorageError("Tenant not found")
if target_type == "campaign":
Campaign = _campaign_model()
campaign = session.get(Campaign, target_id)
if not campaign or campaign.tenant_id != tenant_id:
raise FileStorageError("Campaign not found")
asset_list = list(assets)
if not asset_list:
return []
for asset in asset_list:
if asset.tenant_id != tenant_id:
raise FileStorageError("File not found")
existing_by_asset: dict[str, FileShare] = {}
for share in session.query(FileShare).filter(
FileShare.tenant_id == tenant_id,
FileShare.file_asset_id.in_([asset.id for asset in asset_list]),
FileShare.target_type == target_type,
FileShare.target_id == target_id,
FileShare.revoked_at.is_(None),
).all():
existing_by_asset.setdefault(share.file_asset_id, share)
shares: list[FileShare] = []
for asset in asset_list:
existing = existing_by_asset.get(asset.id)
if existing:
existing.permission = permission
session.add(existing)
shares.append(existing)
continue
share = FileShare(
tenant_id=tenant_id,
file_asset_id=asset.id,
target_type=target_type,
target_id=target_id,
permission=permission,
created_by_user_id=user_id,
)
session.add(share)
shares.append(share)
return shares
def soft_delete_assets(session: Session, assets: Iterable[FileAsset]) -> int:
count = 0
now = utcnow()