From 55a5f9023c8371fb80242b3b46ab16351b3b1166 Mon Sep 17 00:00:00 2001 From: Chris Kankiewicz Date: Mon, 17 Feb 2020 10:02:49 -0700 Subject: [PATCH] Removed leading path to files in generated zip archives --- app/src/Handlers/ZipHandler.php | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/app/src/Handlers/ZipHandler.php b/app/src/Handlers/ZipHandler.php index 506cbd5..9d3d8c2 100644 --- a/app/src/Handlers/ZipHandler.php +++ b/app/src/Handlers/ZipHandler.php @@ -8,6 +8,7 @@ use Psr\Http\Message\ResponseInterface; use Slim\Psr7\Request; use Slim\Psr7\Response; use Symfony\Component\Finder\Finder; +use Symfony\Component\Finder\SplFileInfo; use Tightenco\Collect\Support\Collection; use ZipArchive; @@ -47,17 +48,13 @@ class ZipHandler return $response->withStatus(404, 'File not found'); } - $tempFile = new TemporaryFile( - $this->container->get('base_path') . '/app/cache' - ); - $zip = new ZipArchive; - $zip->open((string) $tempFile, ZipArchive::CREATE | ZipArchive::OVERWRITE); + $zip->open((string) $tempFile = new TemporaryFile( + $this->container->get('base_path') . '/app/cache' + ), ZipArchive::CREATE | ZipArchive::OVERWRITE); - foreach ($this->finder->in($path) as $file) { - if ($file->isFile()) { - $zip->addFile($file->getRealPath(), $file->getPathname()); - } + foreach ($this->finder->in($path)->files() as $file) { + $zip->addFile($file->getRealPath(), $this->stripPath($file, $path)); } $zip->close(); @@ -72,4 +69,19 @@ class ZipHandler $filename == '.' ? 'Home' : $filename )); } + + /** + * Return the path to a file with the preceding root path stripped. + * + * @param \Symfony\Component\Finder\SplFileInfo $file + * @param string $path + * + * @return string + */ + protected function stripPath(SplFileInfo $file, string $path): string + { + $pattern = sprintf('/^%s\/?/', preg_quote($path, '/')); + + return preg_replace($pattern, '', $file->getPathname()); + } }