2020-12-06 14:57:57 +01:00

111 lines
3.8 KiB
Python

import hashlib
import logging
import json
from app_be.services.dropboxservice import DropboxService
from app_be.services.mongodbservice import MongoDBService
from app_be.services.minioservice import MinioService
from app_be.services.hashservice import create_sha512
from app_be.services.wrapperservice import WrapperService
from app_be.views.mongo_db import MongoManager
from django.http import JsonResponse
from django.http import HttpRequest
from rest_framework.decorators import api_view
logger = logging.getLogger(__name__)
class TestApiClass:
@staticmethod
@api_view(['GET'])
def test_api(request):
logger.debug('Test api call: {}'.format(request))
return JsonResponse({'Result': 'success'}, safe=False)
class ImageEndpoint:
@staticmethod
@api_view(['GET'])
def image_api_get_all(request):
logger.debug('Image GET all call: {}'.format(request))
print(request)
metadata = MongoDBService.getAll()
print(metadata)
return JsonResponse({'Result': 'success1'}, safe=False)
@staticmethod
@api_view(['GET'])
def image_api_get_single(request, identifier):
logger.debug('Image GET single call: {}'.format(request))
metadata = MongoDBService.getSingle(identifier)
print(metadata)
dropbox_image_bytes = DropboxService.read_file(metadata['filename'])
#minio_image_bytes = MinioService.read_file(metadata['filename'])
if dropbox_image_bytes is None:
return JsonResponse({'Result': 'Error - could not find image in dropbox.', 'id': identifier}, status=404,
safe=False)
stored_hash = metadata.get('sha512', '')
print(stored_hash)
actual_dropbox_hash = create_sha512(dropbox_image_bytes)
if stored_hash != actual_dropbox_hash:
return JsonResponse('Stored hash does not match generated one! '
'stored: {} actual: {}'.format(stored_hash, actual_dropbox_hash), safe=False)
# TODO - check hash of MinIO image, too
# actual_minio_hash = '' # create_sha512(minio_image_bytes)
# if stored_hash != actual_minio_hash:
# return JsonResponse('Stored hash does not match generated one! '
# 'stored: {} actual: {}'.format(stored_hash, actual_minio_hash), safe=False)
payload = {
'id': identifier,
'metadata': metadata,
'image_data': WrapperService.wrap_file(dropbox_image_bytes)
}
return JsonResponse(payload, safe=False)
@staticmethod
@api_view(['POST'])
def image_api_post(request: HttpRequest):
logger.debug('Image POST call: {}'.format(request))
payload = json.loads(request.body)
b64encoded_image = payload['image_data']
identifier = payload['id']
metadata = payload['metadata']
filename = payload['metadata']['filename']
decoded_image = WrapperService.unwrap_file(b64encoded_image)
MongoDBService.createSingle(metadata,identifier,decoded_image)
if not DropboxService.create_file(filename, decoded_image):
print("Could not save image to dropbox")
if not MinioService.create_file(filename, decoded_image):
print("Could not save image to minio")
return JsonResponse({'id': identifier, 'filename': filename},
safe=False) # 'metadata': metadata response beinhaltet ObjectId welche nicht serializable is?
@staticmethod
@api_view(['DELETE'])
def image_api_delete(request: HttpRequest, identifier):
logger.debug('Image DELETE single call: {}'.format(request))
resp = MongoDBService.deleteSingle(identifier)
print(resp)
return JsonResponse({'Result': 'success1'}, safe=False)