Spaces:
Runtime error
Runtime error
Commit Β·
fd9b2eb
1
Parent(s): 767ed6d
fixed bb upload
Browse files- admin_app/views.py +194 -96
- pms_server/settings.py +22 -5
- test_bb_upload.py +89 -0
admin_app/views.py
CHANGED
|
@@ -302,49 +302,70 @@ def view_paperwork_file(request, paperwork_id, version_no, file_type):
|
|
| 302 |
if not (is_admin or is_owner):
|
| 303 |
return Response({'detail': 'Forbidden'}, status=status.HTTP_403_FORBIDDEN)
|
| 304 |
|
| 305 |
-
#
|
| 306 |
file_path = None
|
| 307 |
-
storage_path = None
|
| 308 |
if file_type == 'pdf':
|
| 309 |
file_path = version.pdf_path
|
| 310 |
-
storage_path = settings.PDF_STORAGE_PATH
|
| 311 |
elif file_type == 'tex':
|
| 312 |
file_path = version.latex_path
|
| 313 |
-
storage_path = settings.LATEX_STORAGE_PATH
|
| 314 |
elif file_type == 'zip':
|
| 315 |
file_path = version.python_path
|
| 316 |
-
storage_path = settings.PYTHON_STORAGE_PATH
|
| 317 |
elif file_type == 'docx':
|
| 318 |
file_path = version.docx_path
|
| 319 |
-
storage_path = settings.DOCX_STORAGE_PATH
|
| 320 |
else:
|
| 321 |
return Response({'error': 'Invalid file type'}, status=status.HTTP_400_BAD_REQUEST)
|
| 322 |
|
| 323 |
if not file_path:
|
| 324 |
raise Http404(f"{file_type.capitalize()} file not found for this version")
|
| 325 |
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 335 |
disposition = 'attachment' if request.GET.get('download') else 'inline'
|
| 336 |
response['Content-Disposition'] = f'{disposition}; filename="{filename}"'
|
|
|
|
|
|
|
| 337 |
return response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 338 |
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
disposition = 'attachment' if request.GET.get('download') else 'inline'
|
| 343 |
-
response['Content-Disposition'] = f'{disposition}; filename="{filename}"'
|
| 344 |
-
return response
|
| 345 |
|
| 346 |
-
|
| 347 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 348 |
|
| 349 |
@api_view(['GET'])
|
| 350 |
@permission_classes([IsAuthenticated, IsAdmin])
|
|
@@ -367,12 +388,7 @@ def view_zip_contents(request, paperwork_id, version_no):
|
|
| 367 |
return Response({'detail': 'Authentication credentials were not provided.'},
|
| 368 |
status=status.HTTP_401_UNAUTHORIZED)
|
| 369 |
|
| 370 |
-
#
|
| 371 |
-
# is_admin = getattr(request.user, 'role', '').upper() == 'ADMIN'
|
| 372 |
-
# if not is_admin:
|
| 373 |
-
# return Response({'detail': 'Forbidden'}, status=status.HTTP_403_FORBIDDEN)
|
| 374 |
-
|
| 375 |
-
# OR: allow the researcher who owns this paperwork to view too
|
| 376 |
try:
|
| 377 |
version = Version.objects.get(
|
| 378 |
paperwork__id=paperwork_id, version_no=version_no
|
|
@@ -385,24 +401,49 @@ def view_zip_contents(request, paperwork_id, version_no):
|
|
| 385 |
if not (is_admin or is_owner):
|
| 386 |
return Response({'detail': 'Forbidden'}, status=status.HTTP_403_FORBIDDEN)
|
| 387 |
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
|
| 391 |
-
|
| 392 |
-
|
| 393 |
-
|
| 394 |
-
|
| 395 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 396 |
|
| 397 |
-
|
| 398 |
-
|
| 399 |
|
| 400 |
-
|
| 401 |
-
|
| 402 |
-
|
| 403 |
-
|
| 404 |
|
| 405 |
-
|
| 406 |
|
| 407 |
@api_view(['GET'])
|
| 408 |
@permission_classes([AllowAny])
|
|
@@ -416,7 +457,7 @@ def view_zip_file_content(request, paperwork_id, version_no, file_path):
|
|
| 416 |
return Response({'detail': 'Authentication credentials were not provided.'},
|
| 417 |
status=status.HTTP_401_UNAUTHORIZED)
|
| 418 |
|
| 419 |
-
#
|
| 420 |
try:
|
| 421 |
version = Version.objects.get(
|
| 422 |
paperwork__id=paperwork_id, version_no=version_no
|
|
@@ -430,60 +471,117 @@ def view_zip_file_content(request, paperwork_id, version_no, file_path):
|
|
| 430 |
if not (is_admin or is_owner):
|
| 431 |
return Response({'detail': 'Forbidden'}, status=status.HTTP_403_FORBIDDEN)
|
| 432 |
|
| 433 |
-
|
| 434 |
-
|
| 435 |
-
|
| 436 |
-
settings.PYTHON_STORAGE_PATH,
|
| 437 |
-
str(paperwork_id),
|
| 438 |
-
version_str,
|
| 439 |
-
"code.zip"
|
| 440 |
-
)
|
| 441 |
-
|
| 442 |
-
if not os.path.exists(zip_path):
|
| 443 |
-
raise Http404("ZIP file not found")
|
| 444 |
|
|
|
|
|
|
|
|
|
|
| 445 |
try:
|
| 446 |
-
|
| 447 |
-
|
| 448 |
-
|
| 449 |
-
|
| 450 |
-
|
| 451 |
-
|
| 452 |
-
|
| 453 |
-
|
| 454 |
-
|
| 455 |
-
|
| 456 |
-
|
| 457 |
-
|
| 458 |
-
|
| 459 |
-
|
| 460 |
-
|
| 461 |
-
|
| 462 |
-
|
| 463 |
-
|
| 464 |
-
|
| 465 |
-
|
| 466 |
-
|
| 467 |
-
|
| 468 |
-
|
| 469 |
-
|
| 470 |
-
|
| 471 |
-
|
| 472 |
-
|
| 473 |
-
|
| 474 |
-
|
| 475 |
-
|
| 476 |
-
|
| 477 |
-
|
| 478 |
-
|
| 479 |
-
|
| 480 |
-
|
| 481 |
-
|
| 482 |
-
|
| 483 |
-
|
| 484 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 485 |
except Exception as e:
|
| 486 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 487 |
|
| 488 |
def download_paperwork_file(request, pk, version_no, file_type):
|
| 489 |
# Try token auth first
|
|
|
|
| 302 |
if not (is_admin or is_owner):
|
| 303 |
return Response({'detail': 'Forbidden'}, status=status.HTTP_403_FORBIDDEN)
|
| 304 |
|
| 305 |
+
# Get the file path from the version based on file type
|
| 306 |
file_path = None
|
|
|
|
| 307 |
if file_type == 'pdf':
|
| 308 |
file_path = version.pdf_path
|
|
|
|
| 309 |
elif file_type == 'tex':
|
| 310 |
file_path = version.latex_path
|
|
|
|
| 311 |
elif file_type == 'zip':
|
| 312 |
file_path = version.python_path
|
|
|
|
| 313 |
elif file_type == 'docx':
|
| 314 |
file_path = version.docx_path
|
|
|
|
| 315 |
else:
|
| 316 |
return Response({'error': 'Invalid file type'}, status=status.HTTP_400_BAD_REQUEST)
|
| 317 |
|
| 318 |
if not file_path:
|
| 319 |
raise Http404(f"{file_type.capitalize()} file not found for this version")
|
| 320 |
|
| 321 |
+
# Use Django's default storage to access files from Backblaze B2
|
| 322 |
+
from django.core.files.storage import default_storage
|
| 323 |
+
from django.http import HttpResponse
|
| 324 |
+
import mimetypes
|
| 325 |
+
|
| 326 |
+
try:
|
| 327 |
+
# Check if file exists in storage (Backblaze B2)
|
| 328 |
+
if not default_storage.exists(file_path):
|
| 329 |
+
logger.error(f"File not found in B2 storage: {file_path}")
|
| 330 |
+
raise Http404("File not found in storage")
|
| 331 |
+
|
| 332 |
+
# Get the file from storage
|
| 333 |
+
file_obj = default_storage.open(file_path, 'rb')
|
| 334 |
+
|
| 335 |
+
# Determine content type
|
| 336 |
+
content_type, _ = mimetypes.guess_type(file_path)
|
| 337 |
+
if not content_type:
|
| 338 |
+
content_type = 'application/octet-stream'
|
| 339 |
+
|
| 340 |
+
# Create response with file content
|
| 341 |
+
response = HttpResponse(file_obj.read(), content_type=content_type)
|
| 342 |
+
|
| 343 |
+
# Set filename for download
|
| 344 |
+
filename = os.path.basename(file_path) or f"paper.{file_type}"
|
| 345 |
disposition = 'attachment' if request.GET.get('download') else 'inline'
|
| 346 |
response['Content-Disposition'] = f'{disposition}; filename="{filename}"'
|
| 347 |
+
|
| 348 |
+
file_obj.close()
|
| 349 |
return response
|
| 350 |
+
|
| 351 |
+
except Exception as e:
|
| 352 |
+
logger.error(f"Error accessing file {file_path} from B2 storage: {str(e)}")
|
| 353 |
+
# Fallback: try local file system as backup
|
| 354 |
+
filename = os.path.basename(file_path) if file_path else f"paper.{file_type}"
|
| 355 |
+
version_str = f"v{version_no}"
|
| 356 |
|
| 357 |
+
storage_full_path = os.path.join(
|
| 358 |
+
settings.MEDIA_ROOT, file_path
|
| 359 |
+
)
|
|
|
|
|
|
|
|
|
|
| 360 |
|
| 361 |
+
if os.path.exists(storage_full_path):
|
| 362 |
+
response = FileResponse(open(storage_full_path, 'rb'))
|
| 363 |
+
disposition = 'attachment' if request.GET.get('download') else 'inline'
|
| 364 |
+
response['Content-Disposition'] = f'{disposition}; filename="{filename}"'
|
| 365 |
+
return response
|
| 366 |
+
|
| 367 |
+
logger.error(f"File not found in both B2 storage and local filesystem: {file_path}")
|
| 368 |
+
raise Http404("File not found")
|
| 369 |
|
| 370 |
@api_view(['GET'])
|
| 371 |
@permission_classes([IsAuthenticated, IsAdmin])
|
|
|
|
| 388 |
return Response({'detail': 'Authentication credentials were not provided.'},
|
| 389 |
status=status.HTTP_401_UNAUTHORIZED)
|
| 390 |
|
| 391 |
+
# Get version from database
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 392 |
try:
|
| 393 |
version = Version.objects.get(
|
| 394 |
paperwork__id=paperwork_id, version_no=version_no
|
|
|
|
| 401 |
if not (is_admin or is_owner):
|
| 402 |
return Response({'detail': 'Forbidden'}, status=status.HTTP_403_FORBIDDEN)
|
| 403 |
|
| 404 |
+
# Get the python ZIP file path
|
| 405 |
+
if not version.python_path:
|
| 406 |
+
raise Http404("ZIP file not found for this version")
|
| 407 |
+
|
| 408 |
+
# Use Django's default storage to access ZIP from Backblaze B2
|
| 409 |
+
from django.core.files.storage import default_storage
|
| 410 |
+
import tempfile
|
| 411 |
+
|
| 412 |
+
try:
|
| 413 |
+
# Check if file exists in storage
|
| 414 |
+
if not default_storage.exists(version.python_path):
|
| 415 |
+
raise Http404("ZIP file not found in storage")
|
| 416 |
+
|
| 417 |
+
# Download ZIP file to temporary location for processing
|
| 418 |
+
with default_storage.open(version.python_path, 'rb') as zip_file:
|
| 419 |
+
contents = []
|
| 420 |
+
with zipfile.ZipFile(zip_file, 'r') as z:
|
| 421 |
+
for name in z.namelist():
|
| 422 |
+
contents.append(name)
|
| 423 |
+
|
| 424 |
+
return JsonResponse({"files": contents})
|
| 425 |
+
|
| 426 |
+
except Exception as e:
|
| 427 |
+
logger.error(f"Error accessing ZIP file {version.python_path} from B2 storage: {str(e)}")
|
| 428 |
+
# Fallback to local file system
|
| 429 |
+
version_str = f"v{version_no}"
|
| 430 |
+
zip_path = os.path.join(
|
| 431 |
+
settings.MEDIA_ROOT,
|
| 432 |
+
settings.PYTHON_STORAGE_PATH,
|
| 433 |
+
str(paperwork_id),
|
| 434 |
+
version_str,
|
| 435 |
+
"code.zip"
|
| 436 |
+
)
|
| 437 |
|
| 438 |
+
if not os.path.exists(zip_path):
|
| 439 |
+
raise Http404("ZIP file not found")
|
| 440 |
|
| 441 |
+
contents = []
|
| 442 |
+
with zipfile.ZipFile(zip_path, 'r') as z:
|
| 443 |
+
for name in z.namelist():
|
| 444 |
+
contents.append(name)
|
| 445 |
|
| 446 |
+
return JsonResponse({"files": contents})
|
| 447 |
|
| 448 |
@api_view(['GET'])
|
| 449 |
@permission_classes([AllowAny])
|
|
|
|
| 457 |
return Response({'detail': 'Authentication credentials were not provided.'},
|
| 458 |
status=status.HTTP_401_UNAUTHORIZED)
|
| 459 |
|
| 460 |
+
# Get version from database
|
| 461 |
try:
|
| 462 |
version = Version.objects.get(
|
| 463 |
paperwork__id=paperwork_id, version_no=version_no
|
|
|
|
| 471 |
if not (is_admin or is_owner):
|
| 472 |
return Response({'detail': 'Forbidden'}, status=status.HTTP_403_FORBIDDEN)
|
| 473 |
|
| 474 |
+
# Get the python ZIP file path
|
| 475 |
+
if not version.python_path:
|
| 476 |
+
raise Http404("ZIP file not found for this version")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 477 |
|
| 478 |
+
# Use Django's default storage to access ZIP from Backblaze B2
|
| 479 |
+
from django.core.files.storage import default_storage
|
| 480 |
+
|
| 481 |
try:
|
| 482 |
+
# Check if file exists in storage
|
| 483 |
+
if not default_storage.exists(version.python_path):
|
| 484 |
+
raise Http404("ZIP file not found in storage")
|
| 485 |
+
|
| 486 |
+
# Access ZIP file from storage
|
| 487 |
+
with default_storage.open(version.python_path, 'rb') as zip_file:
|
| 488 |
+
with zipfile.ZipFile(zip_file, 'r') as z:
|
| 489 |
+
# Check if the file exists in the ZIP
|
| 490 |
+
if file_path not in z.namelist():
|
| 491 |
+
return Response({'error': 'File not found in ZIP archive'}, status=status.HTTP_404_NOT_FOUND)
|
| 492 |
+
|
| 493 |
+
# Determine content type based on file extension
|
| 494 |
+
file_extension = file_path.split('.')[-1].lower() if '.' in file_path else ''
|
| 495 |
+
content_type = 'text/plain'
|
| 496 |
+
is_binary = False
|
| 497 |
+
|
| 498 |
+
# Text-based files
|
| 499 |
+
if file_extension in ['py']:
|
| 500 |
+
content_type = 'text/x-python'
|
| 501 |
+
elif file_extension in ['ipynb']:
|
| 502 |
+
content_type = 'application/json'
|
| 503 |
+
# Image files
|
| 504 |
+
elif file_extension in ['png']:
|
| 505 |
+
content_type = 'image/png'
|
| 506 |
+
is_binary = True
|
| 507 |
+
elif file_extension in ['jpg', 'jpeg']:
|
| 508 |
+
content_type = 'image/jpeg'
|
| 509 |
+
is_binary = True
|
| 510 |
+
|
| 511 |
+
# Handle binary files (images)
|
| 512 |
+
if is_binary:
|
| 513 |
+
file_content = base64.b64encode(z.read(file_path)).decode('utf-8')
|
| 514 |
+
return Response({
|
| 515 |
+
'content': file_content,
|
| 516 |
+
'content_type': content_type,
|
| 517 |
+
'is_binary': True
|
| 518 |
+
})
|
| 519 |
+
else:
|
| 520 |
+
# Handle text files
|
| 521 |
+
file_content = z.read(file_path).decode('utf-8', errors='replace')
|
| 522 |
+
return Response({
|
| 523 |
+
'content': file_content,
|
| 524 |
+
'content_type': content_type,
|
| 525 |
+
'is_binary': False
|
| 526 |
+
})
|
| 527 |
+
|
| 528 |
except Exception as e:
|
| 529 |
+
logger.error(f"Error accessing ZIP file {version.python_path} from B2 storage: {str(e)}")
|
| 530 |
+
# Fallback to local file system
|
| 531 |
+
version_str = f"v{version_no}"
|
| 532 |
+
zip_path = os.path.join(
|
| 533 |
+
settings.MEDIA_ROOT,
|
| 534 |
+
settings.PYTHON_STORAGE_PATH,
|
| 535 |
+
str(paperwork_id),
|
| 536 |
+
version_str,
|
| 537 |
+
"code.zip"
|
| 538 |
+
)
|
| 539 |
+
|
| 540 |
+
if not os.path.exists(zip_path):
|
| 541 |
+
raise Http404("ZIP file not found")
|
| 542 |
+
|
| 543 |
+
try:
|
| 544 |
+
with zipfile.ZipFile(zip_path, 'r') as z:
|
| 545 |
+
# Check if the file exists in the ZIP
|
| 546 |
+
if file_path not in z.namelist():
|
| 547 |
+
return Response({'error': 'File not found in ZIP archive'}, status=status.HTTP_404_NOT_FOUND)
|
| 548 |
+
|
| 549 |
+
# Determine content type based on file extension
|
| 550 |
+
file_extension = file_path.split('.')[-1].lower() if '.' in file_path else ''
|
| 551 |
+
content_type = 'text/plain'
|
| 552 |
+
is_binary = False
|
| 553 |
+
|
| 554 |
+
# Text-based files
|
| 555 |
+
if file_extension in ['py']:
|
| 556 |
+
content_type = 'text/x-python'
|
| 557 |
+
elif file_extension in ['ipynb']:
|
| 558 |
+
content_type = 'application/json'
|
| 559 |
+
# Image files
|
| 560 |
+
elif file_extension in ['png']:
|
| 561 |
+
content_type = 'image/png'
|
| 562 |
+
is_binary = True
|
| 563 |
+
elif file_extension in ['jpg', 'jpeg']:
|
| 564 |
+
content_type = 'image/jpeg'
|
| 565 |
+
is_binary = True
|
| 566 |
+
|
| 567 |
+
# Handle binary files (images)
|
| 568 |
+
if is_binary:
|
| 569 |
+
file_content = base64.b64encode(z.read(file_path)).decode('utf-8')
|
| 570 |
+
return Response({
|
| 571 |
+
'content': file_content,
|
| 572 |
+
'content_type': content_type,
|
| 573 |
+
'is_binary': True
|
| 574 |
+
})
|
| 575 |
+
else:
|
| 576 |
+
# Handle text files
|
| 577 |
+
file_content = z.read(file_path).decode('utf-8', errors='replace')
|
| 578 |
+
return Response({
|
| 579 |
+
'content': file_content,
|
| 580 |
+
'content_type': content_type,
|
| 581 |
+
'is_binary': False
|
| 582 |
+
})
|
| 583 |
+
except Exception as e:
|
| 584 |
+
return Response({'error': str(e)}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
|
| 585 |
|
| 586 |
def download_paperwork_file(request, pk, version_no, file_type):
|
| 587 |
# Try token auth first
|
pms_server/settings.py
CHANGED
|
@@ -240,10 +240,6 @@ USE_I18N = True
|
|
| 240 |
|
| 241 |
USE_TZ = True
|
| 242 |
|
| 243 |
-
# Use S3Boto3 backend for file storage (Backblaze B2)
|
| 244 |
-
DEFAULT_FILE_STORAGE = "storages.backends.s3boto3.S3Boto3Storage"
|
| 245 |
-
STATICFILES_STORAGE = "storages.backends.s3boto3.S3StaticStorage"
|
| 246 |
-
|
| 247 |
# Backblaze B2 credentials
|
| 248 |
AWS_ACCESS_KEY_ID = os.getenv("B2_KEY_ID") # from App Keys
|
| 249 |
AWS_SECRET_ACCESS_KEY = os.getenv("B2_APP_KEY") # from App Keys
|
|
@@ -268,7 +264,28 @@ if not all([AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_STORAGE_BUCKET_NAME, A
|
|
| 268 |
RuntimeWarning
|
| 269 |
)
|
| 270 |
# Fall back to local storage if B2 is not configured
|
| 271 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 272 |
|
| 273 |
# Static files (CSS, JavaScript, Images)
|
| 274 |
# https://docs.djangoproject.com/en/5.2/howto/static-files/
|
|
|
|
| 240 |
|
| 241 |
USE_TZ = True
|
| 242 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 243 |
# Backblaze B2 credentials
|
| 244 |
AWS_ACCESS_KEY_ID = os.getenv("B2_KEY_ID") # from App Keys
|
| 245 |
AWS_SECRET_ACCESS_KEY = os.getenv("B2_APP_KEY") # from App Keys
|
|
|
|
| 264 |
RuntimeWarning
|
| 265 |
)
|
| 266 |
# Fall back to local storage if B2 is not configured
|
| 267 |
+
STORAGES = {
|
| 268 |
+
"default": {
|
| 269 |
+
"BACKEND": "django.core.files.storage.FileSystemStorage",
|
| 270 |
+
},
|
| 271 |
+
"staticfiles": {
|
| 272 |
+
"BACKEND": "django.contrib.staticfiles.storage.StaticFilesStorage",
|
| 273 |
+
},
|
| 274 |
+
}
|
| 275 |
+
else:
|
| 276 |
+
# Use Django 5.2+ STORAGES setting for Backblaze B2
|
| 277 |
+
STORAGES = {
|
| 278 |
+
"default": {
|
| 279 |
+
"BACKEND": "storages.backends.s3boto3.S3Boto3Storage",
|
| 280 |
+
},
|
| 281 |
+
"staticfiles": {
|
| 282 |
+
"BACKEND": "storages.backends.s3boto3.S3StaticStorage",
|
| 283 |
+
},
|
| 284 |
+
}
|
| 285 |
+
|
| 286 |
+
# Legacy settings for compatibility (Django < 4.2)
|
| 287 |
+
DEFAULT_FILE_STORAGE = "storages.backends.s3boto3.S3Boto3Storage"
|
| 288 |
+
STATICFILES_STORAGE = "storages.backends.s3boto3.S3StaticStorage"
|
| 289 |
|
| 290 |
# Static files (CSS, JavaScript, Images)
|
| 291 |
# https://docs.djangoproject.com/en/5.2/howto/static-files/
|
test_bb_upload.py
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Test script to check B2 storage configuration and force reinitialize storage
|
| 2 |
+
import os
|
| 3 |
+
import django
|
| 4 |
+
from pathlib import Path
|
| 5 |
+
from dotenv import load_dotenv
|
| 6 |
+
|
| 7 |
+
# Load environment variables FIRST
|
| 8 |
+
load_dotenv()
|
| 9 |
+
|
| 10 |
+
# Set up Django environment
|
| 11 |
+
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'pms_server.settings')
|
| 12 |
+
django.setup()
|
| 13 |
+
|
| 14 |
+
from django.conf import settings
|
| 15 |
+
from django.core.files.storage import default_storage
|
| 16 |
+
from django.core.files.base import ContentFile
|
| 17 |
+
|
| 18 |
+
# Check if all B2 environment variables are loaded
|
| 19 |
+
print("=== B2 Configuration Check ===")
|
| 20 |
+
print(f"B2_KEY_ID: {'β
' if os.getenv('B2_KEY_ID') else 'β'} {os.getenv('B2_KEY_ID', 'NOT SET')}")
|
| 21 |
+
print(f"B2_APP_KEY: {'β
' if os.getenv('B2_APP_KEY') else 'β'} {'***HIDDEN***' if os.getenv('B2_APP_KEY') else 'NOT SET'}")
|
| 22 |
+
print(f"B2_BUCKET_NAME: {'β
' if os.getenv('B2_BUCKET_NAME') else 'β'} {os.getenv('B2_BUCKET_NAME', 'NOT SET')}")
|
| 23 |
+
print(f"B2_ENDPOINT_URL: {'β
' if os.getenv('B2_ENDPOINT_URL') else 'β'} {os.getenv('B2_ENDPOINT_URL', 'NOT SET')}")
|
| 24 |
+
|
| 25 |
+
# Check Django settings AWS variables
|
| 26 |
+
print(f"\n=== Django AWS Settings ===")
|
| 27 |
+
print(f"AWS_ACCESS_KEY_ID: {'β
' if settings.AWS_ACCESS_KEY_ID else 'β'} {settings.AWS_ACCESS_KEY_ID or 'NOT SET'}")
|
| 28 |
+
print(f"AWS_SECRET_ACCESS_KEY: {'β
' if settings.AWS_SECRET_ACCESS_KEY else 'β'} {'***HIDDEN***' if settings.AWS_SECRET_ACCESS_KEY else 'NOT SET'}")
|
| 29 |
+
print(f"AWS_STORAGE_BUCKET_NAME: {'β
' if settings.AWS_STORAGE_BUCKET_NAME else 'β'} {settings.AWS_STORAGE_BUCKET_NAME or 'NOT SET'}")
|
| 30 |
+
print(f"AWS_S3_ENDPOINT_URL: {'β
' if settings.AWS_S3_ENDPOINT_URL else 'β'} {settings.AWS_S3_ENDPOINT_URL or 'NOT SET'}")
|
| 31 |
+
|
| 32 |
+
print(f"\nDEFAULT_FILE_STORAGE: {settings.DEFAULT_FILE_STORAGE}")
|
| 33 |
+
print(f"default_storage class: {default_storage.__class__.__name__}")
|
| 34 |
+
|
| 35 |
+
# Direct S3 storage test
|
| 36 |
+
try:
|
| 37 |
+
from storages.backends.s3boto3 import S3Boto3Storage
|
| 38 |
+
|
| 39 |
+
# Create S3 storage instance directly
|
| 40 |
+
s3_storage = S3Boto3Storage()
|
| 41 |
+
print(f"\n=== Direct S3 Storage Test ===")
|
| 42 |
+
print(f"S3 storage class: {s3_storage.__class__.__name__}")
|
| 43 |
+
|
| 44 |
+
test_content = ContentFile(b"Test file content for direct S3 upload")
|
| 45 |
+
test_path = "test_files/direct_s3_test.txt"
|
| 46 |
+
|
| 47 |
+
saved_path = s3_storage.save(test_path, test_content)
|
| 48 |
+
print(f"β
File saved to: {saved_path}")
|
| 49 |
+
|
| 50 |
+
if s3_storage.exists(saved_path):
|
| 51 |
+
print(f"β
File exists in S3 storage")
|
| 52 |
+
print(f"File URL: {s3_storage.url(saved_path)}")
|
| 53 |
+
|
| 54 |
+
# Clean up test file
|
| 55 |
+
s3_storage.delete(saved_path)
|
| 56 |
+
print(f"β
Test file cleaned up")
|
| 57 |
+
else:
|
| 58 |
+
print(f"β File not found in S3 storage")
|
| 59 |
+
|
| 60 |
+
except Exception as e:
|
| 61 |
+
print(f"β Direct S3 storage test failed: {str(e)}")
|
| 62 |
+
import traceback
|
| 63 |
+
print(f"Full error: {traceback.format_exc()}")
|
| 64 |
+
|
| 65 |
+
# Test with default_storage
|
| 66 |
+
try:
|
| 67 |
+
print(f"\n=== Testing with default_storage ===")
|
| 68 |
+
print(f"default_storage class: {default_storage.__class__.__name__}")
|
| 69 |
+
|
| 70 |
+
test_content2 = ContentFile(b"Test file content for default storage")
|
| 71 |
+
test_path2 = "test_files/default_storage_test.txt"
|
| 72 |
+
|
| 73 |
+
saved_path2 = default_storage.save(test_path2, test_content2)
|
| 74 |
+
print(f"β
File saved to: {saved_path2}")
|
| 75 |
+
|
| 76 |
+
if default_storage.exists(saved_path2):
|
| 77 |
+
print(f"β
File exists in storage")
|
| 78 |
+
print(f"File URL: {default_storage.url(saved_path2)}")
|
| 79 |
+
|
| 80 |
+
# Clean up test file
|
| 81 |
+
default_storage.delete(saved_path2)
|
| 82 |
+
print(f"β
Test file cleaned up")
|
| 83 |
+
else:
|
| 84 |
+
print(f"β File not found in storage")
|
| 85 |
+
|
| 86 |
+
except Exception as e:
|
| 87 |
+
print(f"β Default storage test failed: {str(e)}")
|
| 88 |
+
import traceback
|
| 89 |
+
print(f"Full error: {traceback.format_exc()}")
|