1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ControleOnline\Controller; |
4
|
|
|
|
5
|
|
|
use ControleOnline\Entity\File; |
6
|
|
|
use Symfony\Component\HttpFoundation\HeaderUtils; |
|
|
|
|
7
|
|
|
use Symfony\Component\HttpFoundation\StreamedResponse; |
|
|
|
|
8
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
|
|
|
9
|
|
|
use Symfony\Component\HttpKernel\KernelInterface; |
|
|
|
|
10
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
|
|
|
11
|
|
|
|
12
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
|
|
|
|
13
|
|
|
|
14
|
|
|
|
15
|
|
|
|
16
|
|
|
class GetFileDataAction |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Entity Manager |
20
|
|
|
* |
21
|
|
|
* @var EntityManagerInterface |
22
|
|
|
*/ |
23
|
|
|
private $manager = null; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Synfony Kernel |
27
|
|
|
* |
28
|
|
|
* @var KernelInterface |
29
|
|
|
*/ |
30
|
|
|
private $kernel; |
31
|
|
|
|
32
|
|
|
public function __construct(EntityManagerInterface $entityManager, KernelInterface $appKernel) |
33
|
|
|
{ |
34
|
|
|
$this->kernel = $appKernel; |
35
|
|
|
$this->manager = $entityManager; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function __invoke(File $data, Request $request) |
39
|
|
|
{ |
40
|
|
|
|
41
|
|
|
try { |
42
|
|
|
$file = $data; |
43
|
|
|
//$file = $this->manager->getRepository(File::class)->findOneBy(['url' => $request->getPathInfo()]); |
44
|
|
|
if (!$file) |
|
|
|
|
45
|
|
|
throw new \Exception('Not found', 404); |
46
|
|
|
|
47
|
|
|
|
48
|
|
|
$content = $file->getContent(); |
49
|
|
|
$response = new StreamedResponse(function () use ($content) { |
50
|
|
|
fputs(fopen('php://output', 'wb'), $content); |
51
|
|
|
}); |
52
|
|
|
|
53
|
|
|
$fileType = $file->getFileType(); |
54
|
|
|
$ext = $file->getExtension(); |
55
|
|
|
if ($ext == 'svg') { |
56
|
|
|
$response->headers->set('Content-Type', 'image/svg+xml'); |
57
|
|
|
} else { |
58
|
|
|
$response->headers->set('Content-Type', "$fileType/$ext"); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
if ($fileType == 'image') |
62
|
|
|
$disposition = HeaderUtils::makeDisposition(HeaderUtils::DISPOSITION_INLINE, basename($request->getPathInfo())); |
63
|
|
|
else |
64
|
|
|
$disposition = HeaderUtils::makeDisposition(HeaderUtils::DISPOSITION_ATTACHMENT, basename($request->getPathInfo())); |
65
|
|
|
|
66
|
|
|
$response->headers->set('Content-Disposition', $disposition); |
67
|
|
|
|
68
|
|
|
return $response; |
69
|
|
|
} catch (\Exception $e) { |
70
|
|
|
return new JsonResponse([ |
71
|
|
|
'response' => [ |
72
|
|
|
'data' => [], |
73
|
|
|
'count' => 0, |
74
|
|
|
'error' => $e->getMessage(), |
75
|
|
|
'success' => false, |
76
|
|
|
], |
77
|
|
|
]); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths