Browse Source

Support BOS temporary URL (#3874)

Lin Manhui 7 months ago
parent
commit
18136a1b1a
1 changed files with 18 additions and 1 deletions
  1. 18 1
      paddlex/inference/serving/infra/utils.py

+ 18 - 1
paddlex/inference/serving/infra/utils.py

@@ -16,11 +16,12 @@ import asyncio
 import base64
 import base64
 import io
 import io
 import mimetypes
 import mimetypes
+import re
 import tempfile
 import tempfile
 import uuid
 import uuid
 from functools import partial
 from functools import partial
 from typing import Awaitable, Callable, List, Optional, Tuple, TypeVar, Union, overload
 from typing import Awaitable, Callable, List, Optional, Tuple, TypeVar, Union, overload
-from urllib.parse import urlparse
+from urllib.parse import parse_qs, urlparse
 
 
 import numpy as np
 import numpy as np
 import pandas as pd
 import pandas as pd
@@ -96,6 +97,22 @@ def infer_file_type(url: str) -> Optional[FileType]:
     file_type = mimetypes.guess_type(filename)[0]
     file_type = mimetypes.guess_type(filename)[0]
 
 
     if file_type is None:
     if file_type is None:
+        # HACK: The support for BOS URLs with query params is implementation-based,
+        # not interface-based.
+        is_bos_url = re.fullmatch(r"\w+\.bcebos\.com", url_parts.netloc) is not None
+        if is_bos_url and url_parts.query:
+            params = parse_qs(url_parts.query)
+            if (
+                "responseContentDisposition" in params
+                and len(params["responseContentDisposition"]) == 1
+            ):
+                match_ = re.match(
+                    r"attachment;filename=(.*)", params["responseContentDisposition"][0]
+                )
+                if not match_:
+                    file_type = mimetypes.guess_type(match_.group(1))[0]
+                    if file_type is None:
+                        return None
         return None
         return None
 
 
     if file_type.startswith("image/"):
     if file_type.startswith("image/"):