Skip to content

Commit 806ffc7

Browse files
authored
Merge pull request #57 from AyushDharDubey/fix/issue_54-add-error-handling-for-model-file-retrieval
Add error handling for model file retrieval
2 parents 0733cc8 + 18f8cc3 commit 806ffc7

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
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'

mainapp/management/commands/nightly.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1+
import os
2+
import logging
3+
14
from django.conf import settings
25
from django.core.management.base import BaseCommand, CommandError
36
from django.utils.dateformat import format
47
from mainapp.models import Model
58
from json import dumps
69
from zipfile import ZipFile
710

11+
logger = logging.getLogger(__name__)
12+
813
class Command(BaseCommand):
914
help = 'Updates the nightly dump'
1015

@@ -54,8 +59,14 @@ def handle(self, *args, **options):
5459
]
5560
})
5661

62+
model_path = '{}/{}/{}.glb'.format(settings.MODEL_DIR, model_id, model.revision)
63+
64+
if not os.path.isfile(model_path):
65+
logging.error(f"model_id: {model_id}, {model.title}'s model file not found at: {model_path}.")
66+
continue
67+
5768
info_file.write('"{}": {}\n'.format(model_id, output))
58-
zip_file.write('{}/{}/{}.glb'.format(settings.MODEL_DIR, model_id, model.revision), 'models/{}.glb'.format(model_id))
69+
zip_file.write(model_path, 'models/{}.glb'.format(model_id))
5970

6071
info_file.write('}')
6172
info_file.close()

0 commit comments

Comments
 (0)