Refactored serve_content_file to dynamically use model based on url folder
This commit is contained in:
parent
d1fa48cb3b
commit
700548d63b
1 changed files with 38 additions and 1 deletions
|
@ -8,9 +8,16 @@ import os
|
||||||
import blake3
|
import blake3
|
||||||
|
|
||||||
from .forms import UploadFileForm
|
from .forms import UploadFileForm
|
||||||
from .models import Submission_File
|
from .models import User_Banner_Images, User_Profile_Images, Metadata_Files, Submission_File
|
||||||
|
|
||||||
|
|
||||||
|
MODEL_MAP = {
|
||||||
|
'user_profile': User_Profile_Images,
|
||||||
|
'user_banner': User_Banner_Images,
|
||||||
|
'submission': Submission_File,
|
||||||
|
'metadata': Metadata_Files,
|
||||||
|
}
|
||||||
|
|
||||||
def compute_file_hash(file):
|
def compute_file_hash(file):
|
||||||
'''
|
'''
|
||||||
Compute BLAKE3 hash of the file
|
Compute BLAKE3 hash of the file
|
||||||
|
@ -49,6 +56,36 @@ def serve_submission_file(request, file_hash):
|
||||||
return HttpResponse("File not found", status=404)
|
return HttpResponse("File not found", status=404)
|
||||||
|
|
||||||
|
|
||||||
|
@login_required(login_url="/login/")
|
||||||
|
def serve_content_file(request, folder, file_hash):
|
||||||
|
'''
|
||||||
|
View function to serve content files for download or inline viewing
|
||||||
|
'''
|
||||||
|
|
||||||
|
ModelClass = MODEL_MAP.get(folder)
|
||||||
|
if ModelClass is None:
|
||||||
|
return HttpResponse("Invalid folder", status=404)
|
||||||
|
|
||||||
|
download = request.GET.get('d')
|
||||||
|
|
||||||
|
try:
|
||||||
|
obj_file = get_object_or_404(ModelClass, file_hash=file_hash)
|
||||||
|
file = obj_file.file.file
|
||||||
|
file_name = obj_file.file_name
|
||||||
|
|
||||||
|
response = FileResponse(file)
|
||||||
|
if download == "1":
|
||||||
|
response['Content-Disposition'] = f'attachment; filename="{file_name}"'
|
||||||
|
else:
|
||||||
|
response['Content-Disposition'] = f'inline; filename="{file_name}"'
|
||||||
|
|
||||||
|
return response
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Error serving file: {e}")
|
||||||
|
return HttpResponse("File not found", status=404)
|
||||||
|
|
||||||
|
|
||||||
@login_required(login_url="/login/")
|
@login_required(login_url="/login/")
|
||||||
def fileUpload(request):
|
def fileUpload(request):
|
||||||
'''
|
'''
|
||||||
|
|
Loading…
Reference in a new issue