From db77fb16130780d4d31471c7a5f8bbb93d86118d Mon Sep 17 00:00:00 2001 From: Daniel Thies Date: Mon, 5 Feb 2024 15:43:30 -0600 Subject: [PATCH] MED-3: Use cURL to access Streamio --- source/streamio/classes/api.php | 25 ++++++++++--------- source/streamio/classes/manager.php | 7 +++++- .../classes/output/media_resource.php | 25 ++++++------------- 3 files changed, 27 insertions(+), 30 deletions(-) diff --git a/source/streamio/classes/api.php b/source/streamio/classes/api.php index bc4d899..cfbd9c4 100644 --- a/source/streamio/classes/api.php +++ b/source/streamio/classes/api.php @@ -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'); diff --git a/source/streamio/classes/manager.php b/source/streamio/classes/manager.php index 261c3a1..ec76ada 100644 --- a/source/streamio/classes/manager.php +++ b/source/streamio/classes/manager.php @@ -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); } } } diff --git a/source/streamio/classes/output/media_resource.php b/source/streamio/classes/output/media_resource.php index 1e8906e..f8a13bf 100644 --- a/source/streamio/classes/output/media_resource.php +++ b/source/streamio/classes/output/media_resource.php @@ -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"; } /** @@ -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; } }