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
 
8544 efrain 69
 
1 www 70
        // Get the file name from GET variable.
71
        $code       = $this->params()->fromRoute('code', '');
8537 efrain 72
        $code2      = $this->params()->fromRoute('code2', '');
1 www 73
        $fileName   = $this->params()->fromRoute('filename', '');
74
        $type       = $this->params()->fromRoute('type', 'user');
75
 
76
 
8551 efrain 77
 
1 www 78
 
79
        $no_image = $this->config['leaderslinked.images_default.no_image'];
80
        $path = '';
81
        switch($type)
82
        {
83
            case 'user' :
84
                $no_image = $this->config['leaderslinked.images_default.user_image'];
85
                $path = $this->config['leaderslinked.fullpath.user'];
86
                break;
87
 
88
 
89
            case 'user-profile' :
90
                $no_image = $this->config['leaderslinked.images_default.user_profile'];
91
                $path = $this->config['leaderslinked.fullpath.user'];
92
                break;
93
 
94
            case 'user-cover' :
95
                $no_image = $this->config['leaderslinked.images_default.user_cover'];
96
                $path = $this->config['leaderslinked.fullpath.user'];
97
                break;
98
 
99
            case 'company' :
100
                $no_image = $this->config['leaderslinked.images_default.company_profile'];
101
                $path = $this->config['leaderslinked.fullpath.company'];
102
                break;
103
 
104
            case 'company-cover' :
105
                $no_image = $this->config['leaderslinked.images_default.company_cover'];
106
 
107
 
108
                $path = $this->config['leaderslinked.fullpath.company'];
109
                break;
110
 
111
            case 'group' :
112
                $no_image = $this->config['leaderslinked.images_default.group_profile'];
113
                $path = $this->config['leaderslinked.fullpath.group'];
114
                break;
115
 
116
            case 'group-cover' :
117
                $no_image = $this->config['leaderslinked.images_default.group_cover'];
118
                $path = $this->config['leaderslinked.fullpath.group'];
119
                break;
120
 
121
            case 'job' :
122
                $path = $this->config['leaderslinked.fullpath.job'];
123
                break;
124
 
125
            case 'chat' :
126
                $path = $this->config['leaderslinked.fullpath.chat'];
127
                break;
128
 
129
            case 'feed' :
130
                $path = $this->config['leaderslinked.fullpath.feed'];
131
                break;
132
 
133
            case 'post' :
134
                $path = $this->config['leaderslinked.fullpath.post'];
135
                break;
136
 
137
            case 'message' :
138
                $path = $this->config['leaderslinked.fullpath.message'];
139
                break;
140
 
141
            case 'microlearning-topic' :
142
                $path = $this->config['leaderslinked.fullpath.microlearning_topic'];
143
                break;
144
 
145
            case 'microlearning-capsule' :
146
                $path = $this->config['leaderslinked.fullpath.microlearning_capsule'];
147
                break;
148
 
149
            case 'microlearning-slide' :
150
                $path = $this->config['leaderslinked.fullpath.microlearning_slide'];
151
                break;
1590 eleazar 152
 
8537 efrain 153
            case 'recruitment-selection' :
1590 eleazar 154
                $path = $this->config['leaderslinked.fullpath.recruitment_selection'];
155
                break;
1 www 156
 
157
            default :
158
                $path = $this->config['leaderslinked.fullpath.image'];
159
                break;
160
 
161
        }
162
        if($code && $fileName) {
8537 efrain 163
 
164
            if (empty($code2)) {
165
                $request_fullpath = $path . $code . DIRECTORY_SEPARATOR . $fileName;
166
            } else {
167
                $request_fullpath = $path . $code . DIRECTORY_SEPARATOR. $code2. DIRECTORY_SEPARATOR . $fileName;
168
            }
169
 
8543 efrain 170
 
1 www 171
        } else {
172
            $request_fullpath = $no_image;
173
        }
174
 
175
        if(empty($fileName)) {
176
            $extensions     = explode('.',$request_fullpath);
177
            $extension      = strtolower(trim($extensions[count($extensions)-1]));
178
            if ($extension=='jpg' || $extension=='jpeg' || $extension=='gif' || $extension == 'png') {
179
                $request_fullpath =  $no_image;
180
            }
181
        }
182
 
183
 
184
        //echo $request_fullpath; exit;
185
 
186
 
187
 
188
        if(file_exists($request_fullpath)) {
189
 
190
            // Try to open file
191
            if (!is_readable($request_fullpath)) {
192
                return $this->getResponse()->setStatusCode(500);
193
            }
194
 
195
            // Get file size in bytes.
196
            $fileSize = filesize($request_fullpath);
197
 
198
            // Get MIME type of the file.
199
            $mimeType = mime_content_type($request_fullpath);
200
            if($mimeType===false) {
201
                $mimeType = 'application/octet-stream';
202
            }
203
 
204
            $fileContent = file_get_contents($request_fullpath);
205
 
206
            $response = $this->getResponse();
207
            $headers = $response->getHeaders();
208
            $headers->addHeaderLine('Content-type: ' . $mimeType);
209
            $headers->addHeaderLine('Content-length: ' . $fileSize);
210
 
211
            if($fileContent!==false) {
212
                $response->setContent($fileContent);
213
            } else {
214
                $this->getResponse()->setStatusCode(500);
215
                return;
216
            }
217
        } else {
218
            return $this->getResponse()->setStatusCode(404);
219
        }
220
 
221
        return $this->getResponse();
222
    }
223
 
224
}