Merge branch 'master' into '3-minio_api'
# Conflicts: # middleware/app_be/views/rest_api.py
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import pymongo
|
||||
|
||||
|
||||
class MongoManager:
|
||||
__instance = None
|
||||
|
||||
@@ -16,4 +17,8 @@ class MongoManager:
|
||||
MongoManager.__instance = pymongo.MongoClient('127.0.0.1', 27017)
|
||||
db = MongoManager.__instance.AIC
|
||||
coll = db.metadata
|
||||
coll.create_index("filename",unique=True)
|
||||
db.coll["identifier"]
|
||||
db.coll["sha512"]
|
||||
db.coll["location"]
|
||||
coll.create_index("identifier", unique=True)
|
||||
coll.create_index([("location", pymongo.GEO2D)])
|
||||
|
||||
@@ -1,15 +1,14 @@
|
||||
import hashlib
|
||||
import logging
|
||||
import json
|
||||
import base64
|
||||
|
||||
from app_be.services.dropboxservice import DropboxService
|
||||
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__)
|
||||
@@ -44,7 +43,6 @@ class ImageEndpoint:
|
||||
|
||||
return JsonResponse({'Result': 'success1'}, safe=False)
|
||||
|
||||
|
||||
@staticmethod
|
||||
@api_view(['GET'])
|
||||
def image_api_get_single(request, identifier):
|
||||
@@ -56,7 +54,7 @@ class ImageEndpoint:
|
||||
col = db.metadata
|
||||
metadata = None
|
||||
try:
|
||||
resp = col.find({"filename": identifier+".jpg"})
|
||||
resp = col.find({"identifier": identifier})
|
||||
metadata = resp[0]
|
||||
except:
|
||||
print("Could not find Metadata")
|
||||
@@ -68,8 +66,23 @@ class ImageEndpoint:
|
||||
|
||||
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)
|
||||
return JsonResponse({'Result': 'Error - could not find image in dropbox.', 'id': identifier}, status=404,
|
||||
safe=False)
|
||||
|
||||
stored_hash = metadata.get('sha512', '')
|
||||
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,
|
||||
@@ -94,15 +107,38 @@ class ImageEndpoint:
|
||||
db = instance.AIC
|
||||
col = db.metadata
|
||||
|
||||
decoded_image = WrapperService.unwrap_file(b64encoded_image)
|
||||
metadata['sha512'] = create_sha512(decoded_image)
|
||||
|
||||
try:
|
||||
resp = col.insert_one(metadata)
|
||||
metadata['identifier'] = identifier
|
||||
metadata['location'] = [metadata['longitude'], metadata['latitude']]
|
||||
col.insert_one(metadata)
|
||||
|
||||
except:
|
||||
print("Could not insert Metadata")
|
||||
|
||||
decoded_image = WrapperService.unwrap_file(b64encoded_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))
|
||||
|
||||
instance = MongoManager.getInstance()
|
||||
|
||||
db = instance.AIC
|
||||
col = db.metadata
|
||||
|
||||
try:
|
||||
col.delete_one({"identifier": identifier})
|
||||
except:
|
||||
print("Could not delete Metadata")
|
||||
|
||||
return JsonResponse({'Result': 'success1'}, safe=False)
|
||||
|
||||
Reference in New Issue
Block a user