Skip to content
Snippets Groups Projects
Commit 5df75382 authored by Jan-Hendrik Willms's avatar Jan-Hendrik Willms Committed by André Noack
Browse files

adjust render_file and render_temporary_file on stud.ip controller since...

adjust render_file and render_temporary_file on stud.ip controller since render_text() can't accept callables anymore, fixes #5064

Closes #5064

Merge request studip/studip!3789
parent 9278a5db
No related branches found
No related tags found
No related merge requests found
......@@ -477,22 +477,21 @@ abstract class StudipController extends Trails\Controller
/**
* Renders a file
* @param string $file Path of the file to render
* @param string $filename Name of the file displayed to user
*
* @param string $file Path of the file to render
* @param string|null $filename Name of the file displayed to user
* (will equal $file when missing)
* @param string $content_type Optional content type (will be determined if missing)
* @param string $content_disposition Either attachment (default) or inline
* @param Closure $callback Optional callback when download has finished
* @param int $chunk_size Optional size of chunks to send (default: 256k)
* @param string|null $content_type Optional content type (will be determined if missing)
* @param string $content_disposition Either attachment (default) or inline
* @param Closure|null $callback Optional callback when download has finished
*/
public function render_file(
$file,
$filename = null,
$content_type = null,
$content_disposition = 'attachment',
Closure $callback = null,
$chunk_size = 262144
) {
string $file,
?string $filename = null,
?string $content_type = null,
string $content_disposition = 'attachment',
?Closure $callback = null
): void {
if (!file_exists($file)) {
throw new Trails\Exception(404);
}
......@@ -524,19 +523,16 @@ abstract class StudipController extends Trails\Controller
$this->response->add_header('Content-Length', filesize($file));
$this->response->add_header('Content-Transfer-Encoding', 'binary');
$this->response->add_header('Pragma', 'public');
$this->render_text(function () use ($file, $chunk_size, $callback) {
$fp = fopen($file, 'rb');
while (!feof($fp)) {
yield fgets($fp, $chunk_size);
}
fclose($fp);
$this->render_text(
app(Psr\Http\Message\StreamFactoryInterface::class)->createStreamFromFile($file)
);
if ($callback) {
if ($callback) {
NotificationCenter::on('SLIM_AFTER_RUN', function () use ($callback, $file) {
$callback($file);
}
});
});
}
}
/**
......@@ -544,23 +540,20 @@ abstract class StudipController extends Trails\Controller
* This is just a convenience method so you don't have to write the delete
* callback.
*
* @param string $file Path of the file to render
* @param string $filename Name of the file displayed to user
* @param string $file Path of the file to render
* @param string|null $filename Name of the file displayed to user
* (will equal $file when missing)
* @param string $content_type Optional content type (will be determined if missing)
* @param string $content_disposition Either attachment (default) or inline
* @param Closure $callback Optional callback when download has finished
* @param int $chunk_size Optional size of chunks to send (default: 256k)
* @param string|null $content_type Optional content type (will be determined if missing)
* @param string $content_disposition Either attachment (default) or inline
* @param Closure|null $callback Optional callback when download has finished
*/
public function render_temporary_file(
$file,
$filename = null,
$content_type = null,
$content_disposition = 'attachment',
Closure $callback = null,
$chunk_size = 262144
) {
string $file,
?string $filename = null,
?string $content_type = null,
string $content_disposition = 'attachment',
?Closure $callback = null
): void {
$delete_callback = function ($file) use ($callback) {
unlink($file);
......@@ -574,8 +567,7 @@ abstract class StudipController extends Trails\Controller
$filename,
$content_type,
$content_disposition,
$delete_callback,
$chunk_size
$delete_callback
);
}
......
......@@ -95,7 +95,11 @@ class StudipDispatcher extends Trails\Dispatcher
$uri = $this->clean_request_uri((string) $uri);
[$controller_path, $unconsumed] = '' === $uri ? $this->default_route() : $this->parse($uri);
$controller = $this->load_controller($controller_path);
return function ($request, $response, array $args) use ($controller, $unconsumed) {
return function (
\Psr\Http\Message\ServerRequestInterface $request,
\Psr\Http\Message\ResponseInterface $response,
array $args
) use ($controller, $unconsumed): \Psr\Http\Message\ResponseInterface {
$controller->injectResponse($response);
$response = $controller->perform($unconsumed);
return $response->getPsrResponse();
......
......@@ -33,3 +33,4 @@ $route_callable = $studip_dispatcher->getRouteCallable(Request::pathInfo());
$app->any(Request::pathInfo(), $route_callable);
NotificationCenter::postNotification('SLIM_BEFORE_RUN', $app);
$app->run();
NotificationCenter::postNotification('SLIM_AFTER_RUN', $app);
......@@ -70,3 +70,4 @@ $app->add(app(Studip\Middleware\SessionMiddleware::class));
NotificationCenter::postNotification('SLIM_BEFORE_RUN', $app);
$app->run();
NotificationCenter::postNotification('SLIM_AFTER_RUN', $app);
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment