11# Copyright (c) Microsoft Corporation.
22# Licensed under the MIT license.
33
4- """File upload helpers."""
4+ """File operations helpers."""
55
66from __future__ import annotations
77
88from typing import Optional
99
1010
11- class _ODataFileUpload :
12- """File upload capabilities (small + chunk) with auto selection ."""
11+ class _ODataFileOperations :
12+ """Provides file management capabilities including upload, download, and delete operations ."""
1313
1414 def _upload_file (
1515 self ,
@@ -127,7 +127,8 @@ def _upload_file_chunk(
127127 None
128128 Returns nothing on success. Any failure raises an exception.
129129 """
130- import os , math
130+ import os
131+ import math
131132 from urllib .parse import quote
132133
133134 if not record_id :
@@ -176,3 +177,46 @@ def _upload_file_chunk(
176177 self ._request ("patch" , location , headers = c_headers , data = chunk , expected = (206 , 204 ))
177178 uploaded_bytes += len (chunk )
178179 return None
180+
181+ def _download_file (
182+ self ,
183+ entity_set : str ,
184+ record_id : str ,
185+ file_name_attribute : str ,
186+ ) -> tuple [str , bytes ]:
187+ """
188+ Download a file from a Dataverse file column.
189+ :param entity_set: Source entity set (plural logical name), e.g. "accounts".
190+ :param record_id: GUID of the record
191+ :param file_name_attribute: Logical name of the file column attribute.
192+
193+ :return: Tuple with file name and file content.
194+ """
195+
196+ key = self ._format_key (record_id )
197+ url = f"{ self .api } /{ entity_set } { key } /{ file_name_attribute } /$value"
198+ response = self ._request ("get" , url )
199+ file_name = response .headers .get ('x-ms-file-name' )
200+ if file_name is None :
201+ raise ValueError ("Response is missing the 'x-ms-file-name' header. The file column may be empty or the server did not return the expected header." )
202+ return file_name , response .content
203+ def _delete_file (
204+ self ,
205+ entity_set : str ,
206+ record_id : str ,
207+ file_name_attribute : str ,
208+ ) -> None :
209+ """
210+ Delete a file from a Dataverse file column.
211+ :param entity_set: Target entity set (plural logical name), e.g. "accounts".
212+ :param record_id: GUID of the record
213+ :param file_name_attribute: Logical name of the file column attribute.
214+
215+ :return: None
216+ """
217+
218+ key = self ._format_key (record_id )
219+ url = f"{ self .api } /{ entity_set } { key } /{ file_name_attribute } "
220+ self ._request ("delete" , url )
221+
222+ return None
0 commit comments