Skip to content

Commit

Permalink
MED-100: Fix resumable upload
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Thies committed Apr 20, 2024
1 parent bfef526 commit 6bcff93
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 15 deletions.
2 changes: 1 addition & 1 deletion source/vimeo/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/vimeo/amd/build/file_upload.min.js.map

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

32 changes: 24 additions & 8 deletions source/vimeo/amd/src/file_upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,54 @@ import Config from 'core/config';
import Log from 'core/log';
import Notification from 'core/notification';

const upload = async(resource) => {
/**
* Handle uploading
*
* @param Vimeo token request response
*/
const upload = async (resource) => {
const file = document.querySelector('input[name="videofile"]').files[0];
const url = new URL(Config.wwwroot + '/admin/tool/mediatime/index.php');
let offset = 0;
let response;

url.searchParams.set('id', resource.id);

do {
const request = new Request(resource.uploadurl, {
body: file,
body: file.slice(Number(offset), Number(offset) + 128*2**20),
headers: {
'Tus-Resumable': '1.0.0',
'Upload-Offset': String(offset),
'Content-Type': 'application/offset+octet-stream'
},
method: 'PATCH'
});
response = await fetch(request);
document.querySelector('.progress').style.width = (response.headers.get('Upload-Offset') / file.size * 100) + '%';
Log.debug(response.headers.get('Upload-Offset'));
} while (response.headers.get('Upload-Offset') < file.size);
const response = await fetch(request).catch(e => {
Log.debug(e);
return new Promise(resolve => setTimeout(resolve, 1000));
});
if (response && response.ok) {
offset = response.headers.get('Upload-Offset');
}
document.querySelector('.progress').style.width = ( offset / file.size * 100) + '%';
} while (offset < file.size);
window.location.href = url;
};

export default {
/**
* Init event listeners
*/
init: function() {
document.body.removeEventListener('click', this.handleClick);
document.body.addEventListener('click', this.handleClick);
},

/**
* Handle button click
*
* Event e
*/
handleClick: function(e) {
const button = e.target.closest('button[name="upload"]');
if (button) {
Expand All @@ -52,7 +69,6 @@ export default {
fail: Notification.exception,
methodname: 'mediatimesrc_vimeo_create_token'
}]);
Log.debug(file.size);
}
}
};
6 changes: 3 additions & 3 deletions source/vimeo/classes/external/create_token.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,14 @@ public static function execute($description, $filesize, $name, $tags, $title): a
'size' => $params['filesize'],
],
]);
$video = $api->request($video['uri'], [
'name' => $params['title'],
$updatedvideo = $api->request($video['uri'], [
'name' => $params['title'] ?: $params['name'],
'description' => $params['description'],
], 'PATCH')['body'];
$id = $DB->insert_record('tool_mediatime', [
'name' => $params['name'],
'source' => 'vimeo',
'content' => json_encode($video),
'content' => json_encode($updatedvideo),
'timecreated' => time(),
'timemodified' => time(),
'usermodified' => $USER->id,
Expand Down
2 changes: 1 addition & 1 deletion source/vimeo/classes/form/delete_resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function definition() {
];
$mform->addGroup($action, 'vimeofileaction', get_string('vimeofileaction', 'mediatimesrc_vimeo'), [' '], false);
$mform->setType('action', PARAM_INT);
$mform->setDefault('action', 2);
$mform->setDefault('action', 1);
$mform->addHelpButton('vimeofileaction', 'vimeofileaction', 'mediatimesrc_vimeo');

$this->add_action_buttons(true, get_string('delete'));
Expand Down
1 change: 1 addition & 0 deletions source/vimeo/classes/form/edit_resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,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->addRule('name', get_string('required'), 'required', null, 'client');

$mform->addElement('text', 'title', get_string('title', 'tool_mediatime'));
$mform->setType('title', PARAM_TEXT);
Expand Down
15 changes: 14 additions & 1 deletion source/vimeo/classes/manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,17 @@ public function __construct($record = null) {

$this->content->tags = core_tag_tag::get_item_tags_array('tool_mediatime', 'tool_mediatime', $edit);

$data += ['name' => $this->record->name, 'title' => $this->content->name] + (array)$this->content;
// Check for updated values at Vimeo.
if (!$this->form->is_submitted()) {
$video = $this->api->request($this->content->uri)['body'];
$data = [
'description' => $video['description'],
'title' => $video['name'],
'name' => $this->record->name,
] + $data;
} else {
$data += ['name' => $this->record->name, 'title' => $this->content->name] + (array)$this->content;
}
}
$this->form->set_data($data);
if ($this->form->is_cancelled()) {
Expand All @@ -123,6 +133,9 @@ public function __construct($record = null) {
foreach ($fs->get_area_files(context_user::instance($USER->id)->id, 'user', 'draft', $data->videofile) as $file) {
if (!$file->is_directory()) {
$data->edit = $DB->insert_record('tool_mediatime', $data);
if (empty($data->title)) {
$data->title = $data->name;
}

file_save_draft_area_files(
$data->videofile,
Expand Down
2 changes: 2 additions & 0 deletions source/vimeo/lang/en/mediatimesrc_vimeo.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
$string['uploadnewfile'] = 'Upload new file';
$string['videofile'] = 'Video file';
$string['videofile_help'] = 'Choose to upload a new video, or choose to use an existing one that you select';
$string['videoupload'] = 'Vimeo video upload';
$string['videoupload_help'] = 'Add video file and click \'upload\'. The file will be uploaded from your computer to Vimeo with the information you entered on the previous form. The video will be viewable after Vimeo receives the file and has had time to process it.';
$string['vimeo:upload'] = 'Upload files to Vimeo';
$string['vimeo:viewall'] = 'View all files available';
$string['vimeofileaction'] = 'Vimeo file action';
Expand Down
2 changes: 2 additions & 0 deletions source/vimeo/templates/file_upload.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
}}
<form id="upload_resource_form" method="post" action=".">
<div class="container-fluid">
<h3>{{# str }} videoupload, mediatimesrc_vimeo {{/ str }}</h3>
<p>{{# str }} videoupload_help, mediatimesrc_vimeo {{/ str }}</p>
<input type="hidden" name="create" value="" >
<input type="hidden" name="source" value="vimeo" >
<input type="hidden" name="filesize" value="" >
Expand Down

0 comments on commit 6bcff93

Please sign in to comment.