|
4 | 4 | use Opencast\Models\VideosShares; |
5 | 5 | use Opencast\Models\LTI\LtiHelper; |
6 | 6 | use Opencast\Models\REST\ApiEventsClient; |
| 7 | +use Opencast\Errors\Error; |
7 | 8 |
|
8 | 9 | class RedirectController extends Opencast\Controller |
9 | 10 | { |
@@ -87,15 +88,46 @@ public function download_action($token, $type, $index) |
87 | 88 |
|
88 | 89 | $publication = $video->publication? json_decode($video->publication, true) : null; |
89 | 90 | if (!empty($publication) && isset($publication['downloads'][$type][$index]['url'])) { |
90 | | - $url = $publication['downloads'][$type][$index]['url']; |
91 | 91 |
|
92 | | - $api_events = ApiEventsClient::getInstance($video->config_id); |
93 | | - $response = $api_events->fileRequest($url); |
| 92 | + // Make sure the server configs are overwritten, in order to allow large file downloads. |
| 93 | + ignore_user_abort(true); |
| 94 | + set_time_limit(0); |
| 95 | + ini_set('memory_limit', '512M'); |
94 | 96 |
|
95 | | - header('Content-Type: '. $response['mimetype']); |
96 | | - |
97 | | - echo $response['body']; |
98 | | - die; |
| 97 | + // Clean all output buffers. |
| 98 | + while (ob_get_level()) { |
| 99 | + ob_end_clean(); |
| 100 | + } |
| 101 | + try { |
| 102 | + $url = $publication['downloads'][$type][$index]['url']; |
| 103 | + |
| 104 | + $api_events = ApiEventsClient::getInstance($video->config_id); |
| 105 | + // Since we are using stream, we need to perform the get directly here! Doing it in another file and pass the body as a parameter won't work! |
| 106 | + $response = $api_events->ocRestClient->get($url, $api_events->getStreamDownloadConfig()); |
| 107 | + $stream = $response->getBody(); |
| 108 | + |
| 109 | + // Set headers properly. |
| 110 | + header('Content-Type: ' . $response->getHeaderLine('Content-Type') ?: 'application/octet-stream'); |
| 111 | + header('Content-Length: ' . $response->getHeaderLine('Content-Length')); |
| 112 | + header('Content-Disposition: attachment; filename*=UTF-8\'\'' . basename($url)); |
| 113 | + header('Cache-Control: no-cache'); |
| 114 | + header('Pragma: no-cache'); |
| 115 | + |
| 116 | + // Stream in chunks |
| 117 | + while (!$stream->eof()) { |
| 118 | + // A double check to make sure the connection is still open. |
| 119 | + if(connection_status() != CONNECTION_NORMAL){ |
| 120 | + // If not leave the loop! |
| 121 | + break; |
| 122 | + } |
| 123 | + // 1MB per chunk. |
| 124 | + echo $stream->read(1024 * 1024); |
| 125 | + flush(); |
| 126 | + } |
| 127 | + die; |
| 128 | + } catch (\Throwable $th) { |
| 129 | + throw new Error(_('Fehler beim Herunterladen der Datei').': ' . $th->getMessage(), 500); |
| 130 | + } |
99 | 131 | } |
100 | 132 | } |
101 | 133 |
|
|
0 commit comments