Added a configuration option to control whether zip downloads are enabled

This commit is contained in:
Chris Kankiewicz
2020-02-18 13:20:10 -07:00
parent 6fd2212bc9
commit 86402e2da6
6 changed files with 39 additions and 6 deletions

View File

@@ -2,6 +2,8 @@ DARK_MODE=false
DISPLAY_READMES=true
ZIP_DOWNLOADS=true
GOOGLE_ANALYTICS_ID=false
SORT_ORDER=type

View File

@@ -17,6 +17,13 @@ return [
*/
'display_readmes' => Helpers::env('DISPLAY_READMES'),
/**
* Enable downloading of directories as a zip archive.
*
* Default value: true
*/
'zip_downloads' => Helpers::env('ZIP_DOWNLOADS'),
/**
* Your Google analytics tracking ID.
* Expected format: 'UA-123456789-0'.

View File

@@ -4,6 +4,7 @@ namespace App\Handlers;
use App\TemporaryFile;
use DI\Container;
use PHLAK\Config\Config;
use Psr\Http\Message\ResponseInterface;
use Slim\Psr7\Request;
use Slim\Psr7\Response;
@@ -17,6 +18,9 @@ class ZipHandler
/** @var Container The application container */
protected $container;
/** @var Config The application config */
protected $config;
/** @var Finder The Finder Component */
protected $finder;
@@ -26,9 +30,10 @@ class ZipHandler
* @param \DI\Container $container
* @param \PhpCsFixer\Finder $finder
*/
public function __construct(Container $container, Finder $finder)
public function __construct(Container $container, Config $config, Finder $finder)
{
$this->container = $container;
$this->config = $config;
$this->finder = $finder;
}
@@ -44,7 +49,7 @@ class ZipHandler
{
$path = $request->getQueryParams()['zip'];
if (! realpath($path)) {
if (! $this->config->get('app.zip_downloads', true) || ! realpath($path)) {
return $response->withStatus(404, 'File not found');
}

View File

@@ -11,7 +11,7 @@
</div>
<div class="flex justify-end items-center">
{% if path %}
{% if path and config('zip_downloads', true) %}
<a href="?zip={{ path }}" title="Download this Directory" class="inline-block text-white mx-2 p-1 hover:text-gray-400">
<i class="fas fa-download fa-lg"></i>
</a>

View File

@@ -13,7 +13,7 @@ class ZipHandlerTest extends TestCase
{
public function test_it_returns_a_successful_response_for_a_zip_request(): void
{
$handler = new ZipHandler($this->container, new Finder);
$handler = new ZipHandler($this->container, $this->config, new Finder);
$request = $this->createMock(Request::class);
$request->method('getQueryParams')->willReturn(['zip' => 'subdir']);
@@ -30,7 +30,7 @@ class ZipHandlerTest extends TestCase
public function test_it_returns_a_404_error_when_not_found(): void
{
$handler = new ZipHandler($this->container, new Finder);
$handler = new ZipHandler($this->container, $this->config, new Finder);
$request = $this->createMock(Request::class);
$request->method('getQueryParams')->willReturn(['zip' => '404']);
@@ -41,4 +41,19 @@ class ZipHandlerTest extends TestCase
$this->assertInstanceOf(ResponseInterface::class, $response);
$this->assertEquals(404, $response->getStatusCode());
}
public function test_it_returns_a_404_error_when_disabled_via_config(): void
{
$this->config->set('app.zip_downloads', false);
$handler = new ZipHandler($this->container, $this->config, new Finder);
$request = $this->createMock(Request::class);
$request->method('getQueryParams')->willReturn(['zip' => 'subdir']);
chdir($this->filePath('.'));
$response = $handler($request, new Response);
$this->assertInstanceOf(ResponseInterface::class, $response);
$this->assertEquals(404, $response->getStatusCode());
}
}

View File

@@ -28,12 +28,16 @@ class TestCase extends PHPUnitTestCase
$this->config = new Config([
'app' => [
'dark_mode' => false,
'display_readmes' => true,
'zip_downloads' => true,
'google_analytics_id' => false,
'sort_order' => 'type',
'reverse_sort' => false,
'hidden_files' => [],
'hide_app_files' => true,
'hide_vcs_files' => false,
'display_readmes' => true,
'date_format' => 'Y-m-d H:i:s',
'max_hash_size' => 1000000000,
'view_cache' => false,
]