Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 6859 | | Comparar con el anterior | 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;
6849 efrain 7
 
1 www 8
use Laminas\Mvc\Controller\AbstractActionController;
9
use Laminas\Log\LoggerInterface;
10
 
11
 
12
class StorageController extends AbstractActionController
13
{
14
    /**
15
     *
6866 efrain 16
     * @var \Laminas\Db\Adapter\AdapterInterface
1 www 17
     */
18
    private $adapter;
6866 efrain 19
 
1 www 20
    /**
21
     *
6866 efrain 22
     * @var \LeadersLinked\Cache\CacheInterface
1 www 23
     */
6866 efrain 24
    private $cache;
25
 
26
 
27
    /**
28
     *
29
     * @var \Laminas\Log\LoggerInterface
30
     */
1 www 31
    private $logger;
32
 
33
    /**
34
     *
35
     * @var array
36
     */
37
    private $config;
38
 
6866 efrain 39
 
1 www 40
    /**
41
     *
6866 efrain 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
1 www 52
     * @param array $config
6866 efrain 53
     * @param \Laminas\Mvc\I18n\Translator $translator
1 www 54
     */
6866 efrain 55
    public function __construct($adapter, $cache, $logger, $config, $translator)
1 www 56
    {
57
        $this->adapter      = $adapter;
6866 efrain 58
        $this->cache        = $cache;
1 www 59
        $this->logger       = $logger;
60
        $this->config       = $config;
6866 efrain 61
        $this->translator   = $translator;
1 www 62
    }
63
 
64
    public function downloadAction()
65
    {
66
 
67
 
68
        // Get the file name from GET variable.
69
        $code       = $this->params()->fromRoute('code', '');
70
        $fileName   = $this->params()->fromRoute('filename', '');
71
        $type       = $this->params()->fromRoute('type', 'user');
72
 
73
 
74
 
75
       // echo 'Code = '. $code . ' Filename = ' . $fileName . ' type = ' . $type; exit;
76
 
77
 
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;
152
 
5205 efrain 153
            case 'daily-pulse' :
154
                $path = $this->config['leaderslinked.fullpath.daily_pulse'];
155
                break;
156
 
5223 efrain 157
            case 'engagement-reward' :
158
                $path = $this->config['leaderslinked.fullpath.engagement_reward'] ;
159
                break;
160
 
6056 efrain 161
            case 'knowledge-area' :
162
                $path = $this->config['leaderslinked.fullpath.knowledge_area'] ;
163
                break;
164
 
1 www 165
            default :
166
                $path = $this->config['leaderslinked.fullpath.image'];
167
                break;
168
 
169
        }
170
        if($code && $fileName) {
171
            $request_fullpath = $path . $code . DIRECTORY_SEPARATOR . $fileName;
172
        } else {
173
            $request_fullpath = $no_image;
174
        }
6803 efrain 175
 
176
 
177
        $extensions     = explode('.',$request_fullpath);
178
        $extension      = strtolower(trim($extensions[count($extensions)-1]));
179
        if ($extension=='jpg' || $extension=='jpeg' || $extension=='gif' || $extension == 'png') {
180
 
181
            if(empty($fileName) ||  !file_exists($request_fullpath )) {
1 www 182
                $request_fullpath =  $no_image;
183
            }
6803 efrain 184
 
185
 
186
 
1 www 187
        }
188
 
189
 
190
 
6803 efrain 191
 
1 www 192
        if(file_exists($request_fullpath)) {
193
 
194
            // Try to open file
195
            if (!is_readable($request_fullpath)) {
196
                return $this->getResponse()->setStatusCode(500);
197
            }
198
 
199
            // Get file size in bytes.
200
            $fileSize = filesize($request_fullpath);
201
 
202
            // Get MIME type of the file.
203
            $mimeType = mime_content_type($request_fullpath);
204
            if($mimeType===false) {
205
                $mimeType = 'application/octet-stream';
206
            }
207
 
208
            $fileContent = file_get_contents($request_fullpath);
209
 
210
            $response = $this->getResponse();
211
            $headers = $response->getHeaders();
212
            $headers->addHeaderLine('Content-type: ' . $mimeType);
213
            $headers->addHeaderLine('Content-length: ' . $fileSize);
214
 
215
            if($fileContent!==false) {
216
                $response->setContent($fileContent);
217
            } else {
218
                $this->getResponse()->setStatusCode(500);
219
                return;
220
            }
221
        } else {
222
            return $this->getResponse()->setStatusCode(404);
223
        }
224
 
225
        return $this->getResponse();
226
    }
227
 
228
}