Skip to content

Commit

Permalink
added highmag file upload to restAPI
Browse files Browse the repository at this point in the history
  • Loading branch information
JoQCcoz committed Sep 20, 2023
1 parent ac7192b commit 8944072
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
43 changes: 43 additions & 0 deletions Smartscope/server/api/viewsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,49 @@ class HighMagModelViewSet(viewsets.ModelViewSet, ExtraActionsMixin, TargetRouteM

detailed_serializer = DetailedHighMagSerializer

@ action(detail=True, methods=['patch'])
def upload_images(self,request, *args, **kwargs):

allowed_keys = ['mrc','png','ctf_img']
obj = self.get_object()
data = request.data
has_errors = False
has_success = False
return_data = dict()
logger.debug(data.keys())
for key,image in data.items():
if key not in allowed_keys:
message = f"Key: {key} is not valid, choose from {', '.join(allowed_keys)}"
return_data[key] = message
has_errors = True
logger.warning(message)
continue
try:
file = io.BytesIO(base64.b64decode(image))
#Validate image while in memory
filepath = getattr(obj,key)
logger.info(f'Saving {obj.name} -> {key} image to {filepath}')
with open(filepath, "wb") as f:
f.write(file.getbuffer())
message = f'Key: {key}, successfully uploaded and saved.'
return_data[key] = message
logger.info(message)
has_success = True
except Exception as err:
has_errors = True
message = f'Key: {key}, an error occured: {err}. Check smartscope.log for more details'
logger.info(message)
return_data[key] = message
logger.exception(err)
logger.info('Done uploading images.')
if not has_errors and has_success:
return Response(status=200, data=return_data)
if has_errors and has_success:
return Response(status=207, data=return_data)
if has_errors and not has_success:
return Response(status=200, data=return_data)


@ action(detail=True, methods=['get'])
def fft(self, request, *args, **kwargs):
obj = self.get_object()
Expand Down
5 changes: 5 additions & 0 deletions config/docker/templates_SSL/default.conf
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ server {
proxy_set_header X-Forwarded-Proto $scheme;
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
proxy_redirect off;

location ~ ^/api/highmag/(.+)/upload_images/$ {
client_max_body_size 100M;
proxy_pass http://smartscope:48001;
}
}

location /websocket/ {
Expand Down
5 changes: 5 additions & 0 deletions config/docker/templates_noSSL/default.conf
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ server {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_redirect off;

location ~ ^/api/highmag/(.+)/upload_images/$ {
client_max_body_size 100M;
proxy_pass http://smartscope:48001;
}
}

location /websocket/ {
Expand Down

0 comments on commit 8944072

Please sign in to comment.