Skip to content

Commit b06780e

Browse files
committed
Implemented upload_thumbnail to files and datasets
1 parent 4b6b91e commit b06780e

File tree

7 files changed

+112
-7
lines changed

7 files changed

+112
-7
lines changed

pyclowder/api/v1/datasets.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,40 @@ def upload_metadata(connector, client, datasetid, metadata):
260260
verify=connector.ssl_verify if connector else True)
261261
result.raise_for_status()
262262

263+
def upload_thumbnail(connector, host, key, datasetid, thumbnailid):
264+
"""Upload thumbnail to Clowder.
265+
266+
Keyword arguments:
267+
connector -- connector information, used to get missing parameters and send status updates
268+
host -- the clowder host, including http and port, should end with a /
269+
key -- the secret key to login to clowder
270+
datasetid -- the dataset that the thumbnail should be associated with
271+
thumbnailid -- the file containing the thumbnail
272+
"""
273+
logger = logging.getLogger(__name__)
274+
logger.info("Upload thumbnails to datasets is not available in V1")
275+
276+
277+
def upload_preview(connector, host, key, datasetid, previewfile, previewmetadata=None, preview_mimetype=None,
278+
visualization_name=None, visualization_description=None, visualization_config_data=None,
279+
visualization_component_id=None):
280+
"""Upload preview to Clowder.
281+
282+
Keyword arguments:
283+
connector -- connector information, used to get missing parameters and send status updates
284+
host -- the clowder host, including http and port, should end with a /
285+
key -- the secret key to login to clowder
286+
datasetid -- the dataset that is currently being processed
287+
previewfile -- the file containing the preview
288+
previewmetadata -- any metadata to be associated with preview, can contain a section_id
289+
to indicate the section this preview should be associated with.
290+
preview_mimetype -- (optional) MIME type of the preview file. By default, this is obtained from the
291+
file itself and this parameter can be ignored. E.g. 'application/vnd.clowder+custom+xml'
292+
"""
293+
294+
logger = logging.getLogger(__name__)
295+
logger.info("Upload preview to datasets is not available in V1")
296+
263297

264298
# TODO not done yet, need more testing
265299
class DatasetsApi(object):

pyclowder/api/v2/datasets.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,3 +316,22 @@ def upload_preview(connector, client, datasetid, previewfile, previewmetadata=No
316316

317317
return preview_id
318318

319+
def upload_thumbnail(connector, client, datasetid, thumbnailid):
320+
"""Upload thumbnail to Clowder.
321+
322+
Keyword arguments:
323+
connector -- connector information, used to get missing parameters and send status updates
324+
host -- the clowder host, including http and port, should end with a /
325+
key -- the secret key to login to clowder
326+
datasetid -- the dataset that the thumbnail should be associated with
327+
thumbnailid -- the file containing the thumbnail
328+
"""
329+
330+
connector.message_process({"type": "dataset", "id": datasetid}, "Uploading thumbnail to dataset.")
331+
headers = {'Content-Type': 'application/json',
332+
'X-API-KEY': client.key}
333+
url = '%s/api/v2/datasets/%s/thumbnail/%s' % (client.host, datasetid, thumbnailid)
334+
result = connector.patch(url, headers=headers,
335+
verify=connector.ssl_verify if connector else True)
336+
return result.json()["thumbnail_id"]
337+

pyclowder/api/v2/files.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ def download_info(connector, client, fileid):
9494

9595
return result
9696

97+
9798
def download_summary(connector, client, fileid):
9899
"""Download file summary from Clowder.
99100
@@ -272,22 +273,24 @@ def upload_tags(connector, client, fileid, tags):
272273
verify=connector.ssl_verify if connector else True)
273274

274275

275-
# TODO not implemented in v2
276-
def upload_thumbnail(connector, client, fileid, thumbnail):
276+
def upload_thumbnail(connector, client, fileid, thumbnailid):
277277
"""Upload thumbnail to Clowder.
278278
279279
Keyword arguments:
280280
connector -- connector information, used to get missing parameters and send status updates
281281
host -- the clowder host, including http and port, should end with a /
282282
key -- the secret key to login to clowder
283283
fileid -- the file that the thumbnail should be associated with
284-
thumbnail -- the file containing the thumbnail
284+
thumbnailid -- the file containing the thumbnail
285285
"""
286286

287-
# TODO: Update the code below after V2 endpoint for uploading a thumbnail is ready.
288-
logger = logging.getLogger(__name__)
289-
logger.info("Thumbnail upload is under construction and currently skipped in Clowder V2 extractors!")
290-
pass
287+
connector.message_process({"type": "file", "id": fileid}, "Uploading thumbnail to file.")
288+
headers = {'Content-Type': 'application/json',
289+
'X-API-KEY': client.key}
290+
url = '%s/api/v2/files/%s/thumbnail/%s' % (client.host, fileid, thumbnailid)
291+
result = connector.patch(url, headers=headers,
292+
verify=connector.ssl_verify if connector else True)
293+
return result.json()["thumbnail_id"]
291294

292295

293296
def upload_to_dataset(connector, client, datasetid, filepath, check_duplicate=False):

pyclowder/connectors.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,23 @@ def post(self, url, data=None, json_data=None, raise_status=True, **kwargs):
565565

566566
return response
567567

568+
def patch(self, url, data=None, json_data=None, raise_status=True, **kwargs):
569+
"""
570+
This methods wraps the Python requests PATCH method
571+
:param url: URl to use in PATCH request
572+
:param data: (optional) data (Dictionary, bytes, or file-like object) to send in the body of PATCH request
573+
:param json_data: (optional) json data to send with PATCH request
574+
:param raise_status: (optional) If set to True, call raise_for_status. Default is True.
575+
:param kwargs: List of other optional arguments to pass to PATCH call
576+
:return: Response of the PATCH request
577+
"""
578+
579+
response = requests.patch(url, data=data, json=json_data, **kwargs)
580+
if raise_status:
581+
response.raise_for_status()
582+
583+
return response
584+
568585
def put(self, url, data=None, raise_status=True, **kwargs):
569586
"""
570587
This methods wraps the Python requests PUT method

pyclowder/datasets.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,3 +250,19 @@ def upload_preview(connector, host, key, datasetid, previewfile, previewmetadata
250250
visualization_config_data=visualization_config_data,
251251
visualization_component_id=visualization_component_id)
252252
return preview_id
253+
254+
255+
def upload_thumbnail(connector, host, key, datasetid, thumbnailid):
256+
"""Upload thumbnail to Clowder.
257+
258+
Keyword arguments:
259+
connector -- connector information, used to get missing parameters and send status updates
260+
host -- the clowder host, including http and port, should end with a /
261+
key -- the secret key to login to clowder
262+
datasetid -- the dataset that the thumbnail should be associated with
263+
thumbnailid -- the file containing the thumbnail
264+
"""
265+
logger = logging.getLogger(__name__)
266+
267+
client = ClowderClient(host=host, key=key)
268+
return datasets.upload_thumbnail(connector, client, datasetid, thumbnailid)

sample-extractors/test-dataset-extractor/test-dataset-extractor.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,14 @@ def process_message(self, connector, host, secret_key, resource, parameters):
7676
else:
7777
logger.info("Preview %s uploaded to dataset successfully ", preview_id)
7878

79+
# Uploaing thumbnail
80+
thumbnailid = "64e4bed765b3b301d4965820"
81+
thumbnail_id = pyclowder.datasets.upload_thumbnail(connector, host, secret_key, dataset_id, thumbnailid)
82+
if thumbnail_id == thumbnailid:
83+
logger.info("Success in uploading thumbnail to file")
84+
else:
85+
logger.error("Error in uploading thumbnail to file")
86+
7987

8088

8189

sample-extractors/test-file-extractor/test-file-extractor.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,14 @@ def process_message(self, connector, host, secret_key, resource, parameters):
6262
else:
6363
logger.error("Error in downloading file summary")
6464

65+
# Uploaing thumbnail
66+
thumbnailid = "64ac275727c83a6786dd9fd4"
67+
thumbnail_id = pyclowder.files.upload_thumbnail(connector, host, secret_key, file_id, thumbnailid)
68+
if thumbnail_id == thumbnailid:
69+
logger.info("Success in uploading thumbnail to file")
70+
else:
71+
logger.error("Error in uploading thumbnail to file")
72+
6573

6674

6775
if __name__ == "__main__":

0 commit comments

Comments
 (0)