Skip to content

Commit

Permalink
MED-3: Improve Streamio integration
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Thies committed Jan 30, 2024
1 parent 79eecc4 commit 42cc8ab
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 35 deletions.
2 changes: 1 addition & 1 deletion source/streamio/amd/build/file_upload.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion source/streamio/amd/build/file_upload.min.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions source/streamio/amd/src/file_upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ var upload;

import * as tus from 'mediatimesrc_streamio/tus';
import Log from 'core/log';
import cfg from 'core/config';

const options = {
endpoint: cfg.wwwroot + '/api/v1/videos/tus',
endpoint: 'https://streamio.com/api/v1/videos/tus',
metadata: {},
onError: Log.debug,
onProgress: function(bytesUploaded, bytesTotal) {
Expand Down
33 changes: 28 additions & 5 deletions source/streamio/classes/form/edit_resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,7 @@ public function definition() {
$mform->addElement('text', 'name', get_string('resourcename', 'tool_mediatime'));
$mform->setType('name', PARAM_TEXT);
$mform->addHelpButton('name', 'resourcename', 'tool_mediatime');

$mform->addElement('text', 'title', get_string('title', 'tool_mediatime'));
$mform->setType('title', PARAM_TEXT);
$mform->addHelpButton('title', 'title', 'tool_mediatime');
$mform->addRule('name', get_string('required'), 'required', null, 'client');

$mform->addElement('hidden', 'edit');
$mform->setType('edit', PARAM_INT);
Expand All @@ -66,8 +63,14 @@ public function definition() {
$mform->setType('newfile', PARAM_INT);
$mform->addHelpButton('filesource', 'videofile', 'mediatimesrc_streamio');

$mform->addElement('text', 'title', get_string('title', 'tool_mediatime'));
$mform->setType('title', PARAM_TEXT);
$mform->addHelpButton('title', 'title', 'tool_mediatime');

$mform->addElement('textarea', 'description', get_string('description'));
$mform->setType('description', PARAM_TEXT);
$mform->disabledIf('description', 'newfile', 0);
$mform->disabledIf('title', 'newfile', 0);

$this->tag_elements();

Expand Down Expand Up @@ -108,7 +111,7 @@ public function definition_after_data() {
$mform->insertElementBefore(
$mform->createElement('autocomplete', 'file', '', $options, [
]),
'description'
'title'
);
$mform->hideIf('file', 'newfile', 'eq', 1);
$mform->setDefault('newfile', 1);
Expand All @@ -120,6 +123,26 @@ public function definition_after_data() {
'description'
);
}
if (!has_capability('mediatimesrc/streamio:upload', context_system::instance())) {
$mform->addRule('file', get_string('required'), 'required', null, 'client');
}
}
}
/**
* Validate data
*
* @param array $data array of ("fieldname"=>value) of submitted data
* @param array $files array of uploaded files "element_name"=>tmp_file_path
* @return array of "element_name"=>"error_description" if there are errors,
* or an empty array if everything is OK (true allowed for backwards compatibility too).
*/
public function validation($data, $files) {
$errors = [];

if (empty($data['newfile']) && empty($data['file'])) {
$errors['file'] = get_string('required');
}

return $errors;
}
}
19 changes: 14 additions & 5 deletions source/streamio/classes/manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ public function __construct($record = null) {

$upload = optional_param('upload', null, PARAM_INT);
if (!empty($upload) && !optional_param('cancel', false, PARAM_BOOL)) {
require_sesskey();
require_capability('mediatimesrc/streamio:upload', context_system::instance());
$video = $this->api->request("/videos", ['tags' => (string)$upload])[0];
$this->api->request("/videos/$video->id", [
'tags' => implode(array_diff($video->tags, ["$upload", "mediatimeupload"])),
Expand Down Expand Up @@ -110,20 +112,24 @@ public function __construct($record = null) {
$redirect = new moodle_url('/admin/tool/mediatime/index.php');
redirect($redirect);
} else if (($data = $this->form->get_data()) && empty($data->newfile)) {
require_sesskey();
$data->timemodified = time();
$data->usermodified = $USER->id;

if (empty($data->edit)) {
$video = $this->api->request("/videos/$data->file");
$video->name = $data->name;
$data->content = json_encode($video);
$data->timecreated = $data->timemodified;
$data->edit = $DB->insert_record('tool_mediatime', $data);
} else {
$data->id = $data->edit;
$this->api->request("/videos/" . $this->content->id, array_intersect_key((array)$data, [
'description' => true,
'title' => true,
]), 'PUT');
if (has_capability('mediatimesrc/streamio:upload', context_system::instance())) {
$this->api->request("/videos/" . $this->content->id, array_intersect_key((array)$data, [
'description' => true,
'title' => true,
]), 'PUT');
}
$video = $this->api->request("/videos/" . $this->content->id);
$video->name = $data->name;
$data->content = json_encode($video);
Expand Down Expand Up @@ -161,7 +167,11 @@ public function export_for_template(renderer_base $output) {
'resource' => $output->render($resource),
];
} else if (($data = $this->form->get_data()) && !empty($data->newfile)) {
require_sesskey();
require_capability('mediatimesrc/streamio:upload', context_system::instance());

$data->upload = file_get_unused_draft_itemid();
$data->sesskey = sesskey();

$data->token = $this->api->create_token([
'tags' => "mediatimeupload,$data->upload",
Expand All @@ -171,7 +181,6 @@ public function export_for_template(renderer_base $output) {
]))->token;
$data->tags = json_encode($data->tags);

require_capability('mediatimesrc/streamio:upload', context_system::instance());
return [
'form' => $output->render_from_template('mediatimesrc_streamio/file_upload', $data),
];
Expand Down
23 changes: 12 additions & 11 deletions source/streamio/templates/file_upload.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,22 @@

}}
<form id="upload_resource_form" method="post" action=".">
<div class="container-fluid">
<input type="hidden" name="source" value="{{ source }}" >
<input type="hidden" name="upload" value="{{ upload }}" >
<input type="hidden" name="name" value="{{ name }}" >
<input type="hidden" name="sesskey" value="{{ sesskey }}" >
<input type="hidden" name="tags" value="{{ tags }}" >
<fieldset class="w-100 m-0 p-0 border-0">
<input type="file" name="streamiofile">
</fieldset>
<div class="col bg-secondary p-0 m-3">
<div class="progress bg-primary" style="width: 0%; height: 1em;"></div>
</div>
<div class="p-3">
<button name="upload" type="button" class="btn btn-primary">{{# str }} upload {{/ str }}</button>
<button name="cancel" value="true" class="btn btn-secondary">{{# str }} cancel {{/ str }}</button>
</div>
<div class="container-fluid">
<fieldset class="w-100 m-0 p-0 border-0">
<input type="file" name="streamiofile">
</fieldset>
<div class="col bg-secondary p-0 m-3">
<div class="progress bg-primary" style="width: 0%; height: 1em;"></div>
</div>
<div class="p-3">
<button name="upload" type="button" class="btn btn-primary">{{# str }} upload {{/ str }}</button>
<button name="cancel" value="true" class="btn btn-secondary">{{# str }} cancel {{/ str }}</button>
</div>
</div>
</form>
{{# js }}
Expand Down
2 changes: 2 additions & 0 deletions source/videotime/classes/form/edit_resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public function definition() {
$mform->setDefault('source', 'videotime');

$mform->addElement('text', 'name', get_string('resourcename', 'tool_mediatime'));
$mform->addRule('name', get_string('required'), 'required', null, 'client');
$mform->setType('name', PARAM_TEXT);
$mform->addHelpButton('name', 'resourcename', 'tool_mediatime');

Expand Down Expand Up @@ -79,6 +80,7 @@ public function definition() {
]
);
$mform->addHelpButton('videofile', 'videofile', 'mediatimesrc_videotime');
$mform->addRule('videofile', get_string('required'), 'required', null, 'client');

$mform->addElement(
'filemanager',
Expand Down
11 changes: 1 addition & 10 deletions source/videotime/classes/manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,7 @@ public function __construct($record = null) {
if ($edit = optional_param('edit', null, PARAM_INT)) {
$draftitemid = file_get_submitted_draft_itemid('videofile');
file_prepare_draft_area(
// The $draftitemid is the target location.
$draftitemid,

// The combination of contextid / component / filearea / itemid
// form the virtual bucket that files are currently stored in
// and will be copied from.
$context->id,
'mediatimesrc_videotime',
'videofile',
Expand All @@ -88,12 +83,7 @@ public function __construct($record = null) {

$draftitemid = file_get_submitted_draft_itemid('posterimage');
file_prepare_draft_area(
// The $draftitemid is the target location.
$draftitemid,

// The combination of contextid / component / filearea / itemid
// form the virtual bucket that files are currently stored in
// and will be copied from.
$context->id,
'mediatimesrc_videotime',
'posterimage',
Expand All @@ -115,6 +105,7 @@ public function __construct($record = null) {
$redirect = new moodle_url('/admin/tool/mediatime/index.php');
redirect($redirect);
} else if ($data = $this->form->get_data()) {
require_sesskey();
$data->timemodified = time();
$data->usermodified = $USER->id;

Expand Down

0 comments on commit 42cc8ab

Please sign in to comment.