From 50fb4b0dd8bb423ac308aac82b2f8a7f17ecb438 Mon Sep 17 00:00:00 2001 From: Benjamin Perche Date: Wed, 2 Jul 2014 15:14:52 +0200 Subject: [PATCH] =?UTF-8?q?Begin=20tar,=20tar.bz2=20and=20tar.gz=20formatt?= =?UTF-8?q?er,=20fix=20zip=20test=20resources=20=09modifi=C3=A9:=20=20=20?= =?UTF-8?q?=20=20=20=20=20=20core/lib/Thelia/Config/Resources/config.xml?= =?UTF-8?q?=20=09modifi=C3=A9:=20=20=20=20=20=20=20=20=20core/lib/Thelia/C?= =?UTF-8?q?ore/FileFormat/Archive/AbstractArchiveBuilder.php=20=09modifi?= =?UTF-8?q?=C3=A9:=20=20=20=20=20=20=20=20=20core/lib/Thelia/Core/FileForm?= =?UTF-8?q?at/Archive/ArchiveBuilder/ZipArchiveBuilder.php=20=09modifi?= =?UTF-8?q?=C3=A9:=20=20=20=20=20=20=20=20=20core/lib/Thelia/Tests/FileFor?= =?UTF-8?q?mat/Archive/ArchiveBuilder/TestResources/bad=5Fformatted.zip=20?= =?UTF-8?q?=09modifi=C3=A9:=20=20=20=20=20=20=20=20=20core/lib/Thelia/Test?= =?UTF-8?q?s/FileFormat/Archive/ArchiveBuilder/TestResources/well=5Fformat?= =?UTF-8?q?ted.zip=20=09modifi=C3=A9:=20=20=20=20=20=20=20=20=20core/lib/T?= =?UTF-8?q?helia/Tests/FileFormat/Archive/ArchiveBuilder/ZipArchiveBuilder?= =?UTF-8?q?Test.php?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/lib/Thelia/Config/Resources/config.xml | 18 +++ .../Archive/AbstractArchiveBuilder.php | 73 +++++++++++++ .../ArchiveBuilder/ZipArchiveBuilder.php | 103 +++++------------- .../TestResources/bad_formatted.zip | 2 + .../TestResources/well_formatted.zip | Bin 0 -> 2789 bytes .../ArchiveBuilder/ZipArchiveBuilderTest.php | 2 - 6 files changed, 119 insertions(+), 79 deletions(-) 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 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..b19e372b9636f905a1ccfdf3f7a37735e61e21ee 100644 GIT binary patch literal 2789 zcmVqAA7dZ+50OK7101E&d08B|kMNU&iE_8TwRa6N8 z1BJ`8Q;o~BQ+0R?009K)0{{R7=>q@&y;*H<+_(|`u3tg;%h|x*=CxP!P!w=|PMX4( zGvZ5&e&kBrT{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4#E literal 0 HcmV?d00001 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() {