Skip to content

Commit 2c2966d

Browse files
committed
Updated in sync with PR 678 in clowder backend
1 parent b06780e commit 2c2966d

File tree

8 files changed

+62
-31
lines changed

8 files changed

+62
-31
lines changed

pyclowder/api/v1/datasets.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,15 +260,15 @@ 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):
263+
def upload_thumbnail(connector, host, key, datasetid, thumbnail):
264264
"""Upload thumbnail to Clowder.
265265
266266
Keyword arguments:
267267
connector -- connector information, used to get missing parameters and send status updates
268268
host -- the clowder host, including http and port, should end with a /
269269
key -- the secret key to login to clowder
270270
datasetid -- the dataset that the thumbnail should be associated with
271-
thumbnailid -- the file containing the thumbnail
271+
thumbnail -- the file containing the thumbnail
272272
"""
273273
logger = logging.getLogger(__name__)
274274
logger.info("Upload thumbnails to datasets is not available in V1")

pyclowder/api/v2/datasets.py

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

317317
return preview_id
318318

319-
def upload_thumbnail(connector, client, datasetid, thumbnailid):
319+
def upload_thumbnail(connector, client, datasetid, thumbnail):
320320
"""Upload thumbnail to Clowder.
321321
322322
Keyword arguments:
323323
connector -- connector information, used to get missing parameters and send status updates
324324
host -- the clowder host, including http and port, should end with a /
325325
key -- the secret key to login to clowder
326326
datasetid -- the dataset that the thumbnail should be associated with
327-
thumbnailid -- the file containing the thumbnail
327+
thumbnail -- the file containing the thumbnail
328328
"""
329329

330+
logger = logging.getLogger(__name__)
331+
330332
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"]
337333

334+
url = '%s/api/v2/thumbnails' % (client.host)
335+
336+
if os.path.exists(thumbnail):
337+
file_data = {"file": open(thumbnail, 'rb')}
338+
headers = {"X-API-KEY": client.key}
339+
result = connector.post(url, files=file_data, headers=headers,
340+
verify=connector.ssl_verify if connector else True)
341+
342+
thumbnailid = result.json()['id']
343+
logger.debug("uploaded thumbnail id = [%s]", thumbnailid)
344+
345+
connector.message_process({"type": "dataset", "id": datasetid}, "Uploading thumbnail to dataset.")
346+
headers = {'Content-Type': 'application/json',
347+
'X-API-KEY': client.key}
348+
url = '%s/api/v2/datasets/%s/thumbnail/%s' % (client.host, datasetid, thumbnailid)
349+
result = connector.patch(url, headers=headers,
350+
verify=connector.ssl_verify if connector else True)
351+
return result.json()["thumbnail_id"]
352+
else:
353+
logger.error("unable to upload thumbnail %s to dataset %s", thumbnail, datasetid)

pyclowder/api/v2/files.py

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -273,24 +273,39 @@ def upload_tags(connector, client, fileid, tags):
273273
verify=connector.ssl_verify if connector else True)
274274

275275

276-
def upload_thumbnail(connector, client, fileid, thumbnailid):
276+
def upload_thumbnail(connector, client, fileid, thumbnail):
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-
thumbnailid -- the file containing the thumbnail
284+
thumbnail -- the file containing the thumbnail
285285
"""
286286

287+
logger = logging.getLogger(__name__)
288+
287289
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"]
290+
291+
url = '%s/api/v2/thumbnails' % (client.host)
292+
293+
if os.path.exists(thumbnail):
294+
file_data = {"file": open(thumbnail, 'rb')}
295+
headers = {"X-API-KEY": client.key}
296+
result = connector.post(url, files=file_data, headers=headers,
297+
verify=connector.ssl_verify if connector else True)
298+
299+
thumbnailid = result.json()['id']
300+
logger.debug("uploaded thumbnail id = [%s]", thumbnailid)
301+
headers = {'Content-Type': 'application/json',
302+
'X-API-KEY': client.key}
303+
url = '%s/api/v2/files/%s/thumbnail/%s' % (client.host, fileid, thumbnailid)
304+
result = connector.patch(url, headers=headers,
305+
verify=connector.ssl_verify if connector else True)
306+
return result.json()["thumbnail_id"]
307+
else:
308+
logger.error("unable to upload thumbnail %s to file %s", thumbnail, fileid)
294309

295310

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

pyclowder/datasets.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -252,17 +252,17 @@ def upload_preview(connector, host, key, datasetid, previewfile, previewmetadata
252252
return preview_id
253253

254254

255-
def upload_thumbnail(connector, host, key, datasetid, thumbnailid):
255+
def upload_thumbnail(connector, host, key, datasetid, thumbnail):
256256
"""Upload thumbnail to Clowder.
257257
258258
Keyword arguments:
259259
connector -- connector information, used to get missing parameters and send status updates
260260
host -- the clowder host, including http and port, should end with a /
261261
key -- the secret key to login to clowder
262262
datasetid -- the dataset that the thumbnail should be associated with
263-
thumbnailid -- the file containing the thumbnail
263+
thumbnail -- the file containing the thumbnail
264264
"""
265265
logger = logging.getLogger(__name__)
266266

267267
client = ClowderClient(host=host, key=key)
268-
return datasets.upload_thumbnail(connector, client, datasetid, thumbnailid)
268+
return datasets.upload_thumbnail(connector, client, datasetid, thumbnail)

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,12 @@ def process_message(self, connector, host, secret_key, resource, parameters):
7777
logger.info("Preview %s uploaded to dataset successfully ", preview_id)
7878

7979
# 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")
80+
thumbnail = "thumbnail.jpeg"
81+
thumbnail_id = pyclowder.datasets.upload_thumbnail(connector, host, secret_key, dataset_id, thumbnail)
82+
if thumbnail_id is None:
83+
logger.info("Error in uploading thumbnail to dataset")
8484
else:
85-
logger.error("Error in uploading thumbnail to file")
85+
logger.error("Success in uploading thumbnail to dataset")
8686

8787

8888

40.3 KB
Loading

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,12 @@ def process_message(self, connector, host, secret_key, resource, parameters):
6363
logger.error("Error in downloading file summary")
6464

6565
# 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")
66+
thumbnail = "thumbnail.jpeg"
67+
thumbnail_id = pyclowder.files.upload_thumbnail(connector, host, secret_key, file_id, thumbnail)
68+
if thumbnail_id is None:
69+
logger.info("Error in uploading thumbnail to file")
7070
else:
71-
logger.error("Error in uploading thumbnail to file")
71+
logger.error("Success in uploading thumbnail to file")
7272

7373

7474

40.3 KB
Loading

0 commit comments

Comments
 (0)