Added dropbox api support as service and cleaned up get single call
This commit is contained in:
@@ -1,14 +1,13 @@
|
||||
import logging
|
||||
import json
|
||||
import base64
|
||||
import dropbox
|
||||
import pymongo
|
||||
|
||||
from app_be.services.dropboxservice import DropboxService
|
||||
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 django.conf import settings
|
||||
|
||||
from rest_framework.decorators import api_view
|
||||
|
||||
@@ -47,44 +46,61 @@ class ImageEndpoint:
|
||||
|
||||
@staticmethod
|
||||
@api_view(['GET'])
|
||||
def image_api_get_single(request, id):
|
||||
def image_api_get_single(request, identifier):
|
||||
logger.debug('Image GET single call: {}'.format(request))
|
||||
|
||||
instance = MongoManager.getInstance()
|
||||
|
||||
db = instance.AIC
|
||||
col = db.metadata
|
||||
|
||||
metadata = None
|
||||
try:
|
||||
resp = col.find({"filename": id+".jpg"})
|
||||
print(resp[0]);
|
||||
resp = col.find({"filename": identifier+".jpg"})
|
||||
metadata = resp[0]
|
||||
except:
|
||||
print("Could not find Metadata")
|
||||
if metadata is None:
|
||||
return JsonResponse({'Result': 'Error - could not find metadata.'}, status=404, safe=False)
|
||||
else:
|
||||
if '_id' in metadata:
|
||||
del metadata['_id']
|
||||
|
||||
return JsonResponse({'Result': 'success1', 'id': id}, safe=False)
|
||||
dropbox_image_bytes = DropboxService.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)
|
||||
|
||||
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.decode('utf-8'))
|
||||
payload = json.loads(request.body)
|
||||
b64encoded_image = payload['image_data']
|
||||
filename = payload['id']
|
||||
identifier = payload['id']
|
||||
metadata = payload['metadata']
|
||||
filename = payload['metadata']['filename']
|
||||
|
||||
instance = MongoManager.getInstance()
|
||||
|
||||
db = instance.AIC
|
||||
col = db.metadata
|
||||
|
||||
|
||||
try:
|
||||
resp = col.insert_one(metadata)
|
||||
except:
|
||||
print("Could not insert Metadata")
|
||||
|
||||
decoded_image = base64.b64decode(b64encoded_image)
|
||||
dbx = dropbox.Dropbox(settings.DROPBOX_OAUTH2_ACCESS_TOKEN)
|
||||
dbx.files_upload(decoded_image, '/Apps/AIC Federated Storage Infrastructure/' + filename)
|
||||
return JsonResponse({'Result': 'success2', 'received file': filename},
|
||||
decoded_image = WrapperService.unwrap_file(b64encoded_image)
|
||||
if not DropboxService.create_file(filename, decoded_image):
|
||||
print("Could not save image to dropbox")
|
||||
|
||||
return JsonResponse({'id': identifier, 'filename': filename},
|
||||
safe=False) # 'metadata': metadata response beinhaltet ObjectId welche nicht serializable is?
|
||||
|
||||
Reference in New Issue
Block a user