Raw url accept also the file extension

Implements and closes #78
This commit is contained in:
Sergio Brighenti 2019-11-13 01:07:38 +01:00
parent 09eeac3ed1
commit 20d13c9bd1
4 changed files with 28 additions and 2 deletions

View file

@ -9,6 +9,7 @@ use League\Flysystem\FileNotFoundException;
use League\Flysystem\Filesystem;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Exception\HttpBadRequestException;
use Slim\Exception\HttpNotFoundException;
use Slim\Exception\HttpUnauthorizedException;
@ -218,17 +219,23 @@ class UploadController extends Controller
* @param Response $response
* @param string $userCode
* @param string $mediaCode
* @param string|null $ext
* @return Response
* @throws FileNotFoundException
* @throws HttpNotFoundException
*/
public function showRaw(Request $request, Response $response, string $userCode, string $mediaCode): Response
public function showRaw(Request $request, Response $response, string $userCode, string $mediaCode, ?string $ext = null): Response
{
$media = $this->getMedia($userCode, $mediaCode);
if (!$media || !$media->published && $this->session->get('user_id') !== $media->user_id && !$this->session->get('admin', false)) {
throw new HttpNotFoundException($request);
}
if($ext !== null && pathinfo($media->filename, PATHINFO_EXTENSION) !== $ext){
throw new HttpBadRequestException($request);
}
return $this->streamMedia($request, $response, $this->storage, $media);
}

View file

@ -5,6 +5,7 @@ namespace App\Exception\Handlers\Renderers;
use App\Exceptions\UnderMaintenanceException;
use Slim\Exception\HttpBadRequestException;
use Slim\Exception\HttpForbiddenException;
use Slim\Exception\HttpMethodNotAllowedException;
use Slim\Exception\HttpNotFoundException;
@ -40,6 +41,10 @@ class HtmlErrorRenderer implements ErrorRendererInterface
return view()->string( 'errors/404.twig');
}
if ($exception instanceof HttpBadRequestException) {
return view()->string( 'errors/400.twig');
}
return view()->string('errors/500.twig', ['exception' => $displayErrorDetails ? $exception : null]);
}
}

View file

@ -66,5 +66,5 @@ $app->post('/upload', [UploadController::class, 'upload'])->setName('upload');
$app->get('/{userCode}/{mediaCode}', [UploadController::class, 'show'])->setName('public');
$app->get('/{userCode}/{mediaCode}/delete/{token}', [UploadController::class, 'show'])->setName('public.delete.show')->add(CheckForMaintenanceMiddleware::class);
$app->post('/{userCode}/{mediaCode}/delete/{token}', [UploadController::class, 'deleteByToken'])->setName('public.delete')->add(CheckForMaintenanceMiddleware::class);
$app->get('/{userCode}/{mediaCode}/raw', [UploadController::class, 'showRaw'])->setName('public.raw');
$app->get('/{userCode}/{mediaCode}/raw[.{ext}]', [UploadController::class, 'showRaw'])->setName('public.raw');
$app->get('/{userCode}/{mediaCode}/download', [UploadController::class, 'download'])->setName('public.download');

View file

@ -0,0 +1,14 @@
{% extends 'base.twig' %}
{% block title %}Forbidden{% endblock %}
{% block content %}
<div class="container-fluid mt-5">
<div class="text-center">
<h1 class="display-1">400 Bad Request</h1>
<p class="lead">The server cannot or will not process the request due to an apparent client error.</p>
</div>
</div>
{% endblock %}
{% block footer %}{% endblock %}