Proyectos de Subversion LeadersLinked - Backend

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 www 1
<?php
2
declare(strict_types=1);
3
 
4
namespace LeadersLinked\Controller;
5
 
6
use Laminas\Db\Adapter\AdapterInterface;
7
use Laminas\Cache\Storage\Adapter\AbstractAdapter;
8
use Laminas\Mvc\Controller\AbstractActionController;
9
use Laminas\Log\LoggerInterface;
10
use Laminas\Mvc\I18n\Translator;
11
 
12
use Laminas\Authentication\AuthenticationService;
13
use LeadersLinked\Library\Functions;
14
use LeadersLinked\Mapper\UserMapper;
15
use LeadersLinked\Mapper\UserProfileMapper;
16
use LeadersLinked\Mapper\CompanyMapper;
17
use LeadersLinked\Mapper\GroupMapper;
18
use LeadersLinked\Mapper\JobMapper;
19
use LeadersLinked\Mapper\ChatUserMapper;
20
use LeadersLinked\Mapper\ChatGroupMapper;
21
 
22
class StorageController extends AbstractActionController
23
{
24
    /**
25
     *
26
     * @var AdapterInterface
27
     */
28
    private $adapter;
29
 
30
 
31
    /**
32
     *
33
     * @var AbstractAdapter
34
     */
35
    private $cache;
36
 
37
    /**
38
     *
39
     * @var  LoggerInterface
40
     */
41
    private $logger;
42
 
43
 
44
    /**
45
     *
46
     * @var array
47
     */
48
    private $config;
49
 
50
    /**
51
     *
52
     * @param AdapterInterface $adapter
53
     * @param AbstractAdapter $cache
54
     * @param LoggerInterface $logger
55
     * @param array $config
56
     */
57
    public function __construct($adapter, $cache , $logger, $config)
58
    {
59
        $this->adapter      = $adapter;
60
        $this->cache        = $cache;
61
        $this->logger       = $logger;
62
        $this->config       = $config;
63
    }
64
 
65
    public function downloadAction()
66
    {
67
 
68
 
69
        // Get the file name from GET variable.
70
        $code       = $this->params()->fromRoute('code', '');
71
        $fileName   = $this->params()->fromRoute('filename', '');
72
        $type       = $this->params()->fromRoute('type', 'user');
73
 
74
       // echo 'Code = '. $code . ' Filename = ' . $fileName . ' type = ' . $type; exit;
75
 
76
 
77
 
78
        $no_image = $this->config['leaderslinked.images_default.no_image'];
79
        $path = '';
80
        switch($type)
81
        {
82
            case 'user' :
83
                $no_image = $this->config['leaderslinked.images_default.user_image'];
84
                $path = $this->config['leaderslinked.fullpath.user'];
85
                break;
86
 
87
 
88
            case 'user-profile' :
89
                $no_image = $this->config['leaderslinked.images_default.user_profile'];
90
                $path = $this->config['leaderslinked.fullpath.user'];
91
                break;
92
 
93
            case 'user-cover' :
94
                $no_image = $this->config['leaderslinked.images_default.user_cover'];
95
                $path = $this->config['leaderslinked.fullpath.user'];
96
                break;
97
 
98
            case 'company' :
99
                $no_image = $this->config['leaderslinked.images_default.company_profile'];
100
                $path = $this->config['leaderslinked.fullpath.company'];
101
                break;
102
 
103
            case 'company-cover' :
104
                $no_image = $this->config['leaderslinked.images_default.company_cover'];
105
 
106
 
107
                $path = $this->config['leaderslinked.fullpath.company'];
108
                break;
109
 
110
            case 'group' :
111
                $no_image = $this->config['leaderslinked.images_default.group_profile'];
112
                $path = $this->config['leaderslinked.fullpath.group'];
113
                break;
114
 
115
            case 'group-cover' :
116
                $no_image = $this->config['leaderslinked.images_default.group_cover'];
117
                $path = $this->config['leaderslinked.fullpath.group'];
118
                break;
119
 
120
            case 'job' :
121
                $path = $this->config['leaderslinked.fullpath.job'];
122
                break;
123
 
124
            case 'chat' :
125
                $path = $this->config['leaderslinked.fullpath.chat'];
126
                break;
127
 
128
            case 'feed' :
129
                $path = $this->config['leaderslinked.fullpath.feed'];
130
                break;
131
 
132
            case 'post' :
133
                $path = $this->config['leaderslinked.fullpath.post'];
134
                break;
135
 
136
            case 'message' :
137
                $path = $this->config['leaderslinked.fullpath.message'];
138
                break;
139
 
140
            case 'microlearning-topic' :
141
                $path = $this->config['leaderslinked.fullpath.microlearning_topic'];
142
                break;
143
 
144
            case 'microlearning-capsule' :
145
                $path = $this->config['leaderslinked.fullpath.microlearning_capsule'];
146
                break;
147
 
148
            case 'microlearning-slide' :
149
                $path = $this->config['leaderslinked.fullpath.microlearning_slide'];
150
                break;
151
 
152
            default :
153
                $path = $this->config['leaderslinked.fullpath.image'];
154
                break;
155
 
156
        }
157
        if($code && $fileName) {
158
            $request_fullpath = $path . $code . DIRECTORY_SEPARATOR . $fileName;
159
        } else {
160
            $request_fullpath = $no_image;
161
        }
162
 
163
        if(empty($fileName)) {
164
            $extensions     = explode('.',$request_fullpath);
165
            $extension      = strtolower(trim($extensions[count($extensions)-1]));
166
            if ($extension=='jpg' || $extension=='jpeg' || $extension=='gif' || $extension == 'png') {
167
                $request_fullpath =  $no_image;
168
            }
169
        }
170
 
171
 
172
        //echo $request_fullpath; exit;
173
 
174
 
175
 
176
        if(file_exists($request_fullpath)) {
177
 
178
            // Try to open file
179
            if (!is_readable($request_fullpath)) {
180
                return $this->getResponse()->setStatusCode(500);
181
            }
182
 
183
            // Get file size in bytes.
184
            $fileSize = filesize($request_fullpath);
185
 
186
            // Get MIME type of the file.
187
            $mimeType = mime_content_type($request_fullpath);
188
            if($mimeType===false) {
189
                $mimeType = 'application/octet-stream';
190
            }
191
 
192
            $fileContent = file_get_contents($request_fullpath);
193
 
194
            $response = $this->getResponse();
195
            $headers = $response->getHeaders();
196
            $headers->addHeaderLine('Content-type: ' . $mimeType);
197
            $headers->addHeaderLine('Content-length: ' . $fileSize);
198
 
199
            if($fileContent!==false) {
200
                $response->setContent($fileContent);
201
            } else {
202
                $this->getResponse()->setStatusCode(500);
203
                return;
204
            }
205
        } else {
206
            return $this->getResponse()->setStatusCode(404);
207
        }
208
 
209
        return $this->getResponse();
210
    }
211
 
212
}