diff --git a/middleware/app_be/services/minioservice.py b/middleware/app_be/services/minioservice.py index bd22b41..f673cea 100644 --- a/middleware/app_be/services/minioservice.py +++ b/middleware/app_be/services/minioservice.py @@ -10,16 +10,15 @@ from app_be.services.StorageServiceInterface import StorageServiceInterface class MinioService(StorageServiceInterface): - name = "MinIO" @staticmethod def check() -> bool: print("Checking MinIO availability") try: - url= settings.AWS_HOST + url = settings.AWS_HOST r = requests.put(url) - if not (r.status_code >= 200 and r.status_code < 500): + if not (200 <= r.status_code < 500): return False except: return False @@ -34,9 +33,9 @@ class MinioService(StorageServiceInterface): :return: Whether file was successfully uploaded """ try: - url= settings.AWS_HOST + filename + url = settings.AWS_HOST + filename headers = {'Content-Type': 'binary/octet-stream'} - r = requests.put(url,data=file,headers=headers) + r = requests.put(url, data=file, headers=headers) if r.status_code == 200: print("Successfully uploaded a file!") else: @@ -45,6 +44,30 @@ class MinioService(StorageServiceInterface): return False return True + @staticmethod + def update_file(filename: str, file: bytes) -> bool: + """Update a file on the minio storage. + + Finds the latest version of the current file and adds another + version to the storage. + :param filename: The filename without the version. + :param file: Byte representation of the file to save as new version + :return True if successful, False otherwise + """ + try: + headers = {'Content-Type': 'binary/octet-stream'} + i = 0 + while MinioService.read_file(filename + '_' + str(i)) is not False: + i = i + 1 + url = settings.AWS_HOST + filename + '_' + str(i) + r = requests.put(url, data=file, headers=headers) + if r.status_code == 200: + print("Successfully uploaded a new version with identifier %s!".format(filename + '_' + str(i))) + else: + print("Something went wrong while updating") + except: + return False + return True @staticmethod def read_file(filename: str) -> bytes: @@ -57,7 +80,7 @@ class MinioService(StorageServiceInterface): try: url = settings.AWS_HOST + filename response = requests.get(url, stream=True) - file_bytes=response.content + file_bytes = response.content except: print("Error downloading file from minio") return file_bytes @@ -112,4 +135,4 @@ class MinioService(StorageServiceInterface): exit() except: return False - return True \ No newline at end of file + return True