|
| 1 | +import base64 |
| 2 | +import os |
| 3 | +import sys |
| 4 | +from pathlib import Path |
| 5 | +from typing import TYPE_CHECKING, List, Optional, Union |
| 6 | + |
| 7 | +if sys.version_info >= (3, 8): # pragma: no cover |
| 8 | + from typing import TypedDict |
| 9 | +else: # pragma: no cover |
| 10 | + from typing_extensions import TypedDict |
| 11 | + |
| 12 | +from playwright._impl._connection import Channel, from_channel |
| 13 | +from playwright._impl._helper import Error, async_readfile |
| 14 | +from playwright._impl._writable_stream import WritableStream |
| 15 | + |
| 16 | +if TYPE_CHECKING: # pragma: no cover |
| 17 | + from playwright._impl._browser_context import BrowserContext |
| 18 | + |
| 19 | +from playwright._impl._api_structures import FilePayload |
| 20 | + |
| 21 | +SIZE_LIMIT_IN_BYTES = 50 * 1024 * 1024 |
| 22 | + |
| 23 | + |
| 24 | +class InputFilesList(TypedDict): |
| 25 | + streams: Optional[List[Channel]] |
| 26 | + localPaths: Optional[List[str]] |
| 27 | + files: Optional[List[FilePayload]] |
| 28 | + |
| 29 | + |
| 30 | +async def convert_input_files( |
| 31 | + files: Union[str, Path, FilePayload, List[Union[str, Path]], List[FilePayload]], |
| 32 | + context: "BrowserContext", |
| 33 | +) -> InputFilesList: |
| 34 | + file_list = files if isinstance(files, list) else [files] |
| 35 | + |
| 36 | + has_large_buffer = any( |
| 37 | + [ |
| 38 | + len(f.get("buffer", "")) > SIZE_LIMIT_IN_BYTES |
| 39 | + for f in file_list |
| 40 | + if not isinstance(f, (str, Path)) |
| 41 | + ] |
| 42 | + ) |
| 43 | + if has_large_buffer: |
| 44 | + raise Error( |
| 45 | + "Cannot set buffer larger than 50Mb, please write it to a file and pass its path instead." |
| 46 | + ) |
| 47 | + |
| 48 | + has_large_file = any( |
| 49 | + [ |
| 50 | + os.stat(f).st_size > SIZE_LIMIT_IN_BYTES |
| 51 | + for f in file_list |
| 52 | + if isinstance(f, (str, Path)) |
| 53 | + ] |
| 54 | + ) |
| 55 | + if has_large_file: |
| 56 | + if context._channel._connection.is_remote: |
| 57 | + streams = [] |
| 58 | + for file in file_list: |
| 59 | + assert isinstance(file, (str, Path)) |
| 60 | + stream: WritableStream = from_channel( |
| 61 | + await context._channel.send( |
| 62 | + "createTempFile", {"name": os.path.basename(file)} |
| 63 | + ) |
| 64 | + ) |
| 65 | + await stream.copy(file) |
| 66 | + streams.append(stream._channel) |
| 67 | + return InputFilesList(streams=streams, localPaths=None, files=None) |
| 68 | + local_paths = [] |
| 69 | + for p in file_list: |
| 70 | + assert isinstance(p, (str, Path)) |
| 71 | + local_paths.append(str(Path(p).absolute().resolve())) |
| 72 | + return InputFilesList(streams=None, localPaths=local_paths, files=None) |
| 73 | + |
| 74 | + return InputFilesList( |
| 75 | + streams=None, localPaths=None, files=await _normalize_file_payloads(files) |
| 76 | + ) |
| 77 | + |
| 78 | + |
| 79 | +async def _normalize_file_payloads( |
| 80 | + files: Union[str, Path, FilePayload, List[Union[str, Path]], List[FilePayload]] |
| 81 | +) -> List: |
| 82 | + file_list = files if isinstance(files, list) else [files] |
| 83 | + file_payloads: List = [] |
| 84 | + for item in file_list: |
| 85 | + if isinstance(item, (str, Path)): |
| 86 | + file_payloads.append( |
| 87 | + { |
| 88 | + "name": os.path.basename(item), |
| 89 | + "buffer": base64.b64encode(await async_readfile(item)).decode(), |
| 90 | + } |
| 91 | + ) |
| 92 | + else: |
| 93 | + file_payloads.append( |
| 94 | + { |
| 95 | + "name": item["name"], |
| 96 | + "mimeType": item["mimeType"], |
| 97 | + "buffer": base64.b64encode(item["buffer"]).decode(), |
| 98 | + } |
| 99 | + ) |
| 100 | + |
| 101 | + return file_payloads |
0 commit comments