api.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. /**
  3. * Admin upload endpoint used by the browser-side uploader (assets/admin.js).
  4. *
  5. * One multipart POST per image (X-CSRF-Token header, fields below); the webhost
  6. * streams the file straight to S3 and appends it to the gallery's JSON file.
  7. * Keeping it to a single file per request means each PHP process only ever
  8. * handles one image, so a multi-gigabyte gallery upload never trips
  9. * post_max_size / max_execution_time — only the largest single image does.
  10. *
  11. * Fields:
  12. * slug gallery slug
  13. * original the full-resolution file (required, stored unmodified)
  14. * thumb browser-generated JPEG thumbnail (optional; absent for RAW/video)
  15. * batch id of the selection this file came from (optional)
  16. * seq its position within that selection, so parallel uploads are
  17. * stored in the order they were picked, not the order they land
  18. * topic id of the topic to upload into (optional; empty = no topic)
  19. *
  20. * The browser never receives an S3 URL or any credential for writing.
  21. */
  22. require dirname(__DIR__) . '/app/bootstrap.php';
  23. if (!auth_check()) {
  24. json_response(['error' => 'Not authenticated'], 401);
  25. }
  26. if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
  27. json_response(['error' => 'POST only'], 405);
  28. }
  29. csrf_verify();
  30. // Release the session file before the slow S3 leg. PHP holds an exclusive lock
  31. // on it for the whole request, so without this every parallel upload from the
  32. // browser would queue behind the previous one and the uploader would be
  33. // serial again no matter how many requests it starts.
  34. session_write_close();
  35. // One image per request; a single file may still be large, so lift the time cap.
  36. @set_time_limit(0);
  37. // When a request body exceeds post_max_size, PHP discards $_POST and $_FILES
  38. // entirely — surface that as a clear 413 instead of a misleading "no file".
  39. if ((int)($_SERVER['CONTENT_LENGTH'] ?? 0) > 0 && !$_POST && !$_FILES) {
  40. json_response(['error' => 'Upload exceeds the server post_max_size limit'], 413);
  41. }
  42. $gallery = gallery_load((string)($_POST['slug'] ?? ''));
  43. if ($gallery === null) {
  44. json_response(['error' => 'Unknown gallery'], 404);
  45. }
  46. // The uploader may target a topic. Validate it here so a stale page cannot
  47. // write a reference to a topic that has since been deleted; gallery_append_image
  48. // re-checks under its lock for the same reason.
  49. $topic = (string)($_POST['topic'] ?? '');
  50. if ($topic !== '' && !isset(gallery_topic_map($gallery)[$topic])) {
  51. $topic = '';
  52. }
  53. // Trusted admin path: any file type is allowed (imagesOnly stays false).
  54. [$status, $payload] = gallery_store_s3_upload(
  55. $gallery,
  56. $_FILES['original'] ?? null,
  57. $_FILES['thumb'] ?? null,
  58. false,
  59. $_POST,
  60. $topic !== '' ? $topic : null
  61. );
  62. json_response($payload, $status);