(
self,
file_ids: list[int],
action: str,
download_limit: Optional[int] = None,
)
| 364 | } |
| 365 | |
| 366 | async def apply_files_policy_action( |
| 367 | self, |
| 368 | file_ids: list[int], |
| 369 | action: str, |
| 370 | download_limit: Optional[int] = None, |
| 371 | ) -> dict[str, Any]: |
| 372 | unique_ids = list(dict.fromkeys(file_ids)) |
| 373 | updated = [] |
| 374 | failed = [] |
| 375 | missing = [] |
| 376 | action = action.strip().lower() |
| 377 | |
| 378 | if action not in self.POLICY_ACTIONS: |
| 379 | raise HTTPException(status_code=400, detail="不支持的策略动作") |
| 380 | |
| 381 | if action == "reset_download_limit": |
| 382 | next_limit = download_limit if download_limit is not None else 5 |
| 383 | if next_limit < 1: |
| 384 | raise HTTPException(status_code=400, detail="取件次数必须大于 0") |
| 385 | |
| 386 | now = await get_now() |
| 387 | for file_id in unique_ids: |
| 388 | file_code = await FileCodes.filter(id=file_id).first() |
| 389 | if not file_code: |
| 390 | missing.append(file_id) |
| 391 | continue |
| 392 | |
| 393 | try: |
| 394 | update_data = self._build_policy_action_update( |
| 395 | file_code=file_code, |
| 396 | action=action, |
| 397 | now=now, |
| 398 | download_limit=download_limit, |
| 399 | ) |
| 400 | await file_code.update_from_dict(update_data).save() |
| 401 | updated.append(file_id) |
| 402 | except Exception as exc: |
| 403 | failed.append({"id": file_id, "reason": str(exc)}) |
| 404 | |
| 405 | if updated: |
| 406 | await self.record_admin_activity( |
| 407 | action="files.batch_policy_action", |
| 408 | target_type="file", |
| 409 | count=len(updated), |
| 410 | meta={ |
| 411 | "policyAction": action, |
| 412 | "requestedCount": len(file_ids), |
| 413 | "uniqueCount": len(unique_ids), |
| 414 | "updated": updated, |
| 415 | "missing": missing, |
| 416 | "failedCount": len(failed), |
| 417 | }, |
| 418 | ) |
| 419 | |
| 420 | return { |
| 421 | "requestedCount": len(file_ids), |
| 422 | "requested_count": len(file_ids), |
| 423 | "uniqueCount": len(unique_ids), |
no test coverage detected