devine icon indicating copy to clipboard operation
devine copied to clipboard

Feature: Add Attachment class with URL support and download functionality

Open Sp5rky opened this issue 10 months ago • 0 comments

feat(Attachment): Add URL support and download functionality to Attachment class

Description

This PR introduces several enhancements to the Attachment class within the devine/core/tracks/attachment.py file:

URL Support:

  • Added the ability to create an Attachment from a URL. This includes downloading the file to a temporary directory if a URL is provided.
  • Added url and session parameters to the constructor.
  • Added a new class method from_url to streamline the creation of attachments from URLs.

Mime Type Handling:

  • Expanded MIME type support to include .jpg, .jpeg, and .png file extensions.

Example Code in Service.

if title.data["info"]["poster"]:
    poster_url = title.data["info"]["poster"]
    
    try:
        thumbnail_name = title.data["meta"]["title"]["en"] + " thumbnail"
    except (KeyError, TypeError):
        thumbnail_name = "thumbnail"
    
    try:
        thumbnail_attachment = Attachment.from_url(
            url=poster_url,
            name=thumbnail_name,
            mime_type="image/jpeg",
            description="Thumbnail",
            session=self.session,
        )
        tracks.attachments.append(thumbnail_attachment)
    except Exception as e:
        self.log.warning(f"Failed to download thumbnail: {e}")

Sp5rky avatar Mar 08 '25 07:03 Sp5rky