Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

Commit

Permalink
improved new file upload
Browse files Browse the repository at this point in the history
  • Loading branch information
Theodor Truffer committed Feb 18, 2021
1 parent 7e8d126 commit 6864b70
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 10 deletions.
1 change: 1 addition & 0 deletions classes/class.ilOneDriveUploadGUI.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public function initUploadForm()
$this->form,
$this->getPluginHookObject(),
$DIC->ctrl()->getLinkTargetByClass(ilCloudPluginUploadGUI::class, self::CMD_ASYNC_GET_RESUMABLE_UPLOAD_URL, "", true, false),
"uploadFiles",
$lng->txt("cld_upload_files")
);
$file->setAfterUploadJsCallback('il.OneDriveList.afterUpload');
Expand Down
10 changes: 9 additions & 1 deletion src/Input/ChunkedFileUpload/html/tpl.chunked_upload.html
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
<input type="file" id="{ID}"/>
<div class="input-group">
<span class="input-group-btn">
<span class="btn btn-default btn-file">
{LABEL}
<input type="file" class="form-control-file" id="{ID}">
</span>
</span>
<input type="text" class="form-control" readonly="readonly" id="{ID}_filename"/>
</div>
27 changes: 21 additions & 6 deletions src/Input/ChunkedFileUpload/js/ChunkedUpload.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,35 @@
initChunkedUpload = (input_id, form_id, url_fetch_upload_url, chunk_size, after_upload_callback, url_upload_aborted, url_upload_failed) => {
const file_picker = document.getElementById(input_id);
initChunkedUpload = (
input_id,
form_id,
url_fetch_upload_url,
chunk_size,
after_upload_callback,
url_upload_aborted,
url_upload_failed, submit_cmd
) => {
let $submit_btn = $('input[name="cmd[' + submit_cmd + ']"]');
$submit_btn.prop("disabled", true);
let $input = $('#' + input_id);
let $filename_input = $('#' + input_id + '_filename');
$input.on('change', () => {
let file = $input.get(0).files[0];
$filename_input.val(file ? file.name : '');
$submit_btn.prop("disabled", !file);
});
$('#form_' + form_id).on('submit', (event) => {
let file = $input.get(0).files[0];
event.preventDefault();
il.waiter.show();
let file = file_picker.files[0];
this.file_in_progress = file;

$input.fileupload();
// fetch upload url
$.ajax({
type: 'post',
url: url_fetch_upload_url,
data: { filename: file.name }
}).success((response) => {
response = JSON.parse(response);
let $input = $('#' + input_id);
// callback functions
$input.on('fileuploaddone', (e, data) => {
il.waiter.hide();
Expand All @@ -35,7 +51,6 @@ initChunkedUpload = (input_id, form_id, url_fetch_upload_url, chunk_size, after_
});

// init chunked upload
$input.fileupload();
$input.fileupload('send', {
files: file,
url: response.uploadUrl,
Expand Down Expand Up @@ -73,7 +88,7 @@ function executeFunctionByName(functionName, context /*, args */) {
var args = Array.prototype.slice.call(arguments, 2);
var namespaces = functionName.split(".");
var func = namespaces.pop();
for(var i = 0; i < namespaces.length; i++) {
for (var i = 0; i < namespaces.length; i++) {
context = context[namespaces[i]];
}
return context[func].apply(context, args);
Expand Down
2 changes: 1 addition & 1 deletion src/Input/ChunkedFileUpload/js/ChunkedUpload.min.js

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

Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
class srChunkedDirectFileUploadInputGUI extends ilFormPropertyGUI
{
const DEFAULT_CHUNK_SIZE = 327680 * 20;
/**
* @var string
*/
protected $submit_cmd;
/**
* @var ilTemplate
*/
Expand Down Expand Up @@ -74,7 +78,7 @@ class srChunkedDirectFileUploadInputGUI extends ilFormPropertyGUI
*/
protected $dic;

public function __construct(ilPropertyFormGUI $ilPropertyFormGUI, ilPlugin $plugin, string $url_fetch_upload_url, $a_title = "")
public function __construct(ilPropertyFormGUI $ilPropertyFormGUI, ilPlugin $plugin, string $url_fetch_upload_url, string $submit_cmd, $a_title = "")
{
global $DIC;
$this->dic = $DIC;
Expand All @@ -84,6 +88,7 @@ public function __construct(ilPropertyFormGUI $ilPropertyFormGUI, ilPlugin $plug
$this->loadJavaScript($DIC->ui()->mainTemplate());
$this->tpl = new ilTemplate(__DIR__ . '/html/tpl.chunked_upload.html', true, true);
parent::__construct($a_title, "");
$this->submit_cmd = $submit_cmd;
}

public static function loadJavaScript(ilTemplate $a_tpl, bool $force = false)
Expand Down Expand Up @@ -111,12 +116,14 @@ protected function getOnloadCode(string $input_id) : string
'' . $this->chunk_size . ',' .
'"' . $this->getAfterUploadJsCallback() . '",' .
'"' . $this->getUploadAbortedUrl() . '",' .
'"' . $this->getUploadFailedUrl() . '")';
'"' . $this->getUploadFailedUrl() . '",' .
'"' . $this->submit_cmd . '")';
}

public function render()
{
$tmp_id = ilUtil::randomhash();
$this->tpl->setVariable('LABEL', $this->dic->language()->txt('select_file'));
$this->tpl->setVariable('ID', $tmp_id);
return $this->tpl->get() .
'<script type="text/javascript">' . $this->getOnloadCode($tmp_id) . '</script>';
Expand Down

0 comments on commit 6864b70

Please sign in to comment.