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

Commit

Permalink
WIP: chunked upload & event logging
Browse files Browse the repository at this point in the history
  • Loading branch information
Theodor Truffer committed Feb 10, 2021
1 parent ef771a2 commit fa7c29a
Show file tree
Hide file tree
Showing 81 changed files with 11,301 additions and 44 deletions.
22 changes: 22 additions & 0 deletions classes/Client/class.exodClientBusiness.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,28 @@ public function hasConnection()

}

/**
* @param $parent_id
* @param $name
* @return ResumableUploadUrlDTO
* @throws ilCloudException
*/
public function getResumableUploadUrl($parent_id, $name) : ResumableUploadUrlDTO
{
$this->setRequestType(self::REQ_TYPE_POST);
$this->setRessource($this->getExodApp()->getRessource() . '/drive/items/' . $parent_id
. ':/' . $name . ':/oneDrive.createUploadSession');
$this->setPostfields([
'item' => [
"@name.conflictBehavior" => "rename",
"name" => $name
]
]);
$this->setRequestContentType(exodCurl::JSON);
$response = $this->getResponseJsonDecoded();
return ResumableUploadUrlDTO::fromStdClass($response);
}

/**
* @param $folder_id
*
Expand Down
64 changes: 64 additions & 0 deletions classes/Util/ResumableUploadUrlDTO.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

/**
* Class ResumableUploadUrlDTO
* @author Theodor Truffer <[email protected]>
*/
class ResumableUploadUrlDTO
{
/**
* @var string
*/
protected $upload_url;
/**
* @var string
*/
protected $expiration_date_time;

/**
* ResumableUploadUrlDTO constructor.
* @param string $upload_url
* @param string $expiration_date_time
*/
public function __construct(string $upload_url, string $expiration_date_time)
{
$this->upload_url = $upload_url;
$this->expiration_date_time = $expiration_date_time;
}

public static function fromStdClass(stdClass $stdClass) : self
{
return new self($stdClass->uploadUrl, $stdClass->expirationDateTime);
}

public function toStdClass() : stdClass
{
$std_class = new stdClass();
$std_class->uploadUrl = $this->getUploadUrl();
$std_class->expirationDateTime = $this->getExpirationDateTime();
return $std_class;
}

public function toJson() : string
{
return json_encode($this->toStdClass());
}

/**
* @return string
*/
public function getUploadUrl() : string
{
return $this->upload_url;
}

/**
* @return string
*/
public function getExpirationDateTime() : string
{
return $this->expiration_date_time;
}


}
5 changes: 1 addition & 4 deletions classes/class.ilOneDrive.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
<?php
require_once('./Customizing/global/plugins/Modules/Cloud/CloudHook/OneDrive/classes/Auth/class.exodBearerToken.php');
require_once('./Modules/Cloud/classes/class.ilCloudPlugin.php');
require_once('./Customizing/global/plugins/Modules/Cloud/CloudHook/OneDrive/classes/App/class.exodAppBusiness.php');
require_once('./Customizing/global/plugins/Modules/Cloud/CloudHook/OneDrive/classes/App/class.exodTenant.php');
require_once('Customizing/global/plugins/Modules/Cloud/CloudHook/OneDrive/vendor/autoload.php');

/**
* Class ilOneDrive
Expand Down
11 changes: 6 additions & 5 deletions classes/class.ilOneDriveActionListGUI.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ class ilOneDriveActionListGUI extends ilCloudPluginActionListGUI {

const CMD_INIT_RENAME = 'initRename';
const CMD_RENAME = 'rename';
const CMD_OPEN_IN_OFFICE_ONLINE = 'openInOfficeOnline';

const CMD_OPEN_IN_OFFICE_ONLINE = 'openInOfficeOnline';
const GET_ID = 'id';
const POST_TITLE = 'title';

const POST_TITLE = 'title';
const ITEM_ID = 'item_id';

/**
/**
* @var ilAdvancedSelectionListGUI
*/
public $selection_list;
Expand Down Expand Up @@ -133,15 +133,15 @@ private function checkOpenInOfficePerm()
* @return ilOneDrive
*/
public function getPluginObject() {
return parent::getPluginObject(); // TODO: Change the autogenerated stub
return parent::getPluginObject();
}


/**
* @return ilOneDriveService
*/
public function getService() {
return parent::getService(); // TODO: Change the autogenerated stub
return parent::getService();
}


Expand Down Expand Up @@ -201,6 +201,7 @@ protected function rename()
}



/**
* @param string $title
*
Expand Down
4 changes: 1 addition & 3 deletions classes/class.ilOneDriveConfigGUI.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?php
require_once('./Customizing/global/plugins/Modules/Cloud/CloudHook/OneDrive/classes/App/class.exodApp.php');
require_once('./Modules/Cloud/classes/class.ilCloudPluginConfigGUI.php');

require_once('Customizing/global/plugins/Modules/Cloud/CloudHook/OneDrive/vendor/autoload.php');
/**
* Class ilOneDriveConfigGUI
*
Expand Down
11 changes: 7 additions & 4 deletions classes/class.ilOneDriveInitGUI.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php
require_once('./Modules/Cloud/classes/class.ilCloudPluginInitGUI.php');

use srag\Plugins\OneDrive\Input\srChunkedDirectFileUploadInputGUI;

require_once('Customizing/global/plugins/Modules/Cloud/CloudHook/OneDrive/vendor/autoload.php');
/**
* Class ilOneDriveInitGUI
*
Expand All @@ -16,12 +18,13 @@ public function beforeSetContent()
{
global $DIC;
$DIC->ui()->mainTemplate()->addJavaScript("./Customizing/global/plugins/Modules/Cloud/CloudHook/OneDrive/js/OneDriveList.js");
require_once 'Customizing/global/plugins/Modules/Cloud/CloudHook/OneDrive/classes/class.ilOneDriveActionListGUI.php';
$rename_url = $DIC->ctrl()->getLinkTargetByClass([ilObjCloudGUI::class, ilCloudPluginActionListGUI::class], ilOneDriveActionListGUI::CMD_INIT_RENAME);
srChunkedDirectFileUploadInputGUI::loadJavaScript($DIC->ui()->mainTemplate());
$rename_url = $DIC->ctrl()->getLinkTargetByClass([ilObjCloudGUI::class, ilCloudPluginActionListGUI::class], ilOneDriveActionListGUI::CMD_INIT_RENAME, "", false, false);
$after_upload_url = $DIC->ctrl()->getLinkTargetByClass([ilObjCloudGUI::class, ilOneDriveUploadGUI::class], ilOneDriveUploadGUI::CMD_AFTER_UPLOAD, "", false, false);
$this->tpl_file_tree->setVariable(
'PLUGIN_AFTER_CONTENT',
'<script type="text/javascript">' .
'il.OneDriveList = new OneDriveList("' . $rename_url . '");' .
'il.OneDriveList = new OneDriveList("' . $rename_url . '", "' . $after_upload_url . '");' .
'</script>'
);
}
Expand Down
6 changes: 1 addition & 5 deletions classes/class.ilOneDrivePlugin.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
<?php
require_once('./Modules/Cloud/classes/class.ilCloudHookPlugin.php');
require_once('./Customizing/global/plugins/Modules/Cloud/CloudHook/OneDrive/classes/class.exodConfig.php');
require_once('./Customizing/global/plugins/Modules/Cloud/CloudHook/OneDrive/classes/App/class.exodAppBusiness.php');
require_once('./Customizing/global/plugins/Modules/Cloud/CloudHook/OneDrive/classes/App/class.exodAppPublic.php');

require_once('Customizing/global/plugins/Modules/Cloud/CloudHook/OneDrive/vendor/autoload.php');
/**
* Class ilOneDrivePlugin
*
Expand Down
114 changes: 111 additions & 3 deletions classes/class.ilOneDriveUploadGUI.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<?php

use srag\Plugins\OneDrive\Input\srChunkedDirectFileUploadInputGUI;
use srag\Plugins\OneDrive\EventLog\EventLogger;

/**
* Class ilOneDriveUploadGUI
*
Expand All @@ -10,19 +13,124 @@
class ilOneDriveUploadGUI extends ilCloudPluginUploadGUI
{

const CMD_AFTER_UPLOAD = 'afterUpload';
const CMD_ASYNC_GET_RESUMABLE_UPLOAD_URL = 'asyncGetResumableUploadUrl';

public function asyncUploadFile()
{
global $DIC;
$ilTabs = $DIC['ilTabs'];

$ilTabs->activateTab("content");
$this->initUploadForm();
echo $this->form->getHTML();

$_SESSION["cld_folder_id"] = $_POST["folder_id"];

exit;
}

public function initUploadForm()
{
global $DIC;
$ilCtrl = $DIC['ilCtrl'];
parent::initUploadForm();
$lng = $DIC['lng'];

$this->form = new ilPropertyFormGUI();
$this->form->setId("upload");
$this->form->setMultipart(true);
$this->form->setHideLabels();

$file = new srChunkedDirectFileUploadInputGUI(
$this->form,
$this->getPluginHookObject(),
$DIC->ctrl()->getLinkTargetByClass(ilCloudPluginUploadGUI::class, self::CMD_ASYNC_GET_RESUMABLE_UPLOAD_URL, "", false, false),
$lng->txt("cld_upload_files")
);
$file->setAfterUploadJsCallback('il.OneDriveList.afterUpload');
$file->setRequired(true);
$this->form->addItem($file);

$this->form->addCommandButton("uploadFiles", $lng->txt("upload"));
$this->form->addCommandButton("cancelAll", $lng->txt("cancel"));

$this->form->setTableWidth("100%");
$this->form->setTitle($lng->txt("upload_files_title"));
// $this->form->setTitleIcon(ilUtil::getImagePath('icon_file.gif'), $lng->txt('obj_file'));
$this->form->setTitleIcon(ilUtil::getImagePath('icon_dcl_file.svg'), $lng->txt('obj_file'));

$this->form->setTitle($lng->txt("upload_files"));
$this->form->setFormAction($ilCtrl->getFormAction($this, "uploadFiles"));
$this->form->setTarget("cld_blank_target");
$this->form->setFormAction($ilCtrl->getFormAction(new ilCloudPluginUploadGUI($this), "uploadFiles"));
}


/**
* @param $file_upload
* @return stdClass
* @deprecated currently not used due to chunked/resumable upload
*/
function handleFileUpload($file_upload)
{
$_POST["title"] = exodFile::formatRename($_POST["title"], $file_upload["name"]);

return parent::handleFileUpload($file_upload);
}
}

protected function afterUpload()
{
// TODO: event logging
}

protected function asyncGetResumableUploadUrl()
{
global $DIC;
if (!$DIC->access()->checkAccess('upload', '', $_GET['ref_id']) ) {
echo $this->getJsonError(403, "Permission Denied.");
exit;
}
$name = filter_input(INPUT_POST, 'name', FILTER_SANITIZE_STRING);
$parent_id = $_SESSION["cld_folder_id"];
try {
$upload_url = $this->getService()->getClient()->getResumableUploadUrl($parent_id, $name);
} catch (ilCloudException $e) {
echo $this->getJsonError(500, $e->getMessage());
exit;
}
EventLogger::logUploadStarted(
$DIC->user()->getId(),
$name,
ilCloudFileTree::getFileTreeFromSession()->getNodeFromId($parent_id)->getPath()
);
http_response_code(200);
echo $upload_url->toJson();
exit;
}

protected function getJsonError(int $status_code, string $message) : string
{
http_response_code($status_code);
return json_encode([
"error" => [
"code" => $status_code,
"message" => $message,
],
]);
}

/**
* @return ilOneDrive
*/
public function getPluginObject() {
return parent::getPluginObject();
}


/**
* @return ilOneDriveService
*/
public function getService() {
return parent::getService();
}

}
14 changes: 12 additions & 2 deletions js/OneDriveList.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
function OneDriveList(url_rename) {
function OneDriveList(url_rename, url_after_upload) {
var url_rename = url_rename;

var url_after_upload = url_after_upload;

var clicked_rename = false;

//Ajax request to rename a file/folder
Expand Down Expand Up @@ -80,6 +82,14 @@ function OneDriveList(url_rename) {
display_message = false;
il.CloudFileList.hideProgressAnimation();
}
}

this.afterUpload = (file) => {
$.ajax({
type: 'POST',
url: url_after_upload,
data: { filename: file.name }
});
il.CloudFileList.afterUpload('chunked upload successfully completed');
}
}
}
2 changes: 1 addition & 1 deletion js/OneDriveList.min.js

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

1 change: 1 addition & 0 deletions node_modules/.bin/tmpl.js

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

20 changes: 20 additions & 0 deletions node_modules/blueimp-canvas-to-blob/LICENSE.txt

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

Loading

0 comments on commit fa7c29a

Please sign in to comment.