DocumentDownloadController.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. namespace Controllers;
  3. use Libs\ResponseLib;
  4. use Models\DocumentModel;
  5. use Psr\Http\Message\ServerRequestInterface;
  6. use React\Http\Message\Response;
  7. use Respect\Validation\Exceptions\ValidationException;
  8. use Respect\Validation\Validator as val;
  9. class DocumentDownloadController
  10. {
  11. private DocumentModel $documentModel;
  12. public function __construct()
  13. {
  14. $this->documentModel = new DocumentModel();
  15. }
  16. public function __invoke(ServerRequestInterface $request)
  17. {
  18. $userId = (int)($request->getAttribute('api_user_id') ?? 0);
  19. $companyId = (int)($request->getAttribute('api_company_id') ?? 0);
  20. if ($userId <= 0 || $companyId <= 0) {
  21. return ResponseLib::sendFail('Unauthorized', [], 'E_VALIDATE')->withStatus(401);
  22. }
  23. $body = json_decode((string)$request->getBody(), true) ?? [];
  24. try {
  25. val::key('document_type', val::stringType()->notEmpty()->length(1, 255))
  26. ->assert($body);
  27. } catch (ValidationException $e) {
  28. return ResponseLib::sendFail('Validation failed: ' . $e->getFullMessage(), [], 'E_VALIDATE')->withStatus(400);
  29. }
  30. $documentType = (string)$body['document_type'];
  31. try {
  32. $doc = $this->documentModel->findLatestByUserIdAndType($userId, $documentType);
  33. } catch (\Throwable $e) {
  34. return ResponseLib::sendFail('Database error: ' . $e->getMessage(), [], 'E_DATABASE')->withStatus(500);
  35. }
  36. if (!$doc) {
  37. return ResponseLib::sendFail('Document not found', [], 'E_NOT_FOUND')->withStatus(404);
  38. }
  39. $path = (string)($doc['document_path'] ?? '');
  40. if ($path === '' || !is_file($path)) {
  41. return ResponseLib::sendFail('Document file not found', [], 'E_NOT_FOUND')->withStatus(404);
  42. }
  43. $filename = basename($path);
  44. $content = @file_get_contents($path);
  45. if ($content === false) {
  46. return ResponseLib::sendFail('Failed to read document file', [], 'E_INTERNAL')->withStatus(500);
  47. }
  48. $contentType = 'application/octet-stream';
  49. $ext = strtolower((string)pathinfo($filename, PATHINFO_EXTENSION));
  50. if ($ext === 'pdf') {
  51. $contentType = 'application/pdf';
  52. }
  53. return new Response(
  54. 200,
  55. [
  56. 'Content-Type' => $contentType,
  57. 'Content-Disposition' => 'attachment; filename="' . $filename . '"',
  58. ],
  59. $content
  60. );
  61. }
  62. }