Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 6860 | Autoría | Comparar con el anterior | Ultima modificación | Ver Log |

<?php
declare(strict_types=1);

namespace LeadersLinked\Controller;

use Laminas\Db\Adapter\AdapterInterface;

use Laminas\Mvc\Controller\AbstractActionController;
use Laminas\Log\LoggerInterface;


class StorageNetworkController extends AbstractActionController
{
    /**
     *
     * @var \Laminas\Db\Adapter\AdapterInterface
     */
    private $adapter;
    
    /**
     *
     * @var \LeadersLinked\Cache\CacheInterface
     */
    private $cache;
    
    
    /**
     *
     * @var \Laminas\Log\LoggerInterface
     */
    private $logger;
    
    /**
     *
     * @var array
     */
    private $config;
    
    
    /**
     *
     * @var \Laminas\Mvc\I18n\Translator
     */
    private $translator;
    
    
    /**
     *
     * @param \Laminas\Db\Adapter\AdapterInterface $adapter
     * @param \LeadersLinked\Cache\CacheInterface $cache
     * @param \Laminas\Log\LoggerInterface LoggerInterface $logger
     * @param array $config
     * @param \Laminas\Mvc\I18n\Translator $translator
     */
    public function __construct($adapter, $cache, $logger, $config, $translator)
    {
        $this->adapter      = $adapter;
        $this->cache        = $cache;
        $this->logger       = $logger;
        $this->config       = $config;
        $this->translator   = $translator;
    }
    
    public function downloadAction()
    {
        $currentNetworkPlugin = $this->plugin('currentNetworkPlugin');
        $currentNetwork = $currentNetworkPlugin->getNetwork();
        

        $path = $this->config['leaderslinked.fullpath.network'];
        
        
        $type = $this->params()->fromRoute('type', 'logo');
        if($type == 'favico') {
            $filename = 'favico.png';
        } else  if($type == 'navbar') {
            $filename = 'navbar.png';
        } else  if($type == 'moodle') {
            $filename = 'moodle.png';
        } else {
            $filename = 'logo.png';
        }
        
        
        $request_fullpath = $path . $currentNetwork->uuid . DIRECTORY_SEPARATOR . $filename;
        
        
        if(!file_exists($request_fullpath)) {
            $request_fullpath = $this->config['leaderslinked.images_default.no_image'];
        }
        
        
        if(file_exists($request_fullpath)) {
            if (!is_readable($request_fullpath)) {
                return $this->getResponse()->setStatusCode(500);
            }
            
            $fileSize = filesize($request_fullpath);
            
            $mimeType = mime_content_type($request_fullpath);
            if($mimeType===false) {
                $mimeType = 'application/octet-stream';
            }
            
            $fileContent = file_get_contents($request_fullpath);
            
            $response = $this->getResponse();
            $headers = $response->getHeaders();
            $headers->addHeaderLine('Content-type: ' . $mimeType);
            $headers->addHeaderLine('Content-length: ' . $fileSize);
            
            if($fileContent!==false) {
                $response->setContent($fileContent);
            } else {
                $this->getResponse()->setStatusCode(500);
                return;
            }
        } else {
            return $this->getResponse()->setStatusCode(404);
        }

        return $this->getResponse();
    }  
    
}