Merge branch '3-minio_api' into 'master'
Resolve "MinIO_API" Closes #3 See merge request aic20/g4t2!7
This commit is contained in:
commit
47dfe26ace
@ -1,5 +1,8 @@
|
||||
import base64
|
||||
import hashlib
|
||||
from typing import Union
|
||||
|
||||
import xml.etree.ElementTree as ET
|
||||
import requests
|
||||
from django.conf import settings
|
||||
|
||||
@ -32,7 +35,7 @@ class MinioService:
|
||||
if r.status_code == 200:
|
||||
print("Successfully uploaded a file!")
|
||||
else:
|
||||
print("Something went wrong")
|
||||
print("Something went wrong while creating")
|
||||
except:
|
||||
return False
|
||||
return True
|
||||
@ -53,3 +56,52 @@ class MinioService:
|
||||
except:
|
||||
print("Error downloading file from minio")
|
||||
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
|
||||
@ -113,6 +113,7 @@ class ImageEndpoint:
|
||||
|
||||
# get metadata from MongoDB
|
||||
metadata = MongoDBService.getSingle(identifier)
|
||||
|
||||
if not metadata:
|
||||
return JsonResponse({'Result': 'Error - could not find any metadata in mongoDB.',
|
||||
'id': identifier}, status=404, safe=False)
|
||||
@ -120,10 +121,15 @@ class ImageEndpoint:
|
||||
resp = MongoDBService.deleteSingle(identifier)
|
||||
print(resp)
|
||||
|
||||
|
||||
if not DropboxService.delete_file(metadata['filename']):
|
||||
print('Error deleting file in dropbox')
|
||||
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)
|
||||
|
||||
@staticmethod
|
||||
@ -140,6 +146,10 @@ class ImageEndpoint:
|
||||
print('Error deleting dropbox folder')
|
||||
result_bool = False
|
||||
|
||||
if not MinioService.delete_all():
|
||||
print('Error deleting minio folder')
|
||||
result_bool = False
|
||||
|
||||
return JsonResponse({'Result': result_bool}, safe=False)
|
||||
|
||||
@staticmethod
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user