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

Commit

Permalink
almost done: chunk upload and event logs
Browse files Browse the repository at this point in the history
  • Loading branch information
Theodor Truffer committed Feb 12, 2021
1 parent 9c5963a commit 7e8d126
Show file tree
Hide file tree
Showing 17 changed files with 323 additions and 78 deletions.
5 changes: 4 additions & 1 deletion classes/Client/class.exodClientBusiness.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ 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');
. ':/' . rawurlencode($name) . ':/oneDrive.createUploadSession');
$this->setPostfields([
'item' => [
"@name.conflictBehavior" => "rename",
Expand All @@ -51,6 +51,9 @@ public function getResumableUploadUrl($parent_id, $name) : ResumableUploadUrlDTO
]);
$this->setRequestContentType(exodCurl::JSON);
$response = $this->getResponseJsonDecoded();
if (is_null($response)) {
throw new ilCloudException('could not create upload session from OneDrive (empty response)');
}
return ResumableUploadUrlDTO::fromStdClass($response);
}

Expand Down
14 changes: 12 additions & 2 deletions classes/class.ilOneDriveDeleteGUI.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
/**
* Class ilOneDriveDeleteGUI
* @author Theodor Truffer <[email protected]>
*
* @ilCtrl_IsCalledBy ilOneDriveDeleteGUI : ilObjCloudGUI
*/
class ilOneDriveDeleteGUI extends ilCloudPluginDeleteGUI
{
Expand Down Expand Up @@ -35,8 +37,16 @@ public function deleteItem()
$response->message = $tpl->getMessageHTML($e->getMessage(), "failure");
}
}
echo "<script language='javascript' type='text/javascript'>window.parent.il.CloudFileList.afterDeleteItem(" . ilJsonUtil::encode($response)
echo "<script type='text/javascript'>window.parent.il.CloudFileList.afterDeleteItem(" . ilJsonUtil::encode($response)
. ");</script>";
exit; }
exit;
}

public function initDeleteItem()
{
global $DIC;
parent::initDeleteItem();
$this->gui->setFormAction($DIC->ctrl()->getFormActionByClass(ilCloudPluginDeleteGUI::class));
}

}
60 changes: 56 additions & 4 deletions classes/class.ilOneDriveUploadGUI.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,19 @@ class ilOneDriveUploadGUI extends ilCloudPluginUploadGUI

const CMD_AFTER_UPLOAD = 'afterUpload';
const CMD_ASYNC_GET_RESUMABLE_UPLOAD_URL = 'asyncGetResumableUploadUrl';
const CMD_UPLOAD_ABORTED = 'uploadAborted';
const CMD_UPLOAD_FAILED = 'uploadFailed';

/**
* onedrive forbids some special chars which will be removed here
*
* @param $name
* @return string
*/
protected function sanitizeFileName($name) : string
{
return str_replace(["/", "\\", "*", "<", ">", "?", ":", "|", "#", "%"], "-", $name);
}

public function asyncUploadFile()
{
Expand Down Expand Up @@ -44,10 +57,14 @@ public function initUploadForm()
$file = new srChunkedDirectFileUploadInputGUI(
$this->form,
$this->getPluginHookObject(),
$DIC->ctrl()->getLinkTargetByClass(ilCloudPluginUploadGUI::class, self::CMD_ASYNC_GET_RESUMABLE_UPLOAD_URL, "", false, false),
$DIC->ctrl()->getLinkTargetByClass(ilCloudPluginUploadGUI::class, self::CMD_ASYNC_GET_RESUMABLE_UPLOAD_URL, "", true, false),
$lng->txt("cld_upload_files")
);
$file->setAfterUploadJsCallback('il.OneDriveList.afterUpload');
$file->setUploadFailedUrl($DIC->ctrl()->getLinkTargetByClass(
ilCloudPluginUploadGUI::class, self::CMD_UPLOAD_FAILED, "", true, false));
$file->setUploadAbortedUrl($DIC->ctrl()->getLinkTargetByClass(
ilCloudPluginUploadGUI::class, self::CMD_UPLOAD_ABORTED, "", true, false));
$file->setRequired(true);
$this->form->addItem($file);

Expand Down Expand Up @@ -81,6 +98,7 @@ protected function afterUpload()
{
global $DIC;
$name = filter_input(INPUT_POST, 'filename', FILTER_SANITIZE_STRING);
$name = $this->sanitizeFileName($name);
$parent_id = $_SESSION["cld_folder_id"];
EventLogger::logUploadComplete(
$DIC->user()->getId(),
Expand All @@ -95,23 +113,57 @@ protected function asyncGetResumableUploadUrl()
echo $this->getJsonError(403, "Permission Denied.");
exit;
}
$name = filter_input(INPUT_POST, 'name', FILTER_SANITIZE_STRING);
$name = filter_input(INPUT_POST, 'filename', FILTER_SANITIZE_STRING);
$name_sanitized = $this->sanitizeFileName($name);
$parent_id = $_SESSION["cld_folder_id"];
$file_path = ilCloudFileTree::getFileTreeFromSession()->getNodeFromId($parent_id)->getPath() . '/' . $name_sanitized;
try {
$upload_url = $this->getService()->getClient()->getResumableUploadUrl($parent_id, $name);
$upload_url = $this->getService()->getClient()->getResumableUploadUrl($parent_id, $name_sanitized);
} catch (ilCloudException $e) {
EventLogger::logUploadFailed(
$DIC->user()->getId(),
$file_path,
$e->getMessage()
);
echo $this->getJsonError(500, $e->getMessage());
exit;
}
EventLogger::logUploadStarted(
$DIC->user()->getId(),
ilCloudFileTree::getFileTreeFromSession()->getNodeFromId($parent_id)->getPath() . '/' . $name
$file_path,
$name_sanitized === $name ? '' : $name
);
http_response_code(200);
echo $upload_url->toJson();
exit;
}

protected function uploadFailed()
{
global $DIC;
$name = filter_input(INPUT_POST, 'filename', FILTER_SANITIZE_STRING);
$message = filter_input(INPUT_POST, 'message', FILTER_SANITIZE_STRING);
$parent_id = $_SESSION["cld_folder_id"];
$file_path = ilCloudFileTree::getFileTreeFromSession()->getNodeFromId($parent_id)->getPath() . '/' . $name;
EventLogger::logUploadFailed(
$DIC->user()->getId(),
$file_path,
$message ?? ''
);
}

protected function uploadAborted()
{
global $DIC;
$name = filter_input(INPUT_POST, 'filename', FILTER_SANITIZE_STRING);
$parent_id = $_SESSION["cld_folder_id"];
$file_path = ilCloudFileTree::getFileTreeFromSession()->getNodeFromId($parent_id)->getPath() . '/' . $name;
EventLogger::logUploadAborted(
$DIC->user()->getId(),
$file_path
);
}

protected function getJsonError(int $status_code, string $message) : string
{
http_response_code($status_code);
Expand Down
2 changes: 2 additions & 0 deletions lang/ilias_de.lang
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ tenant_id#:#Tenant-ID
tenant_name#:#Tenant-Name
ssl_version#:#SSL-Version
settings_root_folder#:#Pfad
settings_subtab_general#:#Allgemein
settings_subtab_logs#:#Event-Logs
open_in_onedrive#:#Ordner in OneDrive öffnen
err_unsupported_chars#:#Der Name enthält Zeichen, welche nicht unterstützt werden. Bitte korrigieren Sie Ihre Eingabe. Die folgenden Zeichen werden nicht unterstützt:
form_title_rename#:#Objekt umbenennen
Expand Down
2 changes: 2 additions & 0 deletions lang/ilias_en.lang
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ tenant_id#:#Tenant-ID
tenant_name#:#Tenant-Name
ssl_version#:#SSL-Version
settings_root_folder#:#Path
settings_subtab_general#:#General
settings_subtab_logs#:#Event Logs
open_in_onedrive#:#Open folder in OneDrive
err_unsupported_chars#:#The name contains unsupported characters. Please correct the form input. The following characters are not supported:
form_title_rename#:#Rename Object
Expand Down
2 changes: 1 addition & 1 deletion src/EventLog/EventLogEntryAR.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class EventLogEntryAR extends ActiveRecord
* @con_has_field true
* @con_fieldtype text
* @con_is_notnull true
* @con_length 64
* @con_length 512
*/
protected $path;
/**
Expand Down
21 changes: 18 additions & 3 deletions src/EventLog/EventLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ class EventLogger
{
public static function logUploadStarted(
int $user_id,
string $file_path
string $file_path,
string $renamed_from = ''
) {
self::log(
$user_id,
EventType::uploadStarted(),
$file_path,
ObjectType::file(),
[]
$renamed_from ? ['renamed_from' => $renamed_from] : []
);
}

Expand Down Expand Up @@ -45,7 +46,21 @@ public static function logUploadAborted(
[]
);
}


public static function logUploadFailed(
int $user_id,
string $file_path,
string $message = ''
) {
self::log(
$user_id,
EventType::uploadFailed(),
$file_path,
ObjectType::file(),
$message ? ['error_msg' => $message] : []
);
}

public static function logObjectDeleted(
int $user_id,
string $object_path,
Expand Down
6 changes: 6 additions & 0 deletions src/EventLog/EventType.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ class EventType
const UPLOAD_STARTED = 'upload_started';
const UPLOAD_COMPLETE = 'upload_complete';
const UPLOAD_ABORTED = 'upload_aborted';
const UPLOAD_FAILED = 'upload_failed';
const OBJECT_DELETED = 'object_deleted';
const OBJECT_RENAMED = 'object_renamed';

protected static $types = [
self::UPLOAD_STARTED,
self::UPLOAD_COMPLETE,
self::UPLOAD_ABORTED,
self::UPLOAD_FAILED,
self::OBJECT_DELETED,
self::OBJECT_RENAMED
];
Expand Down Expand Up @@ -50,6 +52,10 @@ public static function uploadAborted() : self
{
return new self(self::UPLOAD_ABORTED);
}
public static function uploadFailed() : self
{
return new self(self::UPLOAD_FAILED);
}
public static function objectDeleted() : self
{
return new self(self::OBJECT_DELETED);
Expand Down
11 changes: 8 additions & 3 deletions src/EventLog/UI/EventLogTableUI.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use srag\Plugins\OneDrive\EventLog\EventLogEntryAR;
use ilTemplate;
use ILIAS\DI\Container;
use ilObjUser;

/**
* Class EventLogTableUI
Expand All @@ -29,9 +30,13 @@ public function render() : string
{
$tpl = new ilTemplate(__DIR__ . '/table/build/index.html', false, false);
$data = array_map(function($entry) {
return array_map(function($value) {
return $value[1];
}, $entry->getArrayForConnector());
$arrayForConnector = $entry->getArrayForConnector();
array_walk($arrayForConnector, function(&$value, $key) {
$value = $key === 'user_id' ?
ilObjUser::_lookupFullname($value[1]) . ' [' . ilObjUser::_lookupLogin($value[1]) . ']'
: $value[1];
});
return $arrayForConnector;
}, EventLogEntryAR::get());
return '<script type="application/javascript">' .
'window.exod_log_data = ' . json_encode(array_values($data)) . ';' .
Expand Down
78 changes: 49 additions & 29 deletions src/EventLog/UI/table/src/Columns.tsx
Original file line number Diff line number Diff line change
@@ -1,30 +1,50 @@
const columns: {
dataIndex: string,
key: string
}[] = [
{
dataIndex: "id",
key: "id"
},
{
dataIndex: "timestamp",
key: "timestamp"
},
{
dataIndex: "event_type",
key: "event_type"
},
{
dataIndex: "path",
key: "path"
},
{
dataIndex: "object_type",
key: "object_type"
},
{
dataIndex: "additional_data",
key: "additional_data"
}
];
const columns = (translate: ((a: string) => string)) : any => {
return [
{
dataIndex: "timestamp",
key: "timestamp",
sorter: (a: any, b: any) => Date.parse(a.timestamp) - Date.parse(b.timestamp),
defaultSortOrder: 'descend',
title: translate('column.timestamp')
},
{
dataIndex: "user_id",
key: "user",
title: translate('column.user')
},
{
dataIndex: "event_type_translated",
key: "event_type",
title: translate('column.event_type'),
filters: [
{ text: translate("event_type.upload_started"), value: 'upload_started' },
{ text: translate("event_type.upload_complete"), value: 'upload_complete' },
{ text: translate("event_type.upload_aborted"), value: 'upload_aborted' },
{ text: translate("event_type.upload_failed"), value: 'upload_failed' },
{ text: translate("event_type.object_deleted"), value: 'object_deleted' },
{ text: translate("event_type.object_renamed"), value: 'object_renamed' },
],
onFilter: (value: string, record: any) => {
console.log('value: ' + value);
console.log(record);
return record.event_type === value;
}
},
{
dataIndex: "path",
key: "path",
title: translate('column.path')
},
{
dataIndex: "object_type_translated",
key: "object_type",
title: translate('column.object_type')
},
{
dataIndex: "additional_data_translated",
key: "additional_data",
title: translate('column.additional_data')
}
];
};
export default columns;
22 changes: 13 additions & 9 deletions src/EventLog/UI/table/src/DataTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,25 @@ class DataTable extends React.Component<any, any> {
let data: any = window.exod_log_data;
this.state = {
data: data.map((set: any) => {
set.event_type = this.props.t("event_type." + set.event_type);
set.object_type = this.props.t("object_type." + set.object_type);
set.event_type_translated = this.props.t("event_type." + set.event_type);
set.object_type_translated = this.props.t("object_type." + set.object_type);
set.additional_data_translated = this.formatAdditionalData(set.additional_data);
return set;
})
}
}

render() {
return (<Table dataSource={this.state.data} columns={columns.map((val) => {
return {
dataIndex: val.dataIndex,
key: val.key,
title: this.props.t("column." + val.key)
}
})}/>);
return (<Table dataSource={this.state.data} columns={columns(this.props.t)}/>);
}

formatAdditionalData(additional_data: string) {
let parsed = JSON.parse(additional_data);
let string = '';
Object.keys(parsed).map((key: string) => {
string += this.props.t("additional_data." + key) + ": " + parsed[key]
});
return string;
}
}

Expand Down
Loading

0 comments on commit 7e8d126

Please sign in to comment.