Updated recovery logic to fall back on older version if no image-hash is valid

This commit is contained in:
Martin Schett
2021-01-09 21:47:03 +01:00
parent cd707226f6
commit 1ed0627853
7 changed files with 111 additions and 15 deletions
@@ -2,6 +2,15 @@ class StorageServiceInterface:
name = "StorageServiceInterface"
@staticmethod
def get_last_modified(filename: str):
"""Get last modified time of file
:param filename: filename
:return: Date of last modification or None
"""
pass
@staticmethod
def check() -> bool:
pass
@@ -9,6 +9,20 @@ from app_be.services.StorageServiceInterface import StorageServiceInterface
class DropboxService(StorageServiceInterface):
name = "Dropbox"
@staticmethod
def get_last_modified(filename: str):
"""Get last modified time of file
:param filename: filename
:return: Date of last modification or None
"""
try:
dbx = dropbox.Dropbox(settings.DROPBOX_OAUTH2_ACCESS_TOKEN)
return dbx.files_get_metadata(settings.DROPBOX_IMAGE_FOLDER + filename).server_modified
except:
return None
return None
@staticmethod
def check() -> bool:
try:
@@ -1,5 +1,6 @@
import base64
import hashlib
from datetime import datetime
from typing import Union
import xml.etree.ElementTree as ET
@@ -12,6 +13,26 @@ from app_be.services.StorageServiceInterface import StorageServiceInterface
class MinioService(StorageServiceInterface):
name = "MinIO"
@staticmethod
def get_last_modified(filename: str):
"""Get last modified time of file
:param filename: filename
:return: Date of last modification or None
"""
try:
r = requests.get(settings.AWS_HOST + "?prefix="+filename)
root = ET.fromstring(r.content)
for child in root.iter('{http://s3.amazonaws.com/doc/2006-03-01/}Contents'):
if child.find('{http://s3.amazonaws.com/doc/2006-03-01/}Key').text == filename:
date_time_str = child.find('{http://s3.amazonaws.com/doc/2006-03-01/}LastModified').text
print(date_time_str)
date_time_obj = datetime.strptime(date_time_str, '%Y-%m-%dT%H:%M:%S.%fZ')
return date_time_obj
except:
return None
return None
@staticmethod
def check() -> bool:
print("Checking MinIO availability")
@@ -108,6 +108,13 @@ class MongoDBService:
print("Could not delete Metadata")
return resp
@staticmethod
def replaceHash(identifier, decoded_image):
instance = MongoManager.getInstance()
db = instance.AIC
col = db.metadata
col.update_one({"identifier": identifier}, {"$set": {"sha512": create_sha512(decoded_image)}})
@staticmethod
def deleteAll():
instance = MongoManager.getInstance()