DocumentUploadController.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. namespace Controllers;
  3. use Libs\MultipartFormDataParser;
  4. use Libs\ResponseLib;
  5. use Models\DocumentModel;
  6. use Psr\Http\Message\ServerRequestInterface;
  7. use Services\DocumentStorageService;
  8. class DocumentUploadController
  9. {
  10. private DocumentModel $documentModel;
  11. private DocumentStorageService $storage;
  12. private int $maxUploadBytes;
  13. private bool $debug;
  14. public function __construct()
  15. {
  16. $this->documentModel = new DocumentModel();
  17. $this->storage = new DocumentStorageService();
  18. $this->maxUploadBytes = 30 * 1024 * 1024;
  19. $this->debug = filter_var($_ENV['DOCUMENT_UPLOAD_DEBUG'] ?? 'true', FILTER_VALIDATE_BOOLEAN);
  20. }
  21. private function log(string $requestId, string $message, array $context = []): void
  22. {
  23. if (!$this->debug) {
  24. return;
  25. }
  26. $json = '';
  27. if (!empty($context)) {
  28. $json = ' ' . json_encode($context, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
  29. }
  30. error_log('[documents.upload][' . $requestId . '] ' . $message . $json);
  31. }
  32. public function __invoke(ServerRequestInterface $request)
  33. {
  34. $requestId = bin2hex(random_bytes(4));
  35. $userId = (int)($request->getAttribute('api_user_id') ?? 0);
  36. $companyId = (int)($request->getAttribute('api_company_id') ?? 0);
  37. if ($userId <= 0 || $companyId <= 0) {
  38. return ResponseLib::sendFail('Unauthorized', [], 'E_VALIDATE')->withStatus(401);
  39. }
  40. $contentLength = (int)$request->getHeaderLine('Content-Length');
  41. if ($contentLength > 0 && $contentLength > $this->maxUploadBytes) {
  42. $this->log($requestId, 'content-length exceeded', [
  43. 'user_id' => $userId,
  44. 'company_id' => $companyId,
  45. 'content_length' => $contentLength,
  46. 'content_type' => $request->getHeaderLine('Content-Type'),
  47. 'user_agent' => $request->getHeaderLine('User-Agent'),
  48. 'x_forwarded_for' => $request->getHeaderLine('X-Forwarded-For'),
  49. 'x_forwarded_proto' => $request->getHeaderLine('X-Forwarded-Proto'),
  50. ]);
  51. return ResponseLib::sendFail('File too large. Max 30MB.', ['request_id' => $requestId], 'E_TOO_LARGE')->withStatus(413);
  52. }
  53. try {
  54. $parsed = MultipartFormDataParser::parse($request);
  55. } catch (\Throwable $e) {
  56. $this->log($requestId, 'multipart parse failed', [
  57. 'user_id' => $userId,
  58. 'company_id' => $companyId,
  59. 'error' => $e->getMessage(),
  60. 'content_type' => $request->getHeaderLine('Content-Type'),
  61. 'content_length' => $request->getHeaderLine('Content-Length'),
  62. 'transfer_encoding' => $request->getHeaderLine('Transfer-Encoding'),
  63. 'user_agent' => $request->getHeaderLine('User-Agent'),
  64. 'x_forwarded_for' => $request->getHeaderLine('X-Forwarded-For'),
  65. 'x_forwarded_proto' => $request->getHeaderLine('X-Forwarded-Proto'),
  66. ]);
  67. return ResponseLib::sendFail('Invalid multipart form-data: ' . $e->getMessage(), ['request_id' => $requestId], 'E_VALIDATE')->withStatus(400);
  68. }
  69. $fields = $parsed['fields'] ?? [];
  70. $files = $parsed['files'] ?? [];
  71. $meta = $parsed['meta'] ?? [];
  72. $documentType = isset($fields['document_type']) ? (string)$fields['document_type'] : '';
  73. if ($documentType === '') {
  74. $this->log($requestId, 'missing document_type', [
  75. 'user_id' => $userId,
  76. 'company_id' => $companyId,
  77. 'field_names' => array_keys($fields),
  78. 'file_names' => array_keys($files),
  79. 'meta' => $meta,
  80. 'content_type' => $request->getHeaderLine('Content-Type'),
  81. 'content_length' => $request->getHeaderLine('Content-Length'),
  82. 'transfer_encoding' => $request->getHeaderLine('Transfer-Encoding'),
  83. 'user_agent' => $request->getHeaderLine('User-Agent'),
  84. 'x_forwarded_for' => $request->getHeaderLine('X-Forwarded-For'),
  85. 'x_forwarded_proto' => $request->getHeaderLine('X-Forwarded-Proto'),
  86. ]);
  87. $data = ['request_id' => $requestId];
  88. if ($this->debug) {
  89. $data['debug'] = [
  90. 'field_names' => array_keys($fields),
  91. 'file_names' => array_keys($files),
  92. 'meta' => $meta,
  93. 'content_type' => $request->getHeaderLine('Content-Type'),
  94. 'content_length' => $request->getHeaderLine('Content-Length'),
  95. 'transfer_encoding' => $request->getHeaderLine('Transfer-Encoding'),
  96. ];
  97. }
  98. return ResponseLib::sendFail('Missing field: document_type', $data, 'E_VALIDATE')->withStatus(400);
  99. }
  100. $file = $files['file'] ?? null;
  101. if (!is_array($file) || !isset($file['content'])) {
  102. $this->log($requestId, 'missing file', [
  103. 'user_id' => $userId,
  104. 'company_id' => $companyId,
  105. 'field_names' => array_keys($fields),
  106. 'file_names' => array_keys($files),
  107. 'meta' => $meta,
  108. ]);
  109. $data = ['request_id' => $requestId];
  110. if ($this->debug) {
  111. $data['debug'] = [
  112. 'field_names' => array_keys($fields),
  113. 'file_names' => array_keys($files),
  114. 'meta' => $meta,
  115. ];
  116. }
  117. return ResponseLib::sendFail('Missing file field: file', $data, 'E_VALIDATE')->withStatus(400);
  118. }
  119. $originalFilename = (string)($file['filename'] ?? 'upload.bin');
  120. $contentType = (string)($file['content_type'] ?? 'application/octet-stream');
  121. $content = (string)$file['content'];
  122. if (strlen($content) > $this->maxUploadBytes) {
  123. return ResponseLib::sendFail('File too large. Max 30MB.', ['request_id' => $requestId], 'E_TOO_LARGE')->withStatus(413);
  124. }
  125. try {
  126. $documentType = $this->storage->sanitizeDocumentType($documentType);
  127. $dir = $this->storage->ensureDirectory($companyId, $userId, $documentType);
  128. $storedFilename = $this->storage->buildStoredFilename($originalFilename, $contentType);
  129. $storedPath = $this->storage->writeFile($dir, $storedFilename, $content);
  130. $created = $this->documentModel->create($userId, $documentType, $storedPath);
  131. return ResponseLib::sendOk($created, 'S_CREATED');
  132. } catch (\Throwable $e) {
  133. return ResponseLib::sendFail('Upload failed: ' . $e->getMessage(), [], 'E_INTERNAL')->withStatus(500);
  134. }
  135. }
  136. }