Merge branch '3-minio_api' into 'master'

Resolve "MinIO_API"

Closes #3

See merge request aic20/g4t2!7
This commit is contained in:
Martin Weick 2020-12-07 12:41:03 +01:00
commit 47dfe26ace
2 changed files with 63 additions and 1 deletions

View File

@ -1,5 +1,8 @@
import base64
import hashlib
from typing import Union from typing import Union
import xml.etree.ElementTree as ET
import requests import requests
from django.conf import settings from django.conf import settings
@ -32,7 +35,7 @@ class MinioService:
if r.status_code == 200: if r.status_code == 200:
print("Successfully uploaded a file!") print("Successfully uploaded a file!")
else: else:
print("Something went wrong") print("Something went wrong while creating")
except: except:
return False return False
return True return True
@ -53,3 +56,52 @@ class MinioService:
except: except:
print("Error downloading file from minio") print("Error downloading file from minio")
return file_bytes return file_bytes
@staticmethod
def delete_file(filename: str) -> bool:
"""Delete an file from the dropbox storage.
:param filename: filename of the file
:return: Whether file was successfully deleted
"""
try:
url = settings.AWS_HOST + filename
r = requests.delete(url)
if r.status_code == 204:
print("Successfully deleted a file!")
else:
print("Something went wrong while deleting")
except:
return False
return True
@staticmethod
def delete_all() -> bool:
"""Delete every file from the dropbox storage.
:return: Whether the storage was successfully emptied
"""
try:
r = requests.get(settings.AWS_HOST + "?list")
root = ET.fromstring(r.content)
list = []
for child in root.iter('{http://s3.amazonaws.com/doc/2006-03-01/}Contents'):
list.append(child.find('{http://s3.amazonaws.com/doc/2006-03-01/}Key').text)
body = "<Delete>"
for key in list:
body += "<Object><Key>" + key + "</Key></Object>"
body += "</Delete>"
print(body)
header = {"Content-MD5": base64.b64encode(hashlib.md5(body.encode()).digest()),
'Content-Type': 'binary/application/xml'}
r = requests.post(settings.AWS_HOST + "?delete", data=body, headers=header)
if r.status_code == 200:
print("Successfully deleted all files!")
else:
print("Something went wrong by deleting", r.status_code, r.content)
exit()
except:
return False
return True

View File

@ -113,6 +113,7 @@ class ImageEndpoint:
# get metadata from MongoDB # get metadata from MongoDB
metadata = MongoDBService.getSingle(identifier) metadata = MongoDBService.getSingle(identifier)
if not metadata: if not metadata:
return JsonResponse({'Result': 'Error - could not find any metadata in mongoDB.', return JsonResponse({'Result': 'Error - could not find any metadata in mongoDB.',
'id': identifier}, status=404, safe=False) 'id': identifier}, status=404, safe=False)
@ -120,10 +121,15 @@ class ImageEndpoint:
resp = MongoDBService.deleteSingle(identifier) resp = MongoDBService.deleteSingle(identifier)
print(resp) print(resp)
if not DropboxService.delete_file(metadata['filename']): if not DropboxService.delete_file(metadata['filename']):
print('Error deleting file in dropbox') print('Error deleting file in dropbox')
result_bool = False result_bool = False
if not MinioService.delete_file(metadata['filename']):
print('Error deleting file in minio')
result_bool = False
return JsonResponse({'Result': result_bool}, safe=False) return JsonResponse({'Result': result_bool}, safe=False)
@staticmethod @staticmethod
@ -140,6 +146,10 @@ class ImageEndpoint:
print('Error deleting dropbox folder') print('Error deleting dropbox folder')
result_bool = False result_bool = False
if not MinioService.delete_all():
print('Error deleting minio folder')
result_bool = False
return JsonResponse({'Result': result_bool}, safe=False) return JsonResponse({'Result': result_bool}, safe=False)
@staticmethod @staticmethod