Skip to content

Commit

Permalink
MED-3: Use cURL to access Streamio
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Thies committed Feb 5, 2024
1 parent 36c166b commit db77fb1
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 30 deletions.
25 changes: 13 additions & 12 deletions source/streamio/classes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,20 +65,21 @@ public function __construct() {
* @return mixed
*/
public function request($endpoint, $params = [], $method = 'GET') {
$opts = [
'http' => [
"header" => "Authorization: Basic " . base64_encode("$this->username:$this->password")
. "\nAccept: application/json\n"
. "Content-type: application/json\n",
'method' => $method,
"protocol_version" => 1.1,
'content' => json_encode($params),
],
];
$ch = curl_init('https://streamio.com/api/v1' . $endpoint);

$context = stream_context_create($opts);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
"Authorization: Basic " . base64_encode("$this->username:$this->password"),
"Accept: application/json",
"Content-type: application/json",
]);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = file_get_contents('https://streamio.com/api/v1' . $endpoint, false, $context);
$response = curl_exec($ch);

curl_close($ch);

if ($response === false) {
throw new moodle_exception('streamiorequesterror');
Expand Down
7 changes: 6 additions & 1 deletion source/streamio/classes/manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,10 +220,15 @@ protected function save_file($id, $video) {
'filepath' => '/',
];

$ch = curl_init("https://streamio.com/api/v1/videos/$video->id/public_show.m3u8");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$fs->create_file_from_string(
$fileinfo,
file_get_contents("https://streamio.com/api/v1/videos/$video->id/public_show.m3u8")
curl_exec($ch)
);

curl_close($ch);
}
}
}
25 changes: 8 additions & 17 deletions source/streamio/classes/output/media_resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,23 +95,8 @@ public function video_url($output) {
$this->videourl = '';

$id = $this->record->content->id;
return "https://streamio.com/api/v1/videos/$id/public_show.m3u8";

$fs = get_file_storage();
foreach ($fs->get_area_files($this->context->id, 'mediatimesrc_file', 'videofile', $this->record->id) as $file) {
if (!$file->is_directory()) {
$this->videourl = moodle_url::make_pluginfile_url(
$this->context->id,
'mediatimesrc_file',
'videofile',
$this->record->id,
$file->get_filepath(),
$file->get_filename()
)->out(false);
}
}

return $this->videourl;
return "https://streamio.com/api/v1/videos/$id/public_show.m3u8";
}

/**
Expand All @@ -121,6 +106,12 @@ public function video_url($output) {
* @return string url
*/
public function video_file_content($output) {
return file_get_contents($this->video_url($output));
$ch = curl_init($this->video_url($output));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);

curl_close($ch);

return $response;
}
}

0 comments on commit db77fb1

Please sign in to comment.