Commit 871f01a3 authored by Bertrand Gauthier's avatar Bertrand Gauthier
Browse files

wip

parent a2e759bc
Loading
Loading
Loading
Loading
+2 −0
Changes for composer.json: 2 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -12,7 +12,9 @@
        "zendframework/zend-developer-tools":   "^1.0"
    },
    "require": {
        "ext-fileinfo":                         "*",
        "ext-json":                             "*",
        "ext-imagick":                          "*",
        "unicaen/app":                          "^1.3",
        "unicaen/auth":                         "^1.2",
        "unicaen/faq":                          "^1.0",
+14 −5
Changes for module/Application/src/Application/Service/Fichier/FichierService.php: 14 added lines, 5 removed lines.
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ use Application\Service\BaseService;
use Application\Service\Etablissement\EtablissementServiceAwareTrait;
use Application\Service\Fichier\Exception\DepotImpossibleException;
use Application\Service\Fichier\Exception\ValidationImpossibleException;
use Application\Service\File\FileServiceAwareTrait;
use Application\Service\Notification\NotifierServiceAwareTrait;
use Application\Service\These\PageDeGarde\PageDeCouverturePdfExporter;
use Application\Service\ValiditeFichier\ValiditeFichierServiceAwareTrait;
@@ -44,9 +45,11 @@ class FichierService extends BaseService
    use RetraitementServiceAwareTrait;
    use EtablissementServiceAwareTrait;
    use NotifierServiceAwareTrait;
    use FileServiceAwareTrait;

    /**
     * @var string
     * @deprecated
     */
    private $rootDirectoryPath;

@@ -65,18 +68,20 @@ class FichierService extends BaseService

    /**
     * @param string $rootDirectoryPath
     * @deprecated
     */
    public function setRootDirectoryPath($rootDirectoryPath)
    {
        $this->rootDirectoryPath = $rootDirectoryPath;
        $this->fileService->setRootDirectoryPath($rootDirectoryPath);
    }

    /**
     * @return string
     * @deprecated
     */
    public function getRootDirectoryPath()
    {
        return $this->rootDirectoryPath;
        return $this->fileService->getRootDirectoryPath();
    }

    /**
@@ -153,7 +158,8 @@ class FichierService extends BaseService
     */
    private function computeDestinationDirectoryPathForFichier(Fichier $fichier)
    {
        return $this->rootDirectoryPath . '/' . strtolower($fichier->getNature()->getCode());
//        return $this->rootDirectoryPath . '/' . strtolower($fichier->getNature()->getCode());
        return $this->fileService->getCompleteFilepath(strtolower($fichier->getNature()->getCode()));
    }

    /**
@@ -509,6 +515,7 @@ class FichierService extends BaseService
     * @param string $inputFilePath
     * @return string Contenu binaire du fichier PNG généré
     * @throws LogicException Format de fichier incorrect
     * @deprecated
     */
    public function generateFirstPagePreview($inputFilePath)
    {
@@ -649,7 +656,6 @@ class FichierService extends BaseService
     */
    public function generatePageDeCouverture(These $these, RendererInterface $renderer, $filepath = null)
    {

        $pdcData = $this->fetchInformationsPageDeCouverture($these);

        $exporter = new PageDeCouverturePdfExporter($renderer, 'A4');
@@ -672,7 +678,8 @@ class FichierService extends BaseService
     */
    public function createResponseForFileContent(Response $response, $fileContent, $cacheMaxAge = null)
    {
        $response->setContent($fileContent);
        /*
         * $response->setContent($fileContent);

        $headers = $response->getHeaders();
        $headers
@@ -694,6 +701,8 @@ class FichierService extends BaseService
        }

        return $response;
        */
        return $this->fileService->createResponseForFileContent($response, $fileContent, $cacheMaxAge);
    }

    /**
+68 −0
Changes for module/Application/src/Application/Service/File/FileService.php: 68 added lines, 0 removed lines.
Original line number Diff line number Diff line
<?php

namespace Application\Service\File;

use Application\Util;
use UnicaenApp\Exception\RuntimeException;
use Zend\Http\Response;

class FileService
{
    /**
     * @var string
     */
    private $rootDirectoryPath;

    /**
     * @param string $rootDirectoryPath
     */
    public function setRootDirectoryPath($rootDirectoryPath)
    {
        $this->rootDirectoryPath = $rootDirectoryPath;
    }

    /**
     * @return string
     */
    public function getRootDirectoryPath()
    {
        return $this->rootDirectoryPath;
    }

    public function getCompleteFilepath($relativeFilepath)
    {
        return $this->rootDirectoryPath . '/' . $relativeFilepath;
    }

    /**
     * @param Response $response
     * @param string   $fileContent
     * @param int|null $cacheMaxAge En secondes, ex: 60*60*24 = 86400 s = 1 jour
     * @return Response
     */
    public function createResponseForFileContent(Response $response, $fileContent, $cacheMaxAge = null)
    {
        $response->setContent($fileContent);

        $headers = $response->getHeaders();
        $headers
            ->addHeaderLine('Content-Transfer-Encoding', "binary")
            ->addHeaderLine('Content-Type', "image/png")
            ->addHeaderLine('Content-length', strlen($fileContent));

        if ($cacheMaxAge === null) {
            $headers
                ->addHeaderLine('Cache-Control', "no-cache, no-store, must-revalidate")
                ->addHeaderLine('Pragma', 'no-cache');
        }
        else {
            // autorisation de la mise en cache de l'image par le client
            $headers
                ->addHeaderLine('Cache-Control', "private, max-age=$cacheMaxAge")
                ->addHeaderLine('Pragma', 'private')// tout sauf 'no-cache'
                ->addHeaderLine('Expires', gmdate('D, d M Y H:i:s \G\M\T', time() + $cacheMaxAge));
        }

        return $response;
    }
}
 No newline at end of file
+19 −0
Changes for module/Application/src/Application/Service/File/FileServiceAwareTrait.php: 19 added lines, 0 removed lines.
Original line number Diff line number Diff line
<?php

namespace Application\Service\File;

trait FileServiceAwareTrait
{
    /**
     * @var FileService
     */
    protected $fileService;

    /**
     * @param FileService $fileService
     */
    public function setFileService(FileService $fileService)
    {
        $this->fileService = $fileService;
    }
}
 No newline at end of file
+49 −0
Changes for module/Application/src/Application/Service/File/FileServiceFactory.php: 49 added lines, 0 removed lines.
Original line number Diff line number Diff line
<?php

namespace Application\Service\File;

use UnicaenApp\Exception\RuntimeException;
use Zend\ServiceManager\ServiceLocatorInterface as ContainerInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

class FileServiceFactory
{
    /**
     * Create service
     *
     * @param ContainerInterface $container
     * @return FileService
     */
    public function __invoke(ContainerInterface $container)
    {
        $service = new FileService();

        $service->setRootDirectoryPath($this->getRootDirectoryPath($container));

        return $service;
    }

    /**
     * @param ServiceLocatorInterface $container
     * @return string
     */
    private function getRootDirectoryPath(ContainerInterface $container)
    {
        /** @var array $config */
        $config = $container->get('config');

        if (empty($config['fichier']['root_dir_path'])) {
            throw new RuntimeException(
                "Vous devez spécifier dans la config le chemin du répertoire de destination des fichiers (clé fichier.root_dir_path).");
        }

        $path = $config['fichier']['root_dir_path'];

        if (! is_readable($path)) {
            throw new RuntimeException(
                "Le chemin du répertoire de destination des fichiers doit exister et être accessible : " . $path);
        }

        return $path;
    }
}