(task_name, project_name, project, cloud_token, cloud_url, resources, resources_base_path, progress_callback, should_cancel)
| 91 | return Response({'size': get_size_bytes(resources)}) |
| 92 | |
| 93 | def share_task(task_name, project_name, project, cloud_token, cloud_url, resources, resources_base_path, progress_callback, should_cancel): |
| 94 | import uuid |
| 95 | import requests |
| 96 | import os |
| 97 | import time |
| 98 | from zipstream.ng import ZipStream |
| 99 | import logging |
| 100 | import jwt |
| 101 | logger = logging.getLogger('app.logger') |
| 102 | |
| 103 | CHUNK_SIZE = 8 * 1024 * 1024 # 8MB |
| 104 | cloud_url = cloud_url.rstrip('/') |
| 105 | session = requests.Session() |
| 106 | session.headers.update({ |
| 107 | 'Authorization': 'JWT {}'.format(cloud_token) |
| 108 | }) |
| 109 | |
| 110 | def check_refresh_token(): |
| 111 | nonlocal cloud_token |
| 112 | |
| 113 | try: |
| 114 | meta = jwt.decode(cloud_token, None, False) |
| 115 | exp = meta.get('exp', time.time()) |
| 116 | |
| 117 | # Refresh token if less than 1 hour remaining to expiry |
| 118 | if exp - time.time() < 60 * 60: |
| 119 | res = session.post(cloud_url + '/api/token-auth/refresh/', json={'token': cloud_token}) |
| 120 | if res.status_code == 200: |
| 121 | j = res.json() |
| 122 | if 'token' in j: |
| 123 | cloud_token = j['token'] |
| 124 | session.headers.update({ |
| 125 | 'Authorization': 'JWT {}'.format(cloud_token) |
| 126 | }) |
| 127 | except Exception as e: |
| 128 | logger.warning(f"Cannot check refresh token: {str(e)}") |
| 129 | |
| 130 | check_refresh_token() |
| 131 | cleanup = lambda: None |
| 132 | |
| 133 | # If project is None, create a new project on the remote |
| 134 | if project is None: |
| 135 | res = session.post(cloud_url + '/api/projects/', json={'name': project_name}) |
| 136 | if res.status_code != 201: |
| 137 | logger.info(res.content.decode("utf-8")) |
| 138 | return {'error': 'Failed to create Lightning project. Try again in a bit.'} |
| 139 | |
| 140 | project = res.json().get('id') |
| 141 | |
| 142 | def cleanup(): |
| 143 | try: |
| 144 | session.delete(cloud_url + f'/api/projects/{project}') |
| 145 | except Exception as e: |
| 146 | logger.warning(f"Cannot cleanup project: {str(e)}") |
| 147 | |
| 148 | dzuuid = str(uuid.uuid4()) |
| 149 | zs = ZipStream(sized=True) |
| 150 | zs.comment = "Generated by WebODM Lightning Plugin" |
nothing calls this directly
no test coverage detected