DocumentUploadController.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. public function __construct()
  14. {
  15. $this->documentModel = new DocumentModel();
  16. $this->storage = new DocumentStorageService();
  17. $this->maxUploadBytes = 30 * 1024 * 1024;
  18. }
  19. public function __invoke(ServerRequestInterface $request)
  20. {
  21. $userId = (int)($request->getAttribute('api_user_id') ?? 0);
  22. $companyId = (int)($request->getAttribute('api_company_id') ?? 0);
  23. if ($userId <= 0 || $companyId <= 0) {
  24. return ResponseLib::sendFail('Unauthorized', [], 'E_VALIDATE')->withStatus(401);
  25. }
  26. $contentLength = (int)$request->getHeaderLine('Content-Length');
  27. if ($contentLength > 0 && $contentLength > $this->maxUploadBytes) {
  28. return ResponseLib::sendFail('File too large. Max 30MB.', [], 'E_TOO_LARGE')->withStatus(413);
  29. }
  30. try {
  31. $parsed = MultipartFormDataParser::parse($request);
  32. } catch (\Throwable $e) {
  33. return ResponseLib::sendFail('Invalid multipart form-data: ' . $e->getMessage(), [], 'E_VALIDATE')->withStatus(400);
  34. }
  35. $fields = $parsed['fields'] ?? [];
  36. $files = $parsed['files'] ?? [];
  37. $documentType = isset($fields['document_type']) ? (string)$fields['document_type'] : '';
  38. if ($documentType === '') {
  39. return ResponseLib::sendFail('Missing field: document_type', [], 'E_VALIDATE')->withStatus(400);
  40. }
  41. $file = $files['file'] ?? null;
  42. if (!is_array($file) || !isset($file['content'])) {
  43. return ResponseLib::sendFail('Missing file field: file', [], 'E_VALIDATE')->withStatus(400);
  44. }
  45. $originalFilename = (string)($file['filename'] ?? 'upload.bin');
  46. $contentType = (string)($file['content_type'] ?? 'application/octet-stream');
  47. $content = (string)$file['content'];
  48. if (strlen($content) > $this->maxUploadBytes) {
  49. return ResponseLib::sendFail('File too large. Max 30MB.', [], 'E_TOO_LARGE')->withStatus(413);
  50. }
  51. try {
  52. $documentType = $this->storage->sanitizeDocumentType($documentType);
  53. $dir = $this->storage->ensureDirectory($companyId, $userId, $documentType);
  54. $storedFilename = $this->storage->buildStoredFilename($originalFilename, $contentType);
  55. $storedPath = $this->storage->writeFile($dir, $storedFilename, $content);
  56. $created = $this->documentModel->create($userId, $documentType, $storedPath);
  57. return ResponseLib::sendOk($created, 'S_CREATED');
  58. } catch (\Throwable $e) {
  59. return ResponseLib::sendFail('Upload failed: ' . $e->getMessage(), [], 'E_INTERNAL')->withStatus(500);
  60. }
  61. }
  62. }