Proyectos de Subversion LeadersLinked - Services

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
declare(strict_types=1);
3
 
4
namespace LeadersLinked\Controller;
5
 
6
use Laminas\Db\Adapter\AdapterInterface;
7
 
8
use Laminas\Mvc\Controller\AbstractActionController;
9
use Laminas\Log\LoggerInterface;
10
 
11
 
12
class StorageNetworkController extends AbstractActionController
13
{
14
    /**
15
     *
16
     * @var \Laminas\Db\Adapter\AdapterInterface
17
     */
18
    private $adapter;
19
 
20
    /**
21
     *
22
     * @var \LeadersLinked\Cache\CacheInterface
23
     */
24
    private $cache;
25
 
26
 
27
    /**
28
     *
29
     * @var \Laminas\Log\LoggerInterface
30
     */
31
    private $logger;
32
 
33
    /**
34
     *
35
     * @var array
36
     */
37
    private $config;
38
 
39
 
40
    /**
41
     *
42
     * @var \Laminas\Mvc\I18n\Translator
43
     */
44
    private $translator;
45
 
46
 
47
    /**
48
     *
49
     * @param \Laminas\Db\Adapter\AdapterInterface $adapter
50
     * @param \LeadersLinked\Cache\CacheInterface $cache
51
     * @param \Laminas\Log\LoggerInterface LoggerInterface $logger
52
     * @param array $config
53
     * @param \Laminas\Mvc\I18n\Translator $translator
54
     */
55
    public function __construct($adapter, $cache, $logger, $config, $translator)
56
    {
57
        $this->adapter      = $adapter;
58
        $this->cache        = $cache;
59
        $this->logger       = $logger;
60
        $this->config       = $config;
61
        $this->translator   = $translator;
62
    }
63
 
64
    public function downloadAction()
65
    {
66
        $currentNetworkPlugin = $this->plugin('currentNetworkPlugin');
67
        $currentNetwork = $currentNetworkPlugin->getNetwork();
68
 
69
 
70
        $path = $this->config['leaderslinked.fullpath.network'];
71
 
72
 
73
        $type = $this->params()->fromRoute('type', 'logo');
74
        if($type == 'favico') {
75
            $filename = 'favico.png';
76
        } else  if($type == 'navbar') {
77
            $filename = 'navbar.png';
78
        } else  if($type == 'moodle') {
79
            $filename = 'moodle.png';
80
        } else {
81
            $filename = 'logo.png';
82
        }
83
 
84
 
85
        $request_fullpath = $path . $currentNetwork->uuid . DIRECTORY_SEPARATOR . $filename;
86
 
87
 
88
        if(!file_exists($request_fullpath)) {
89
            $request_fullpath = $this->config['leaderslinked.images_default.no_image'];
90
        }
91
 
92
 
93
        if(file_exists($request_fullpath)) {
94
            if (!is_readable($request_fullpath)) {
95
                return $this->getResponse()->setStatusCode(500);
96
            }
97
 
98
            $fileSize = filesize($request_fullpath);
99
 
100
            $mimeType = mime_content_type($request_fullpath);
101
            if($mimeType===false) {
102
                $mimeType = 'application/octet-stream';
103
            }
104
 
105
            $fileContent = file_get_contents($request_fullpath);
106
 
107
            $response = $this->getResponse();
108
            $headers = $response->getHeaders();
109
            $headers->addHeaderLine('Content-type: ' . $mimeType);
110
            $headers->addHeaderLine('Content-length: ' . $fileSize);
111
 
112
            if($fileContent!==false) {
113
                $response->setContent($fileContent);
114
            } else {
115
                $this->getResponse()->setStatusCode(500);
116
                return;
117
            }
118
        } else {
119
            return $this->getResponse()->setStatusCode(404);
120
        }
121
 
122
        return $this->getResponse();
123
    }
124
 
125
}