Skip to content

Commit 86c52b2

Browse files
committed
Merge branch 'issue-1351' into issue-1325
2 parents f13db65 + 6a87cbf commit 86c52b2

File tree

2 files changed

+53
-7
lines changed

2 files changed

+53
-7
lines changed

app/controllers/redirect.php

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use Opencast\Models\VideosShares;
55
use Opencast\Models\LTI\LtiHelper;
66
use Opencast\Models\REST\ApiEventsClient;
7+
use Opencast\Errors\Error;
78

89
class RedirectController extends Opencast\Controller
910
{
@@ -87,15 +88,46 @@ public function download_action($token, $type, $index)
8788

8889
$publication = $video->publication? json_decode($video->publication, true) : null;
8990
if (!empty($publication) && isset($publication['downloads'][$type][$index]['url'])) {
90-
$url = $publication['downloads'][$type][$index]['url'];
9191

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');
9496

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+
}
99131
}
100132
}
101133

lib/Models/REST/RestClient.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,20 @@ public function __construct($config)
8282
$this->ocRestClient = new OcRestClient($oc_config);
8383
}
8484

85+
/**
86+
* Get the Guzzle client options suitable for stream downloading files.
87+
*
88+
* @return array
89+
*/
90+
public function getStreamDownloadConfig() {
91+
return [
92+
'auth' => [$this->username, $this->password],
93+
'timeout' => 0,
94+
'connect_timeout' => 0,
95+
'stream' => true,
96+
];
97+
}
98+
8599
public function fileRequest($file_url)
86100
{
87101
$response = $this->ocRestClient->get($file_url, [

0 commit comments

Comments
 (0)