Commit 9e5e6144 authored by Bertrand Gauthier's avatar Bertrand Gauthier
Browse files

Stockage de fichier S3/Filesystem : plus de copie sur disque dans le cas d'une...

Stockage de fichier S3/Filesystem : plus de copie sur disque dans le cas d'une demande d'un fichier issu du Filesystem.
parent 86c073c1
Loading
Loading
Loading
Loading
+2 −0
Changes for CHANGELOG.md: 2 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -4,6 +4,8 @@ Journal des modifications
7.1.0
-----
- Rapports d'activité : initialisation du formulaire de création d'un nouveau rapport avec les formations suivies (module Formation).
- Stockage de fichier S3/Filesystem : plus de copie sur disque dans le cas d'une demande d'un fichier issu du Filesystem.
- [FIX] Module Substitutions : plantage de la page consacrée aux doublons (substitutions possibles). 

7.0.1
-----
+18 −15
Changes for module/Fichier/src/Fichier/Service/Fichier/FichierStorageService.php: 18 added lines, 15 removed lines.
Original line number Diff line number Diff line
@@ -16,6 +16,8 @@ class FichierStorageService
{
    use PageFichierIntrouvablePdfExporterTrait;

    const TEMP_FILENAME_PREFIX = 'sygal_storage_service_';

    const DIR_ETAB = 'Etab';
    const DIR_ED = 'ED';
    const DIR_UR = 'UR';
@@ -35,12 +37,11 @@ class FichierStorageService
    /**
     * @param bool $genererFichierSubstitutionSiIntrouvable
     */
    public function setGenererFichierSubstitutionSiIntrouvable(?bool $genererFichierSubstitutionSiIntrouvable = true)
    public function setGenererFichierSubstitutionSiIntrouvable(?bool $genererFichierSubstitutionSiIntrouvable = true): void
    {
        $this->genererFichierSubstitutionSiIntrouvable = $genererFichierSubstitutionSiIntrouvable;
    }


    /********************************** Fichiers standards *************************************/

    /**
@@ -83,10 +84,10 @@ class FichierStorageService
        $fileName = $this->computeFileNameForFichier($fichier);
        $dirPath = $this->storageAdapter->computeDirectoryPath($dirName);

        $tmpFilePath = sys_get_temp_dir() . '/' . uniqid();
        $tmpFilePath = sys_get_temp_dir() . '/' . self::TEMP_FILENAME_PREFIX . uniqid();

        try {
            $this->storageAdapter->saveToFilesystem($dirPath, $fileName, $tmpFilePath);
            $filePath = $this->storageAdapter->saveToFilesystem($dirPath, $fileName, $tmpFilePath);
        } catch (StorageAdapterException $e) {
            // en cas de fichier introuvable dans le storage, génération éventuelle d'un fichier de substitution
            if ($this->genererFichierSubstitutionSiIntrouvable) {
@@ -95,13 +96,14 @@ class FichierStorageService
                if ($substitutionFileContent === null ) {
                    throw $e; // solution de facilité (todo: lancer une exception spécifique)
                }
                file_put_contents($tmpFilePath, $substitutionFileContent);
                $filePath = $tmpFilePath;
                file_put_contents($filePath, $substitutionFileContent);
            } else {
                throw $e;
            }
        }

        return $tmpFilePath;
        return $filePath;
    }

    /**
@@ -146,7 +148,7 @@ class FichierStorageService
     * @param \Fichier\Entity\Db\Fichier $fichier Entité Fichier concernée
     * @throws \Fichier\Service\Storage\Adapter\Exception\StorageAdapterException
     */
    public function saveFileForFichier(string $filepath, Fichier $fichier)
    public function saveFileForFichier(string $filepath, Fichier $fichier): void
    {
        $dirName = $this->computeDirectoryNameForFichier($fichier);
        $fileName = $this->computeFileNameForFichier($fichier);
@@ -161,7 +163,7 @@ class FichierStorageService
     * @param \Fichier\Entity\Db\Fichier $fichier Entité Fichier concernée
     * @throws \Fichier\Service\Storage\Adapter\Exception\StorageAdapterException
     */
    public function deleteFileForFichier(Fichier $fichier)
    public function deleteFileForFichier(Fichier $fichier): void
    {
        $dirName = $this->computeDirectoryNameForFichier($fichier);
        $fileName = $this->computeFileNameForFichier($fichier);
@@ -249,7 +251,7 @@ class FichierStorageService
     * Retourne le chemin absolu d'une copie du fichier physique du logo existant d'une structure.
     *
     * @param \Structure\Entity\Db\StructureInterface $structure Entité Structure concernée
     * @return string
     * @return string|null
     * @throws \Fichier\Service\Storage\Adapter\Exception\StorageAdapterException Fichier introuvable dans le storage
     */
    public function getFileForLogoStructure(StructureInterface $structure): ?string
@@ -261,10 +263,10 @@ class FichierStorageService
        $dirPath = $this->computeDirectoryPathForLogoStructure($structure);
        $fileName = $structure->getCheminLogo();

        $tmpFilePath = sys_get_temp_dir() . '/' . uniqid();
        $tmpFilePath = sys_get_temp_dir() . '/' . self::TEMP_FILENAME_PREFIX . uniqid();

        try {
            $this->storageAdapter->saveToFilesystem($dirPath, $fileName, $tmpFilePath);
            $filePath = $this->storageAdapter->saveToFilesystem($dirPath, $fileName, $tmpFilePath);
        } catch (StorageAdapterException $e) {
            // en cas de fichier introuvable dans le storage, génération éventuelle d'un fichier de substitution
            if ($this->genererFichierSubstitutionSiIntrouvable) {
@@ -275,13 +277,14 @@ class FichierStorageService
                if ($substitutionFileContent === null ) {
                    throw $e; // solution de facilité (todo: lancer une exception spécifique)
                }
                file_put_contents($tmpFilePath, $substitutionFileContent);
                $filePath = $tmpFilePath;
                file_put_contents($filePath, $substitutionFileContent);
            } else {
                throw $e;
            }
        }

        return $tmpFilePath;
        return $filePath;
    }

    private function createSubstitutionFileContentForFichier(Fichier $fichier): ?string
@@ -303,7 +306,7 @@ class FichierStorageService
     * @param \Structure\Entity\Db\StructureInterface $structure
     * @throws \Fichier\Service\Storage\Adapter\Exception\StorageAdapterException
     */
    public function saveFileForLogoStructure(string $logoFilepath, StructureInterface $structure)
    public function saveFileForLogoStructure(string $logoFilepath, StructureInterface $structure): void
    {
        $logoDir = $this->computeDirectoryPathForLogoStructure($structure);
        $logoFilename = $this->computeFileNameForNewLogoStructure($structure);
@@ -317,7 +320,7 @@ class FichierStorageService
     * @param \Structure\Entity\Db\StructureInterface $structure
     * @throws \Fichier\Service\Storage\Adapter\Exception\StorageAdapterException
     */
    public function deleteFileForLogoStructure(StructureInterface $structure)
    public function deleteFileForLogoStructure(StructureInterface $structure): void
    {
        $dirPath = $this->computeDirectoryPathForLogoStructure($structure);
        $fileName = $structure->getCheminLogo();
+7 −14
Changes for module/Fichier/src/Fichier/Service/Storage/Adapter/FilesystemStorageAdapter.php: 7 added lines, 14 removed lines.
Original line number Diff line number Diff line
@@ -13,7 +13,7 @@ class FilesystemStorageAdapter extends AbstractStorageAdapter
     * @param array $config
     * @throws \Fichier\Service\Storage\Adapter\Exception\StorageAdapterException
     */
    public function setConfig(array $config)
    public function setConfig(array $config): void
    {
        parent::setConfig($config);

@@ -39,7 +39,7 @@ class FilesystemStorageAdapter extends AbstractStorageAdapter
     * @param string $absolutePath
     * @throws \Fichier\Service\Storage\Adapter\Exception\StorageAdapterException
     */
    public function createDirectory(string $absolutePath)
    public function createDirectory(string $absolutePath): void
    {
        $ok = $this->createWritableFolder($absolutePath, 0770);
        if (!$ok) {
@@ -60,7 +60,7 @@ class FilesystemStorageAdapter extends AbstractStorageAdapter
     *
     * @codeCoverageIgnore
     */
    public function createWritableFolder(string $folder, int $mode = 0700): bool
    private function createWritableFolder(string $folder, int $mode = 0700): bool
    {
        if($folder !== '.' && $folder !== '/' ) {
            $this->createWritableFolder(dirname($folder));
@@ -72,7 +72,7 @@ class FilesystemStorageAdapter extends AbstractStorageAdapter
        return mkdir($folder, $mode, true) && is_writable($folder);
    }

    public function deleteFile(string $dirPath, string $fileName)
    public function deleteFile(string $dirPath, string $fileName): void
    {
        $filePath = $this->assemblePath($dirPath, $fileName);

@@ -102,16 +102,9 @@ class FilesystemStorageAdapter extends AbstractStorageAdapter
        }

        return file_get_contents($filePath);

    }

    /**
     * @param string $fromDirPath
     * @param string $fromFileName
     * @param string $toFilesystemPath
     * @throws \Fichier\Service\Storage\Adapter\Exception\StorageAdapterException
     */
    public function saveToFilesystem(string $fromDirPath, string $fromFileName, string $toFilesystemPath)
    public function saveToFilesystem(string $fromDirPath, string $fromFileName, string $toFilesystemPath): string
    {
        $filePath = $this->assemblePath($fromDirPath, $fromFileName);

@@ -126,13 +119,13 @@ class FilesystemStorageAdapter extends AbstractStorageAdapter
                ->setFileName($fromFileName);
        }

        copy($filePath, $toFilesystemPath);
        return $filePath;
    }

    /**
     * @throws \Fichier\Service\Storage\Adapter\Exception\StorageAdapterException
     */
    public function saveFileContent(string $fileContent, string $toDirPath, string $toFileName)
    public function saveFileContent(string $fileContent, string $toDirPath, string $toFileName): void
    {
        $this->createDirectory($toDirPath);

+7 −5
Changes for module/Fichier/src/Fichier/Service/Storage/Adapter/S3StorageAdapter.php: 7 added lines, 5 removed lines.
Original line number Diff line number Diff line
@@ -14,7 +14,7 @@ class S3StorageAdapter extends AbstractStorageAdapter
    private ?array $clientConfig;
    private ?S3Client $client = null;

    public function setConfig(array $config)
    public function setConfig(array $config): void
    {
        parent::setConfig($config);

@@ -56,7 +56,7 @@ class S3StorageAdapter extends AbstractStorageAdapter
    /**
     * @throws \Fichier\Service\Storage\Adapter\Exception\StorageAdapterException
     */
    protected function createBucket(string $name)
    protected function createBucket(string $name): void
    {
        $client = $this->getClient();

@@ -85,7 +85,7 @@ class S3StorageAdapter extends AbstractStorageAdapter
    /**
     * @inheritDoc
     */
    public function saveFileContent(string $fileContent, string $toDirPath, string $toFileName)
    public function saveFileContent(string $fileContent, string $toDirPath, string $toFileName): void
    {
        $client = $this->getClient();

@@ -110,7 +110,7 @@ class S3StorageAdapter extends AbstractStorageAdapter
    /**
     * @inheritDoc
     */
    public function deleteFile(string $dirPath, string $fileName)
    public function deleteFile(string $dirPath, string $fileName): void
    {
        $client = $this->getClient();

@@ -161,11 +161,13 @@ class S3StorageAdapter extends AbstractStorageAdapter
    /**
     * @inheritDoc
     */
    public function saveToFilesystem(string $fromDirPath, string $fromFileName, string $toFilesystemPath)
    public function saveToFilesystem(string $fromDirPath, string $fromFileName, string $toFilesystemPath): string
    {
        $content = $this->getFileContent($fromDirPath, $fromFileName);

        file_put_contents($toFilesystemPath, $content);

        return $toFilesystemPath;
    }

    private function createS3Client(): S3Client
+8 −3
Changes for module/Fichier/src/Fichier/Service/Storage/Adapter/StorageAdapterInterface.php: 8 added lines, 3 removed lines.
Original line number Diff line number Diff line
@@ -21,18 +21,23 @@ interface StorageAdapterInterface
     * @param string $dirPath
     * @param string $fileName
     * @return string
     *
     * @throws \Fichier\Service\Storage\Adapter\Exception\StorageAdapterException
     */
    public function getFileContent(string $dirPath, string $fileName): string;

    /**
     * Enregistre *si besoin* le fichier sur le disque.
     * Sauf si le storage est de type Filesystem (disque), auquel cas le chemin retourné est l'emplacement original
     * du fichier.
     *
     * @param string $fromDirPath
     * @param string $fromFileName
     * @param string $toFilesystemPath
     * @param string $toFilesystemPath Chemin où enregistrer le fichier sur le disque
     * @return string Chemin où a été enregistré le fichier, ou emplacement original dans le cas d'un storage Filesystem.
     *
     * @throws \Fichier\Service\Storage\Adapter\Exception\StorageAdapterException
     */
    public function saveToFilesystem(string $fromDirPath, string $fromFileName, string $toFilesystemPath);
    public function saveToFilesystem(string $fromDirPath, string $fromFileName, string $toFilesystemPath): string;

    /**
     * @param string $fileContent