#3 add delete all

This commit is contained in:
Martin Weick 2020-12-07 12:41:32 +01:00
parent 8a50493b18
commit d6d9daabf0
2 changed files with 38 additions and 0 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
@ -70,4 +73,35 @@ class MinioService:
print("Something went wrong while deleting") print("Something went wrong while deleting")
except: except:
return False 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 return True

View File

@ -146,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