vault backup: 2025-12-26 02:09:22
All checks were successful
Deploy Quartz site to GitHub Pages / build (push) Successful in 2m29s
All checks were successful
Deploy Quartz site to GitHub Pages / build (push) Successful in 2m29s
This commit is contained in:
48
stroma/file/views/content_api_view.py
Normal file
48
stroma/file/views/content_api_view.py
Normal file
@@ -0,0 +1,48 @@
|
||||
import json
|
||||
from django.http import JsonResponse
|
||||
from django.views.decorators.http import require_http_methods
|
||||
from django.shortcuts import get_object_or_404
|
||||
from file.models import File
|
||||
|
||||
@require_http_methods(["GET"])
|
||||
def get_file_content(request, file_id):
|
||||
"""Get the text content of a specific file."""
|
||||
file_obj = get_object_or_404(File, id=file_id, user=request.quiz_user)
|
||||
return JsonResponse({
|
||||
'id': file_obj.id,
|
||||
'name': file_obj.name,
|
||||
'content': file_obj.text
|
||||
})
|
||||
|
||||
@require_http_methods(["POST"])
|
||||
def save_file_content(request, file_id):
|
||||
"""Save updated text content to a file."""
|
||||
file_obj = get_object_or_404(File, id=file_id, user=request.quiz_user)
|
||||
try:
|
||||
data = json.loads(request.body)
|
||||
new_content = data.get('content', '')
|
||||
file_obj.text = new_content
|
||||
file_obj.save()
|
||||
return JsonResponse({'success': True})
|
||||
except (json.JSONDecodeError, KeyError):
|
||||
return JsonResponse({'success': False, 'error': 'Invalid data'}, status=400)
|
||||
|
||||
|
||||
from django.http import FileResponse
|
||||
|
||||
def serve_pdf_api(request, file_id):
|
||||
"""Serve the raw PDF file for viewing."""
|
||||
file_obj = get_object_or_404(File, id=file_id, user=request.quiz_user)
|
||||
|
||||
if not file_obj.mime_type == 'application/pdf' or not file_obj.file_content:
|
||||
return JsonResponse({'error': 'Not a PDF file'}, status=400)
|
||||
|
||||
response = FileResponse(
|
||||
file_obj.file_content.open('rb'),
|
||||
content_type='application/pdf'
|
||||
)
|
||||
# Add CORS headers to allow CDN-hosted PDF.js viewer to fetch the PDF
|
||||
response['Access-Control-Allow-Origin'] = '*'
|
||||
response['Access-Control-Allow-Methods'] = 'GET, OPTIONS'
|
||||
response['Access-Control-Allow-Headers'] = 'Content-Type'
|
||||
return response
|
||||
Reference in New Issue
Block a user