diff --git a/core/lib/Thelia/Core/FileFormat/Archive/ArchiveBuilder/TarArchiveBuilder.php b/core/lib/Thelia/Core/FileFormat/Archive/ArchiveBuilder/TarArchiveBuilder.php index b9ccb6a6a..08cd483e9 100644 --- a/core/lib/Thelia/Core/FileFormat/Archive/ArchiveBuilder/TarArchiveBuilder.php +++ b/core/lib/Thelia/Core/FileFormat/Archive/ArchiveBuilder/TarArchiveBuilder.php @@ -13,9 +13,10 @@ namespace Thelia\Core\FileFormat\Archive\ArchiveBuilder; use Thelia\Core\FileFormat\Archive\AbstractArchiveBuilder; use Thelia\Core\FileFormat\Archive\ArchiveBuilder\Exception\TarArchiveException; -use Thelia\Core\FileFormat\Archive\ArchiveBuilderInterface; +use Thelia\Core\HttpFoundation\Response; use Thelia\Core\Thelia; use Thelia\Core\Translation\Translator; +use Thelia\Exception\FileNotReadableException; use Thelia\Log\Tlog; use Thelia\Tools\FileDownload\FileDownloaderInterface; @@ -68,7 +69,7 @@ class TarArchiveBuilder extends AbstractArchiveBuilder $this->compression = $compressionType; } - /** + public function __destruct() { if ($this->tar instanceof \PharData) { @@ -76,7 +77,7 @@ class TarArchiveBuilder extends AbstractArchiveBuilder unlink($this->cacheFile); } } - }*/ + } /** * @param string $filePath It is the path to access the file. @@ -118,6 +119,11 @@ class TarArchiveBuilder extends AbstractArchiveBuilder $this->tar->addFile($filePath, $name); + /** + * And clear the download temp file + */ + unlink($fileDownloadCache); + return $this; } @@ -257,9 +263,32 @@ class TarArchiveBuilder extends AbstractArchiveBuilder */ public function buildArchiveResponse() { - $this->tar->setMetadata("comment", "Generated by Thelia v" . Thelia::THELIA_VERSION); + $this->tar->setMetadata("Generated by Thelia v" . Thelia::THELIA_VERSION); + if (!is_file($this->cacheFile)) { + $this->throwFileNotFound($this->cacheFile); + } + if (!is_readable($this->cacheFile)) { + throw new FileNotReadableException( + $this->translator->trans( + "The file %file is not readable", + [ + "%file" => $this->cacheFile + ] + ) + ); + } + + $content = file_get_contents($this->cacheFile); + + return new Response( + $content, + 200, + [ + "Content-Type" => $this->getMimeType(), + ] + ); } /** diff --git a/core/lib/Thelia/Tests/FileFormat/Archive/ArchiveBuilder/TarArchiveBuilderTest.php b/core/lib/Thelia/Tests/FileFormat/Archive/ArchiveBuilder/TarArchiveBuilderTest.php index 6c3bc61a7..323300714 100644 --- a/core/lib/Thelia/Tests/FileFormat/Archive/ArchiveBuilder/TarArchiveBuilderTest.php +++ b/core/lib/Thelia/Tests/FileFormat/Archive/ArchiveBuilder/TarArchiveBuilderTest.php @@ -132,7 +132,6 @@ class TarArchiveBuilderTest extends \PHPUnit_Framework_TestCase ); } - public function testDeleteFile() { $this->tar->addFileFromString( @@ -149,4 +148,138 @@ class TarArchiveBuilderTest extends \PHPUnit_Framework_TestCase $this->tar->hasFile("bar") ); } + + public function testLoadValidArchive() + { + $tar = TarArchiveBuilder::loadArchive( + __DIR__ . DS . "TestResources/well_formatted.tar", + "dev" + ); + + $this->assertInstanceOf( + get_class($this->tar), + $tar + ); + + $this->assertTrue( + $tar->hasFile("LICENSE.txt") + ); + } + + /** + * @expectedException \Thelia\Core\FileFormat\Archive\ArchiveBuilder\Exception\TarArchiveException + */ + public function testLoadInvalidArchive() + { + $tar = TarArchiveBuilder::loadArchive( + __DIR__ . DS . "TestResources/bad_formatted.tar", + "dev" + ); + } + + public function testFormatDirectoryPath() + { + $this->assertEquals( + "foo/", + $this->tar->formatDirectoryPath("foo") + ); + + $this->assertEquals( + "foo/", + $this->tar->formatDirectoryPath("/foo") + ); + + $this->assertEquals( + "foo/", + $this->tar->formatDirectoryPath("foo/") + ); + + $this->assertEquals( + "foo/", + $this->tar->formatDirectoryPath("/foo/") + ); + + $this->assertEquals( + "foo/bar/", + $this->tar->formatDirectoryPath("foo/bar") + ); + + $this->assertEquals( + "foo/bar/", + $this->tar->formatDirectoryPath("/foo/bar") + ); + + $this->assertEquals( + "foo/bar/", + $this->tar->formatDirectoryPath("/foo//bar/") + ); + + $this->assertEquals( + "foo/bar/", + $this->tar->formatDirectoryPath("/foo/bar/") + ); + + $this->assertEquals( + "foo/bar/baz/", + $this->tar->formatDirectoryPath("foo/bar/baz") + ); + + $this->assertEquals( + "foo/bar/baz/", + $this->tar->formatDirectoryPath("//foo/bar///baz/") + ); + } + + public function testFormatFilePath() + { + $this->assertEquals( + "foo", + $this->tar->formatFilePath("foo") + ); + + $this->assertEquals( + "foo", + $this->tar->formatFilePath("/foo") + ); + + $this->assertEquals( + "foo", + $this->tar->formatFilePath("foo/") + ); + + $this->assertEquals( + "foo", + $this->tar->formatFilePath("/foo/") + ); + + $this->assertEquals( + "foo/bar", + $this->tar->formatFilePath("foo/bar") + ); + + $this->assertEquals( + "foo/bar", + $this->tar->formatFilePath("/foo/bar") + ); + + $this->assertEquals( + "foo/bar", + $this->tar->formatFilePath("/foo//bar/") + ); + + $this->assertEquals( + "foo/bar", + $this->tar->formatFilePath("/foo/bar/") + ); + + $this->assertEquals( + "foo/bar/baz", + $this->tar->formatFilePath("foo/bar/baz") + ); + + $this->assertEquals( + "foo/bar/baz", + $this->tar->formatFilePath("//foo/bar///baz/") + ); + } } \ No newline at end of file diff --git a/core/lib/Thelia/Tests/FileFormat/Archive/ArchiveBuilder/TestResources/bad_formatted.tar b/core/lib/Thelia/Tests/FileFormat/Archive/ArchiveBuilder/TestResources/bad_formatted.tar new file mode 100644 index 000000000..3bd1f0e29 --- /dev/null +++ b/core/lib/Thelia/Tests/FileFormat/Archive/ArchiveBuilder/TestResources/bad_formatted.tar @@ -0,0 +1,2 @@ +foo +bar diff --git a/core/lib/Thelia/Tests/FileFormat/Archive/ArchiveBuilder/TestResources/well_formatted.tar b/core/lib/Thelia/Tests/FileFormat/Archive/ArchiveBuilder/TestResources/well_formatted.tar new file mode 100644 index 000000000..3bff92c8e Binary files /dev/null and b/core/lib/Thelia/Tests/FileFormat/Archive/ArchiveBuilder/TestResources/well_formatted.tar differ diff --git a/core/lib/Thelia/Tests/FileFormat/Archive/ArchiveBuilder/ZipArchiveBuilderTest.php b/core/lib/Thelia/Tests/FileFormat/Archive/ArchiveBuilder/ZipArchiveBuilderTest.php index 7fd5aeed8..0f141d1b6 100644 --- a/core/lib/Thelia/Tests/FileFormat/Archive/ArchiveBuilder/ZipArchiveBuilderTest.php +++ b/core/lib/Thelia/Tests/FileFormat/Archive/ArchiveBuilder/ZipArchiveBuilderTest.php @@ -49,7 +49,7 @@ class ZipArchiveBuilderTest extends \PHPUnit_Framework_TestCase /** * This method formats a path to be compatible with \ZipArchive */ - public function testGetFilePath() + public function testFormatFilePath() { $this->assertEquals( "foo", @@ -102,7 +102,7 @@ class ZipArchiveBuilderTest extends \PHPUnit_Framework_TestCase ); } - public function testGetDirectoryPath() + public function testFormatDirectoryPath() { $this->assertEquals( "/foo/",