Skip to content

Commit 18f8cc3

Browse files
add error handling for model file retrieval in API
1 parent a987a38 commit 18f8cc3

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

mainapp/api.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import math
22
import json
3+
import os
34

45
from django.conf import settings
56
from django.shortcuts import get_object_or_404
6-
from django.http import JsonResponse, FileResponse, Http404, HttpResponseBadRequest
7+
from django.http import JsonResponse, FileResponse, Http404, HttpResponseBadRequest, HttpResponseServerError
78
from django.core.paginator import Paginator, EmptyPage
89
from .models import Model
910
from .utils import get_kv, admin
@@ -80,8 +81,12 @@ def get_model(request, model_id, revision=None):
8081
if model.is_hidden and not admin(request):
8182
raise Http404('Model does not exist.')
8283

84+
model_path = '{}/{}/{}.glb'.format(settings.MODEL_DIR, model_id, revision)
8385

84-
response = FileResponse(open('{}/{}/{}.glb'.format(settings.MODEL_DIR, model_id, revision), 'rb'))
86+
if not os.path.isfile(model_path):
87+
return HttpResponseServerError('Model file not found on the server')
88+
89+
response = FileResponse(open(model_path, 'rb'))
8590
response['Content-Disposition'] = 'attachment; filename={}_{}.glb'.format(model_id, revision)
8691
response['Content-Type'] = 'model/gltf-binary'
8792
response['Cache-Control'] = 'public, max-age=86400'

0 commit comments

Comments
 (0)