2828from copy import deepcopy
2929from io import BytesIO
3030from logging import getLogger
31+ from os import path
3132from typing import BinaryIO , Optional , Any
3233
3334from requests .models import Response
@@ -72,6 +73,18 @@ def __init__(self, archivist_instance: archivist.Archivist):
7273 def __str__ (self ) -> str :
7374 return f"AttachmentsClient({ self ._archivist .url } )"
7475
76+ def get_default_key (self , data : dict [str , str ]) -> str :
77+ """
78+ Return a key to use if no key was provided
79+ either use filename or url as one of them is required
80+ """
81+ attachment_key = (
82+ data .get ("filename" , "" )
83+ if data .get ("filename" , "" )
84+ else data .get ("url" , "" )
85+ )
86+ return attachment_key .replace ("." , "_" )
87+
7588 def create (self , data : dict [str , Any ]) -> dict [str , Any ]: # pragma: no cover
7689 """
7790 Create an attachment and return struct suitable for use in an asset
@@ -108,14 +121,17 @@ def create(self, data: dict[str, Any]) -> dict[str, Any]: # pragma: no cover
108121 .. code-block:: yaml
109122
110123 arc_display_name: Telephone
111- arc_attachment_identity: blobs/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
112- arc_hash_alg: SHA256
113- arc_hash_value: xxxxxxxxxxxxxxxxxxxxxxx
124+ arc_blob_identity: blobs/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
125+ arc_blob_hash_alg: SHA256
126+ arc_blob_hash_value: xxxxxxxxxxxxxxxxxxxxxxx
127+ arc_file_name: gdn_front.jpg
114128
115129 """
116130 result = None
131+ file_part = None
117132 filename = data .get ("filename" )
118133 if filename is not None :
134+ _ , file_part = path .split (filename )
119135 with open (filename , "rb" ) as fd :
120136 attachment = self .upload (fd , mtype = data .get ("content_type" ))
121137
@@ -126,11 +142,15 @@ def create(self, data: dict[str, Any]) -> dict[str, Any]: # pragma: no cover
126142 attachment = self .upload (fd , mtype = data .get ("content_type" ))
127143
128144 result = {
129- "arc_attachment_identity" : attachment ["identity" ],
130- "arc_hash_alg" : attachment ["hash" ]["alg" ],
131- "arc_hash_value" : attachment ["hash" ]["value" ],
145+ "arc_attribute_type" : "arc_attachment" ,
146+ "arc_blob_identity" : attachment ["identity" ],
147+ "arc_blob_hash_alg" : attachment ["hash" ]["alg" ],
148+ "arc_blob_hash_value" : attachment ["hash" ]["value" ],
132149 }
133150
151+ if file_part :
152+ result ["arc_file_name" ] = file_part
153+
134154 display_name = data .get ("display_name" )
135155 if display_name is not None :
136156 result ["arc_display_name" ] = display_name
@@ -179,7 +199,7 @@ def download(
179199 fd iterator
180200
181201 Args:
182- identity (str): attachment identity e.g. attachments /xxxxxxxxxxxxxxxxxxxxxxx
202+ identity (str): attachment identity e.g. blobs /xxxxxxxxxxxxxxxxxxxxxxx
183203 fd (file): opened file descriptor or other file-type sink..
184204 params (dict): e.g. {"allow_insecure": "true"} OR {"strict": "true" }
185205
@@ -202,7 +222,7 @@ def info(
202222 Reads attachment info
203223
204224 Args:
205- identity (str): attachment identity e.g. attachments /xxxxxxxxxxxxxxxxxxxxxxx
225+ identity (str): attachment identity e.g. blobs /xxxxxxxxxxxxxxxxxxxxxxx
206226
207227 Returns:
208228 REST response
0 commit comments