Skip to content

Commit

Permalink
Refactored the method to handle the retrieval of Zammad attachments.
Browse files Browse the repository at this point in the history
- Set the HTTP method to 'get' and enabled file buffering.
- Added error handling to raise a ConfigError if an error occurs during the request.
- Extracted the image and response from the result.
- Obtained the content type from the response headers.
- Returned the image as an HTTP response with the appropriate content type.

This change ensures proper handling of Zammad attachments and improves error handling.
  • Loading branch information
urbanoprogramador committed Dec 13, 2024
1 parent 0aab129 commit 98c2c0f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 23 deletions.
13 changes: 12 additions & 1 deletion navigator/actions/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,18 @@ async def process_request(self, future, url: str):
result = filename
# getting the result, based on the Accept logic
elif self.file_buffer is True:
data = await response.read()
"""
Changed response.read() to response.content to fix the following error:
Traceback (most recent call last):
File "/home/ubuntu/navigator-api/.venv/lib/python3.11/site-packages/navigator/actions/rest.py", line 345, in process_request
data = await response.read()
^^^^^^^^^^^^^
AttributeError: 'Response' object has no attribute 'read'
During handling of the above exception, another exception occurred:
"""
data = response.content
buffer = BytesIO(data)
buffer.seek(0)
result = buffer
Expand Down
36 changes: 14 additions & 22 deletions navigator/actions/zammad.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
)
from .ticket import AbstractTicket
from .rest import RESTAction
from aiohttp.web import Response



Expand Down Expand Up @@ -394,27 +395,18 @@ async def get_attachment_img(self, attachment: str):

image, response = result

image_name = response.headers.get('Content-Disposition', 'attachment').split('=')[1].replace('"', '')
image_format = response.headers.get('Content-Type', 'image/png')
from aiohttp.web import StreamResponse

response = StreamResponse(
status=200,
headers={
'Content-Type': image_format,
'Content-Disposition': f'attachment; filename="{image_name}"',
'Content-Length': str(len(image)),
'Transfer-Encoding': 'chunked',
'Connection': 'keep-alive',
'Content-Description': 'File Transfer',
'Content-Transfer-Encoding': 'binary'
}
)
await response.prepare()
await response.write(image)
await response.write_eof()
return response

# Obtener el tipo de contenido de la respuesta

content_type = response.headers.get('Content-Type', 'application/octet-stream')

# Devolver la imagen como respuesta HTTP
"""
Changed the return method to use web.Response instead of StreamResponse due to the following error:
navigator.exceptions.exceptions: Error Getting Zammad Attachment: object of type '_io.BytesIO' has no len()
Updated the headers and response handling to fix the issue.
"""
return Response(body=image, content_type=content_type)

Check warning

Code scanning / CodeQL

Information exposure through an exception Medium

Stack trace information
flows to this location and may be exposed to an external user.
Stack trace information
flows to this location and may be exposed to an external user.
Stack trace information
flows to this location and may be exposed to an external user.
Stack trace information
flows to this location and may be exposed to an external user.
Stack trace information
flows to this location and may be exposed to an external user.
Stack trace information
flows to this location and may be exposed to an external user.
Stack trace information
flows to this location and may be exposed to an external user.
except Exception as e:
raise ConfigError(f"Error Getting Zammad Attachment: {e}") from e

0 comments on commit 98c2c0f

Please sign in to comment.