|
9 | 9 | import tempfile |
10 | 10 |
|
11 | 11 | import requests |
| 12 | +from requests_toolbelt.multipart.encoder import MultipartEncoder |
12 | 13 |
|
13 | 14 | from pyclowder.client import ClowderClient |
14 | 15 | from pyclowder.collections import get_datasets, get_child_collections, delete as delete_collection |
@@ -233,3 +234,120 @@ def upload_metadata(connector, client, datasetid, metadata): |
233 | 234 | verify=connector.ssl_verify if connector else True) |
234 | 235 | result.raise_for_status() |
235 | 236 |
|
| 237 | +def upload_preview(connector, client, datasetid, previewfile, previewmetadata=None, preview_mimetype=None, |
| 238 | + visualization_name=None, visualization_description=None, visualization_config_data=None, |
| 239 | + visualization_component_id=None): |
| 240 | + """Upload visualization to Clowder. |
| 241 | +
|
| 242 | + Keyword arguments: |
| 243 | + connector -- connector information, used to get missing parameters and send status updates |
| 244 | + client -- ClowderClient containing authentication credentials |
| 245 | + datsetid -- the dataset that is currently being processed |
| 246 | + previewfile -- the file containing the preview |
| 247 | + previewmetadata -- any metadata to be associated with preview, can contain a section_id |
| 248 | + to indicate the section this preview should be associated with. |
| 249 | + preview_mimetype -- (optional) MIME type of the preview file. By default, this is obtained from the |
| 250 | + file itself and this parameter can be ignored. E.g. 'application/vnd.clowder+custom+xml' |
| 251 | + """ |
| 252 | + |
| 253 | + connector.message_process({"type": "dataset", "id": datasetid}, "Uploading dataset preview.") |
| 254 | + logger = logging.getLogger(__name__) |
| 255 | + |
| 256 | + preview_id = None |
| 257 | + visualization_config_id = None |
| 258 | + |
| 259 | + if os.path.exists(previewfile): |
| 260 | + |
| 261 | + # upload visualization URL |
| 262 | + visualization_config_url = '%s/api/v2/visualizations/config' % client.host |
| 263 | + |
| 264 | + if visualization_config_data is None: |
| 265 | + visualization_config_data = dict() |
| 266 | + |
| 267 | + payload = json.dumps({ |
| 268 | + "resource": { |
| 269 | + "collection": "datasets", |
| 270 | + "resource_id": datasetid |
| 271 | + }, |
| 272 | + "client": client.host, |
| 273 | + "parameters": visualization_config_data, |
| 274 | + "visualization_mimetype": preview_mimetype, |
| 275 | + "visualization_component_id": visualization_component_id |
| 276 | + }) |
| 277 | + |
| 278 | + headers = { |
| 279 | + "X-API-KEY": client.key, |
| 280 | + "Content-Type": "application/json" |
| 281 | + } |
| 282 | + |
| 283 | + response = connector.post(visualization_config_url, headers=headers, data=payload, |
| 284 | + verify=connector.ssl_verify if connector else True) |
| 285 | + |
| 286 | + if response.status_code == 200: |
| 287 | + visualization_config_id = response.json()['id'] |
| 288 | + logger.debug("Uploaded visualization config ID = [%s]", visualization_config_id) |
| 289 | + else: |
| 290 | + logger.error("An error occurred when uploading visualization config to dataset: " + datasetid) |
| 291 | + |
| 292 | + if visualization_config_id is not None: |
| 293 | + |
| 294 | + # upload visualization URL |
| 295 | + visualization_url = '%s/api/v2/visualizations?name=%s&description=%s&config=%s' % ( |
| 296 | + client.host, visualization_name, visualization_description, visualization_config_id) |
| 297 | + |
| 298 | + filename = os.path.basename(previewfile) |
| 299 | + if preview_mimetype is not None: |
| 300 | + multipart_encoder_object = MultipartEncoder( |
| 301 | + fields={'file': (filename, open(previewfile, 'rb'), preview_mimetype)}) |
| 302 | + else: |
| 303 | + multipart_encoder_object = MultipartEncoder(fields={'file': (filename, open(previewfile, 'rb'))}) |
| 304 | + headers = {'X-API-KEY': client.key, |
| 305 | + 'Content-Type': multipart_encoder_object.content_type} |
| 306 | + response = connector.post(visualization_url, data=multipart_encoder_object, headers=headers, |
| 307 | + verify=connector.ssl_verify if connector else True) |
| 308 | + |
| 309 | + if response.status_code == 200: |
| 310 | + preview_id = response.json()['id'] |
| 311 | + logger.debug("Uploaded visualization data ID = [%s]", preview_id) |
| 312 | + else: |
| 313 | + logger.error("An error occurred when uploading the visualization data to dataset: " + datasetid) |
| 314 | + else: |
| 315 | + logger.error("Visualization data file not found") |
| 316 | + |
| 317 | + return preview_id |
| 318 | + |
| 319 | +def upload_thumbnail(connector, client, datasetid, thumbnail): |
| 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 | + thumbnail -- the file containing the thumbnail |
| 328 | + """ |
| 329 | + |
| 330 | + logger = logging.getLogger(__name__) |
| 331 | + |
| 332 | + connector.message_process({"type": "dataset", "id": datasetid}, "Uploading thumbnail to dataset.") |
| 333 | + |
| 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) |
0 commit comments