|
| 1 | +from aiohttp import ClientConnectorError, ClientSession |
| 2 | + |
| 3 | +from pydis_core.utils import logging |
| 4 | + |
| 5 | +log = logging.get_logger(__name__) |
| 6 | + |
| 7 | +FAILED_REQUEST_ATTEMPTS = 3 |
| 8 | +MAX_PASTE_SIZE = 128 * 1024 # 128kB |
| 9 | + |
| 10 | + |
| 11 | +class PasteUploadError(Exception): |
| 12 | + """Raised when an error is encountered uploading to the paste service.""" |
| 13 | + |
| 14 | + |
| 15 | +class PasteTooLongError(Exception): |
| 16 | + """Raised when content is too large to upload to the paste service.""" |
| 17 | + |
| 18 | + |
| 19 | +async def send_to_paste_service( |
| 20 | + *, |
| 21 | + contents: str, |
| 22 | + paste_url: str, |
| 23 | + http_session: ClientSession, |
| 24 | + file_name: str ="", |
| 25 | + lexer: str = "python", |
| 26 | + max_size: int = MAX_PASTE_SIZE, |
| 27 | +) -> str: |
| 28 | + """ |
| 29 | + Upload some contents to the paste service. |
| 30 | +
|
| 31 | + Args: |
| 32 | + contents: The content to upload to the paste service. |
| 33 | + paste_url: The base url to the paste service. |
| 34 | + http_session (aiohttp.ClientSession): The session to use when POSTing the content to the paste service. |
| 35 | + file_name: The name of the file to save to the paste service. |
| 36 | + lexer: The lexer to save the content with. |
| 37 | + max_size: The maximum number of bytes to be allowed. Anything larger that 256kB will be rejected. |
| 38 | +
|
| 39 | + Raises: |
| 40 | + :exc:`ValueError`: ``max_length`` greater than the maximum allowed by the paste service. |
| 41 | + :exc:`PasteTooLongError`: ``contents`` too long to upload. |
| 42 | + :exc:`PasteUploadError`: Uploading failed. |
| 43 | +
|
| 44 | + Returns: |
| 45 | + The URL to the uploaded contents. |
| 46 | + """ |
| 47 | + if max_size > MAX_PASTE_SIZE: |
| 48 | + raise ValueError(f"`max_length` must not be greater than {MAX_PASTE_SIZE}") |
| 49 | + |
| 50 | + contents_size = len(contents.encode()) |
| 51 | + if contents_size > max_size: |
| 52 | + log.info("Contents too large to send to paste service.") |
| 53 | + raise PasteTooLongError(f"Contents of size {contents_size} greater than maximum size {max_size}") |
| 54 | + |
| 55 | + log.debug(f"Sending contents of size {contents_size} bytes to paste service.") |
| 56 | + payload = { |
| 57 | + "expiry": "1month", |
| 58 | + "files": [ |
| 59 | + {"name": file_name, "lexer": lexer, "content": contents}, |
| 60 | + ] |
| 61 | + } |
| 62 | + for attempt in range(1, FAILED_REQUEST_ATTEMPTS + 1): |
| 63 | + try: |
| 64 | + async with http_session.post(f"{paste_url}/api/v1/paste", json=payload) as response: |
| 65 | + response_json = await response.json() |
| 66 | + except ClientConnectorError: |
| 67 | + log.warning( |
| 68 | + f"Failed to connect to paste service at url {paste_url}, " |
| 69 | + f"trying again ({attempt}/{FAILED_REQUEST_ATTEMPTS})." |
| 70 | + ) |
| 71 | + continue |
| 72 | + except Exception: |
| 73 | + log.exception( |
| 74 | + f"An unexpected error has occurred during handling of the request, " |
| 75 | + f"trying again ({attempt}/{FAILED_REQUEST_ATTEMPTS})." |
| 76 | + ) |
| 77 | + continue |
| 78 | + |
| 79 | + if response.status == 400: |
| 80 | + log.warning( |
| 81 | + f"Paste service returned error {response_json['message']} with status code {response.status}, " |
| 82 | + f"trying again ({attempt}/{FAILED_REQUEST_ATTEMPTS})." |
| 83 | + ) |
| 84 | + continue |
| 85 | + |
| 86 | + if link := response_json.get("link" ): |
| 87 | + log.info(f"Successfully uploaded contents to {link}.") |
| 88 | + return link |
| 89 | + |
| 90 | + log.warning( |
| 91 | + f"Got unexpected JSON response from paste service: {response_json}\n" |
| 92 | + f"trying again ({attempt}/{FAILED_REQUEST_ATTEMPTS})." |
| 93 | + ) |
| 94 | + |
| 95 | + raise PasteUploadError("Failed to upload contents to paste service") |
0 commit comments