Skip to content

Conversation

DanSheps
Copy link
Member

Fixes: #19669 - Add an API endpoint to download image attachments

  • Add a detailed API endpoint for downloading image attachment(s)

@DanSheps DanSheps self-assigned this Jul 24, 2025
@DanSheps DanSheps requested review from a team and bctiemann and removed request for a team July 29, 2025 17:08
@DanSheps DanSheps marked this pull request as ready for review July 29, 2025 17:08
def download(self, request, pk, *args, **kwargs):
obj = get_object_or_404(self.queryset, pk=pk)
# Render and return the elevation as an SVG drawing with the correct content type
return serve(request, obj.image.path, document_root=settings.MEDIA_ROOT)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this path construction correct? When I run this I get:

GET /api/extras/image-attachments/1/download/
HTTP 404 Not Found
Allow: GET, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept

{
    "detail": "“/Users/btman/Development/netbox-community/netbox/netbox/media/Users/btman/Development/netbox-community/netbox/netbox/media/image-attachments/device_27_20240730_212632-51361-scaled.jpg” does not exist"
}

My MEDIA_ROOT is /Users/btman/Development/netbox-community/netbox/netbox/media

It works if I set document_root="/"

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Odd, it did work when I tested it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am going to try and construct a test for this as well. Just need to figure out the best way to do that.

Copy link
Contributor

@bctiemann bctiemann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See inline comment.

@DanSheps DanSheps requested a review from bctiemann August 5, 2025 13:56
@DanSheps DanSheps changed the base branch from main to feature August 7, 2025 13:12
@jeremystretch
Copy link
Member

We should also consider FR #18396, which concerns front & rear images for device types. The proposed new endpoint serves image attachments, but not image fields in general. What should we do for other images? A consistent approach would be preferable.

@DanSheps DanSheps closed this Aug 7, 2025
@DanSheps
Copy link
Member Author

DanSheps commented Aug 7, 2025

Per discussion in maintainers meeting, we will be allowing Token Authentication against the /media/* URL

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Media assets not reachable anymore through api requests since media requires Authentication
3 participants