from giteapy.rest import ApiException import giteapy import base64 from storage.storage import BaseStorage import logging logger = logging.getLogger(__name__) # Gitea Support - https://pypi.org/project/giteapy/ class GiteaStorage(BaseStorage): TYPE = 'gitea' def __init__(self, url=None, username=None, password=None, source=None, destination=None, encryption_key=None, sender_name=None, sender_email=None): # The repository is added to the url parameter. Use a '#' as seperator. The repository needs to be created first. # Ex: https://git.web.rug.nl/api/v1#RepositoryName (url, self.repository) = url.split('#') destination = destination.strip('/') super().__init__(url, username, password, source, destination, encryption_key, sender_name, sender_email) # Create a commiter object when the data is uploaded through one of the invited accounts. self.committer = None if sender_name is not None or sender_email is not None: self.committer = giteapy.Identity(name=sender_name, email=sender_email) def __connect(self): try: assert(self.client) except AttributeError: # Configuration for the GITEA connection configuration = giteapy.Configuration() # Overrule the host url....? configuration.host = self.url #configuration.debug = False configuration.api_key['access_token'] = self.password # Create the client self.client = giteapy.RepositoryApi(giteapy.ApiClient(configuration)) logger.info(f'Created Gitea connection to url: {self.url}') def file_exists(self, filepath): self.__connect() try: self.client.repo_get_contents(self.username, self.repository, filepath) return True except ApiException: return False def directory_exists(self, filepath): self.__connect() return self.file_exists(filepath) def _make_folder_action(self, path): # On GitHub you cannot create empty directories. So this actions will always succeed return True def _upload_file_action(self, source, destination): self.__connect() try: with open(source, 'rb') as datafile: # This is a very big issue. Big files will be stored completely in memory :( body = giteapy.CreateFileOptions(content=base64.b64encode(datafile.read()).decode(), message=f'Upload from VRE DataDropOff\n Added file: {destination}', committer=self.committer) except Exception: return False try: # Create a file in a repository api_response = self.client.repo_create_file(self.username, self.repository, destination, body) return True except ApiException as ex: logger.exception(f'Exception when calling RepositoryApi->repo_create_file: {ex}') return True def _download_file_action(self, source, destination): self.__connect() with open(destination, 'wb') as destination_file: try: data = self.client.repo_get_contents(self.username, self.repository, source) destination_file.write(base64.b64decode(data.content)) except ApiException as ex: logger.exception(f'Exception when calling RepositoryApi->repo_get_contents: {ex}') return True