diff --git a/core/lib/Thelia/Config/Resources/config.xml b/core/lib/Thelia/Config/Resources/config.xml index 9a287e0cd..74392256a 100644 --- a/core/lib/Thelia/Config/Resources/config.xml +++ b/core/lib/Thelia/Config/Resources/config.xml @@ -142,10 +142,28 @@ %kernel.environment% + + + + + + + + + gz + + + + + + bz2 + + + diff --git a/core/lib/Thelia/Core/FileFormat/Archive/AbstractArchiveBuilder.php b/core/lib/Thelia/Core/FileFormat/Archive/AbstractArchiveBuilder.php index 66613ba45..447bf1265 100644 --- a/core/lib/Thelia/Core/FileFormat/Archive/AbstractArchiveBuilder.php +++ b/core/lib/Thelia/Core/FileFormat/Archive/AbstractArchiveBuilder.php @@ -12,6 +12,8 @@ namespace Thelia\Core\FileFormat\Archive; use Thelia\Core\FileFormat\FormatInterface; +use Thelia\Core\Translation\Translator; +use Thelia\Log\Tlog; use Thelia\Tools\FileDownload\FileDownloaderAwareTrait; /** @@ -22,4 +24,75 @@ use Thelia\Tools\FileDownload\FileDownloaderAwareTrait; abstract class AbstractArchiveBuilder implements FormatInterface, ArchiveBuilderInterface { use FileDownloaderAwareTrait; + + const TEMP_DIRECTORY_NAME = "archive_builder"; + + /** @var \Thelia\Core\Translation\Translator */ + protected $translator; + + /** @var \Thelia\Log\Tlog */ + protected $logger; + + /** @var string */ + protected $cacheDir; + + public function __construct() + { + $this->translator = Translator::getInstance(); + + $this->logger = Tlog::getNewInstance(); + } + + public function getArchiveBuilderCacheDirectory($environment) + { + $theliaCacheDir = THELIA_CACHE_DIR . $environment . DS; + + if (!is_writable($theliaCacheDir)) { + throw new \ErrorException( + $this->translator->trans( + "The cache directory \"%env\" is not writable", + [ + "%env" => $environment + ] + ) + ); + } + + $archiveBuilderCacheDir = $this->cache_dir = $theliaCacheDir . static::TEMP_DIRECTORY_NAME; + + if (!is_dir($archiveBuilderCacheDir) && !mkdir($archiveBuilderCacheDir, 0755)) { + throw new \ErrorException( + $this->translator->trans( + "Error while creating the directory \"%directory\"", + [ + "%directory" => static::TEMP_DIRECTORY_NAME + ] + ) + ); + } + + return $archiveBuilderCacheDir; + } + + + public function getCacheDir() + { + return $this->cacheDir; + } + + /** + * @return Tlog + */ + public function getLogger() + { + return $this->logger; + } + + /** + * @return Translator + */ + public function getTranslator() + { + return $this->translator; + } } \ No newline at end of file diff --git a/core/lib/Thelia/Core/FileFormat/Archive/ArchiveBuilder/ZipArchiveBuilder.php b/core/lib/Thelia/Core/FileFormat/Archive/ArchiveBuilder/ZipArchiveBuilder.php index f38b1c3fe..41611330a 100644 --- a/core/lib/Thelia/Core/FileFormat/Archive/ArchiveBuilder/ZipArchiveBuilder.php +++ b/core/lib/Thelia/Core/FileFormat/Archive/ArchiveBuilder/ZipArchiveBuilder.php @@ -33,8 +33,6 @@ use Thelia\Tools\FileDownload\FileDownloaderInterface; */ class ZipArchiveBuilder extends AbstractArchiveBuilder { - const TEMP_DIRECTORY_NAME = "archive_builder"; - /** * @var \ZipArchive */ @@ -43,30 +41,18 @@ class ZipArchiveBuilder extends AbstractArchiveBuilder /** * @var string This is the absolute path to the zip file in cache */ - protected $zip_cache_file; + protected $zipCacheFile; /** * @var string This is the path of the cache */ - protected $cache_dir; - - /** - * @var \Thelia\Log\Tlog - */ - protected $logger; - - /** - * @var Translator - */ - protected $translator; + protected $cacheDir; public function __construct() { + parent::__construct(); + $this->zip = new \ZipArchive(); - - $this->logger = Tlog::getNewInstance(); - - $this->translator = Translator::getInstance(); } /** @@ -78,8 +64,8 @@ class ZipArchiveBuilder extends AbstractArchiveBuilder if ($this->zip instanceof \ZipArchive) { @$this->zip->close(); - if (file_exists($this->zip_cache_file)) { - unlink($this->zip_cache_file); + if (file_exists($this->zipCacheFile)) { + unlink($this->zipCacheFile); } } } @@ -106,7 +92,7 @@ class ZipArchiveBuilder extends AbstractArchiveBuilder $directoryInArchive = ""; } - if(!empty($directoryInArchive) && $directoryInArchive != "/") { + if(!empty($directoryInArchive)) { $directoryInArchive = $this->getDirectoryPath($directoryInArchive); if (!$this->zip->addEmptyDir($directoryInArchive)) { @@ -121,8 +107,12 @@ class ZipArchiveBuilder extends AbstractArchiveBuilder } } + /** + * Download the file if it is online + * If it's local check if the file exists and if it is redable + */ if ($isOnline) { - $fileDownloadCache = $this->cache_dir . DS . "download"; + $fileDownloadCache = $this->cacheDir . DS . "download"; $this->getFileDownloader() ->download($filePath, $fileDownloadCache) @@ -149,6 +139,10 @@ class ZipArchiveBuilder extends AbstractArchiveBuilder $name = basename($filePath); } + /** + * Then write the file in the archive and commit the changes + */ + $destination = $directoryInArchive . $name; if (!$this->zip->addFile($filePath,$destination)) { @@ -212,22 +206,22 @@ class ZipArchiveBuilder extends AbstractArchiveBuilder $this->commit(); - if (!file_exists($this->zip_cache_file)) { - $this->throwFileNotFound($this->zip_cache_file); + if (!file_exists($this->zipCacheFile)) { + $this->throwFileNotFound($this->zipCacheFile); } - if (!is_readable($this->zip_cache_file)) { + if (!is_readable($this->zipCacheFile)) { throw new FileNotReadableException( $this->translator->trans( "The cache file %file is not readable", [ - "%file" => $this->zip_cache_file + "%file" => $this->zipCacheFile ] ) ); } - $content = file_get_contents($this->zip_cache_file); + $content = file_get_contents($this->zipCacheFile); $this->zip->close(); @@ -344,36 +338,11 @@ class ZipArchiveBuilder extends AbstractArchiveBuilder */ public function setEnvironment($environment) { - $theliaCacheDir = THELIA_CACHE_DIR . $environment . DS; - - if (!is_writable($theliaCacheDir)) { - throw new \ErrorException( - $this->translator->trans( - "The cache directory \"%env\" is not writable", - [ - "%env" => $environment - ] - ) - ); - } - - $archiveBuilderCacheDir = $this->cache_dir = $theliaCacheDir . static::TEMP_DIRECTORY_NAME; - - if (!is_dir($archiveBuilderCacheDir) && !mkdir($archiveBuilderCacheDir, 0755)) { - throw new \ErrorException( - $this->translator->trans( - "Error while creating the directory \"%directory\"", - [ - "%directory" => static::TEMP_DIRECTORY_NAME - ] - ) - ); - } $cacheFileName = md5 (uniqid()); - $cacheFile = $archiveBuilderCacheDir . DS . $cacheFileName; - $cacheFile .= "." . $this->getExtension(); + $cacheFile = $this->getArchiveBuilderCacheDirectory($environment) . DS; + $cacheFile .= $cacheFileName . "." . $this->getExtension(); if (file_exists($cacheFile)) { unlink($cacheFile); @@ -387,12 +356,12 @@ class ZipArchiveBuilder extends AbstractArchiveBuilder if($opening !== true) { throw new \ErrorException( $this->translator->trans( - "Unknown" + "An unknown error append" ) ); } - $this->zip_cache_file = $cacheFile; + $this->zipCacheFile = $cacheFile; return $this; } @@ -572,22 +541,6 @@ class ZipArchiveBuilder extends AbstractArchiveBuilder return "application/zip"; } - /** - * @return Tlog - */ - public function getLogger() - { - return $this->logger; - } - - /** - * @return Translator - */ - public function getTranslator() - { - return $this->translator; - } - /** * @return \ZipArchive */ @@ -598,11 +551,7 @@ class ZipArchiveBuilder extends AbstractArchiveBuilder public function getZipCacheFile() { - return $this->zip_cache_file; + return $this->zipCacheFile; } - public function getCacheDir() - { - return $this->cache_dir; - } } \ No newline at end of file diff --git a/core/lib/Thelia/Tests/FileFormat/Archive/ArchiveBuilder/TestResources/bad_formatted.zip b/core/lib/Thelia/Tests/FileFormat/Archive/ArchiveBuilder/TestResources/bad_formatted.zip index e69de29bb..3bd1f0e29 100644 --- a/core/lib/Thelia/Tests/FileFormat/Archive/ArchiveBuilder/TestResources/bad_formatted.zip +++ b/core/lib/Thelia/Tests/FileFormat/Archive/ArchiveBuilder/TestResources/bad_formatted.zip @@ -0,0 +1,2 @@ +foo +bar diff --git a/core/lib/Thelia/Tests/FileFormat/Archive/ArchiveBuilder/TestResources/well_formatted.zip b/core/lib/Thelia/Tests/FileFormat/Archive/ArchiveBuilder/TestResources/well_formatted.zip index e69de29bb..b19e372b9 100644 Binary files a/core/lib/Thelia/Tests/FileFormat/Archive/ArchiveBuilder/TestResources/well_formatted.zip and b/core/lib/Thelia/Tests/FileFormat/Archive/ArchiveBuilder/TestResources/well_formatted.zip differ diff --git a/core/lib/Thelia/Tests/FileFormat/Archive/ArchiveBuilder/ZipArchiveBuilderTest.php b/core/lib/Thelia/Tests/FileFormat/Archive/ArchiveBuilder/ZipArchiveBuilderTest.php index 68c5fbfad..aae3402fc 100644 --- a/core/lib/Thelia/Tests/FileFormat/Archive/ArchiveBuilder/ZipArchiveBuilderTest.php +++ b/core/lib/Thelia/Tests/FileFormat/Archive/ArchiveBuilder/ZipArchiveBuilderTest.php @@ -48,8 +48,6 @@ class ZipArchiveBuilderTest extends \PHPUnit_Framework_TestCase /** * This method formats a path to be compatible with \ZipArchive - * - * */ public function testGetFilePath() {