Download apps/doc_convert/api/doc_convert_api.py from jun2024/fc: direct link, hf CLI and curl.
- Browser
- Download file 4.21 kB
-
https://huggingface.co/spaces/jun2024/fc/resolve/main/apps/doc_convert/api/doc_convert_api.py
- Command line
-
hf download hf://spaces/jun2024/fc/apps/doc_convert/api/doc_convert_api.py
-
curl -L -o doc_convert_api.py https://huggingface.co/spaces/jun2024/fc/resolve/main/apps/doc_convert/api/doc_convert_api.py
4.21 kB
| """ๆไนฆ่ฝฌๆข API ๅฑใ""" | |
| from __future__ import annotations | |
| import logging | |
| import urllib.parse | |
| from typing import Any | |
| from django.conf import settings | |
| from django.http import HttpRequest, HttpResponse | |
| from ninja import File, Form, Router, Schema | |
| from ninja.files import UploadedFile | |
| from apps.doc_convert.constants import MbidDefinition | |
| from apps.doc_convert.exceptions import ZnszjDisabledError, ZnszjNotConfiguredError | |
| from apps.doc_convert.services.doc_convert_service import DocConvertService | |
| from apps.doc_convert.services.znszj_loader import get_znszj_client | |
| logger = logging.getLogger(__name__) | |
| router = Router() | |
| # โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
| # Schema | |
| # โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
| class MbidItem(Schema): | |
| """ๅไธชๆไนฆ็ฑปๅใ""" | |
| mbid: str | |
| name: str | |
| class MbidCategoryOut(Schema): | |
| """ๆไนฆ็ฑปๅๅ็ฑปใ""" | |
| category: str | |
| items: list[MbidItem] | |
| class MbidListResponse(Schema): | |
| """ๆไนฆ็ฑปๅๅ่กจๅๅบใ""" | |
| categories: list[MbidCategoryOut] | |
| # โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
| # ๅทฅๅๅฝๆฐ | |
| # โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
| def _check_znszj_enabled() -> None: | |
| """ๆฃๆฅ ZNSZJ_ENABLED ้ ็ฝฎ๏ผๆชๅฏ็จๆถๆๅบ 403ใ""" | |
| if not getattr(settings, "ZNSZJ_ENABLED", False): | |
| raise ZnszjDisabledError() | |
| def _get_doc_convert_service() -> DocConvertService: | |
| """่ทๅ DocConvertService ๅฎไพใ""" | |
| client = get_znszj_client() | |
| if client is None: | |
| raise ZnszjNotConfiguredError() | |
| return DocConvertService(znszj_client=client) | |
| def _build_mbid_list_response(grouped: dict[str, list[MbidDefinition]]) -> MbidListResponse: | |
| """ๅฐๅ็ปๆฐๆฎ่ฝฌๆขไธบๅๅบ Schemaใ""" | |
| categories = [ | |
| MbidCategoryOut( | |
| category=cat, | |
| items=[MbidItem(mbid=item["mbid"], name=item["name"]) for item in items], | |
| ) | |
| for cat, items in grouped.items() | |
| ] | |
| return MbidListResponse(categories=categories) | |
| # โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
| # ็ซฏ็น | |
| # โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
| def get_mbid_list(request: HttpRequest) -> Any: | |
| """ | |
| ่ฟๅๆๆๆฏๆ็ๆไนฆ็ฑปๅ๏ผmbid๏ผ๏ผๆ็ฑปๅซๅ็ปใ | |
| ไธไพ่ต็งๆๆจกๅ๏ผๅง็ปๅฏ็จ๏ผไธๅ ZNSZJ_ENABLED ๅผๅ ณๆงๅถ๏ผใ | |
| """ | |
| from apps.doc_convert.constants import get_mbid_by_category | |
| grouped = get_mbid_by_category() | |
| return _build_mbid_list_response(grouped) | |
| def convert_document( | |
| request: HttpRequest, | |
| file: UploadedFile = File(...), | |
| mbid: str = Form(...), | |
| ) -> HttpResponse: | |
| """ | |
| ไธไผ ไผ ็ปๆไนฆ๏ผ่ฝฌๆขไธบ่ฆ็ด ๅผๆไนฆๅนถ่ฟๅไธ่ฝฝใ | |
| - file: .docx/.doc/.pdf ๆไปถ๏ผๆๅคง 20MB | |
| - mbid: ๆไนฆ็ฑปๅๆ ่ฏ็ฌฆ๏ผๅ่ง /mbid-list๏ผ | |
| ้่ฆ ZNSZJ_ENABLED=Trueใ | |
| """ | |
| _check_znszj_enabled() | |
| service = _get_doc_convert_service() | |
| file_content = file.read() | |
| filename = file.name or "document.docx" | |
| result_bytes = service.convert_document( | |
| file_content=file_content, | |
| filename=filename, | |
| mbid=mbid, | |
| ) | |
| # ๆ้ ไธ่ฝฝๆไปถๅ | |
| encoded_name = urllib.parse.quote("่ฆ็ด ๅผๆไนฆ.docx", safe="") | |
| response = HttpResponse( | |
| content=result_bytes, | |
| content_type="application/vnd.openxmlformats-officedocument.wordprocessingml.document", | |
| ) | |
| response["Content-Disposition"] = f"attachment; filename*=UTF-8''{encoded_name}" | |
| return response | |