Compare commits

...

2 commits

2 changed files with 39 additions and 2 deletions

View file

@ -3,7 +3,7 @@ from . import views
urlpatterns = [ urlpatterns = [
# Add a URL pattern that captures the file path # Add a URL pattern that captures the file path
path('submission/<str:file_hash>', views.serve_submission_file, name='serve_submission_file'), path('<folder>/<str:file_hash>', views.serve_content_file, name='serve_content_file'),
# Other URL patterns if any # Other URL patterns if any
path('upload/', views.fileUpload, name='file_upload'), path('upload/', views.fileUpload, name='file_upload'),
] ]

View file

@ -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):
''' '''