def login(self): if (self.public): logger.info("No need to login to DroneDB.") return True try: # Authenticate payload = {'username': self.username, 'password': self.password} response = requests.post(self.__authenticate_url, data=payload) if response.status_code != 200: return False # Get the token self.token = response.json()['token'] logger.info("Logged in to DroneDB as user " + self.username + ".") if (self.update_token is not None): self.update_token(self.token) return True except(Exception) as e: logger.error(e) return False
def verify_url(url, username=None, password=None): try: registryUrl, orgSlug, dsSlug, folder = parse_url(url).values() ddb = DroneDB(registryUrl, username, password) files = ddb.get_files_list(orgSlug, dsSlug, folder) # return some info return { 'success': True, 'orgSlug': orgSlug, 'dsSlug': dsSlug, 'folder': folder, 'count': len(files), 'size': sum(i['size'] for i in files) } except Exception as e: logger.error(e) return { 'success': False, 'orgSlug': None, 'dsSlug': None, 'folder': None, 'count': None, 'size': None }
def download_file(task, file): path = task.task_path(file['name']) if connection: max_attempts = 10 for i in range(max_attempts): try: connection.download(file['url'], path) break except (Exception) as e: logger.error("Exception ocurred downloading file: " + file['url'] + " : " + str(e)) logger.info("Attempting to re-connect...") import time time.sleep(2) connection.connect_dict(connection_details, user_id) else: download_stream = requests.get(file['url'], stream=True, timeout=60) with open(path, 'wb') as fd: for chunk in download_stream.iter_content(4096): fd.write(chunk) models.ImageUpload.objects.create(task=task, image=path)
def verify_folder_url(self, folder_url): try: # Parse the url and get all necessary information information = self.parse_url(folder_url) # Define the API url we will call to assert that the folder exists and is valid folder_api_url = self.build_folder_api_url(information) # Call the API payload = self.call_api(folder_api_url) # Parse payload into a Folder instance return self.parse_payload_into_folder(folder_url, payload) except Exception as e: logger.error(str(e)) return None
def verify_server_url(self, server_url): try: # Define the API url we will call to get all the folders in the server folder_list_api_url = self.build_folder_list_api_url(server_url) # Call the API payload = self.call_api(folder_list_api_url) # Parse the payload into File instances self.parse_payload_into_folders(payload) # If I could parse it, then everything is ok return "OK" except Exception as e: logger.error(str(e)) return "Error. Invalid server URL."
def share_to_ddb(pk, settings, files): from app.plugins import logger status_key = get_status_key(pk) datastore = get_current_plugin().get_global_data_store() registry_url, username, password, token = settings ddb = DroneDB(registry_url, username, password, token) # Init share (to check) share_token = ddb.share_init() status = datastore.get_json(status_key) status['totalFiles'] = len(files) status['totalSize'] = sum(i['size'] for i in files) datastore.set_json(status_key, status) for file in files: # check that file exists if not os.path.exists(file['path']): logger.info("File {} does not exist".format(file['path'])) continue attempt = 0 while attempt < 3: try: attempt += 1 up = ddb.share_upload(share_token, file['path'], file['name']) logger.info("Uploaded " + file['name'] + " to Dronedb (hash: " + up['hash'] + ")") status['uploadedFiles'] += 1 status['uploadedSize'] += file['size'] datastore.set_json(status_key, status) break except Exception as e: if (attempt == 3): logger.error("Error uploading file {}: {}".format( file['name'], str(e))) status['error'] = "Error uploading file {}: {}".format( file['name'], str(e)) status['status'] = 2 # Error datastore.set_json(status_key, status) return else: logger.info( "Error uploading file {}: {}. Retrying...".format( file['name'], str(e))) time.sleep(5) continue res = ddb.share_commit(share_token) status['status'] = 3 # Done status['shareUrl'] = registry_url + res['url'] logger.info("Shared on url " + status['shareUrl']) datastore.set_json(status_key, status)