From a146a9b8d3d3844321b1bfd66201b11968f0166b Mon Sep 17 00:00:00 2001 From: Ahmad Altaher Alfayad Date: Fri, 31 Mar 2023 13:48:42 -0400 Subject: [PATCH 1/4] Allow UDOIT to accept modern filetypes --- assets/js/Components/FilesModal.js | 46 ++++++++++++++++++++++++++++-- assets/js/Components/FilesPage.js | 2 +- translations/en.json | 1 + 3 files changed, 46 insertions(+), 3 deletions(-) diff --git a/assets/js/Components/FilesModal.js b/assets/js/Components/FilesModal.js index 993e24afb..aa7e04ae5 100644 --- a/assets/js/Components/FilesModal.js +++ b/assets/js/Components/FilesModal.js @@ -36,6 +36,7 @@ class FilesModal extends React.Component { this.handleDropAccept = this.handleDropAccept.bind(this) this.handleDropReject = this.handleDropReject.bind(this) this.handleFilePost = this.handleFilePost.bind(this) + this.setAcceptType = this.setAcceptType.bind(this) } componentDidUpdate(prevProps, prevState) { @@ -59,6 +60,46 @@ class FilesModal extends React.Component { return -1; } + setAcceptType(file) { + let accept = [] + + switch(file.fileType) { + case "doc": + accept = ["doc", "docx"] + break + + case "ppt": + accept = ["ppt", "pptx"] + break + + case "xls": + accept = ["xls", "xlsx"] + break + + default: + accept = fileType + break + } + + let extension = file.fileName.slice(-4) + + switch(extension) { + case "xlsx": + accept = "xlsx" + break + + case "pptx": + accept = "pptx" + break + + case "docx": + accept = "docx" + break + } + + return accept + } + // Handler for the previous and next buttons on the modal // Will wrap around if the index goes out of bounds handleFileChange(newIndex) { @@ -80,7 +121,8 @@ class FilesModal extends React.Component { } render() { - const { activeFile } = this.props + let { activeFile } = this.props + activeFile.acceptType = this.setAcceptType(activeFile) let activeIndex = this.findActiveIndex() return ( @@ -121,7 +163,7 @@ class FilesModal extends React.Component { {this.props.t('label.replace')} {this.props.t('label.replace.desc')} Date: Fri, 4 Aug 2023 13:06:48 -0400 Subject: [PATCH 2/4] Add apiDelete function --- src/Lms/Canvas/CanvasApi.php | 35 +++++++++++++++++++++++++++++++-- src/Lms/Canvas/CanvasLms.php | 4 ++-- src/Lms/D2l/D2lLms.php | 2 +- src/Lms/LmsInterface.php | 2 +- src/Services/LmsPostService.php | 2 +- 5 files changed, 38 insertions(+), 7 deletions(-) diff --git a/src/Lms/Canvas/CanvasApi.php b/src/Lms/Canvas/CanvasApi.php index 845c6601c..7415b049e 100644 --- a/src/Lms/Canvas/CanvasApi.php +++ b/src/Lms/Canvas/CanvasApi.php @@ -106,7 +106,7 @@ public function apiPost($url, $options, $sendAuthorized = true) } // Posts a file to Canvas - public function apiFilePost(string $url, array $options, string $filepath): LmsResponse + public function apiFilePost(string $url, array $options, string $filepath, string $newFileName): LmsResponse { $fileResponse = $this->apiGet($url); $file = $fileResponse->getContent(); @@ -114,13 +114,16 @@ public function apiFilePost(string $url, array $options, string $filepath): LmsR // TODO: handle failed call $endpointOptions = [ - 'name' => urldecode($file['filename']), + 'name' => $newFileName, 'parent_folder_id' => $file['folder_id'], + 'content_type' => $file['content-type'], ]; $endpointResponse = $this->apiPost($options['postUrl'], ['query' => $endpointOptions], true); $endpointContent = $endpointResponse->getContent(); + $this->apiDelete($url); + // TODO: handle failed call $formFields = $endpointContent['upload_params']; @@ -158,4 +161,32 @@ public function apiPut($url, $options) return $lmsResponse; } + public function apiDelete($url) { + $lmsResponse = new LmsResponse(); + + if (strpos($url, 'https://') === false) { + $pattern = '/\/files\/\d+/'; + + preg_match($pattern, $url, $matches); + + $url = "https://" . $this->baseUrl . "/api/v1/" . $matches[0]; + } + + $response = $this->httpClient->request('DELETE', $url); + + $lmsResponse->setResponse($response); + + $content = $lmsResponse->getContent(); + if (!empty($content['errors'])) { + // TODO: If error is invalid token, refresh API token and try again + + foreach ($content['errors'] as $error) { + $lmsResponse->setError($error['message']); + } + } + + return $lmsResponse; + + } + } diff --git a/src/Lms/Canvas/CanvasLms.php b/src/Lms/Canvas/CanvasLms.php index ec30a2fc9..28ca0dbeb 100644 --- a/src/Lms/Canvas/CanvasLms.php +++ b/src/Lms/Canvas/CanvasLms.php @@ -370,7 +370,7 @@ public function postContentItem(ContentItem $contentItem) return $canvasApi->apiPut($url, ['body' => $options]); } - public function postFileItem(FileItem $file) + public function postFileItem(FileItem $file, string $newFileName) { $user = $this->security->getUser(); $apiDomain = $this->getApiDomain($user); @@ -382,7 +382,7 @@ public function postFileItem(FileItem $file) 'postUrl' => "courses/{$file->getCourse()->getLmsCourseId()}/files" ]; - $fileResponse = $canvasApi->apiFilePost($url, $options, $filepath); + $fileResponse = $canvasApi->apiFilePost($url, $options, $filepath, $newFileName); $fileObj = $fileResponse->getContent(); if (isset($fileObj['id'])) { diff --git a/src/Lms/D2l/D2lLms.php b/src/Lms/D2l/D2lLms.php index 1b403781f..055ff5229 100644 --- a/src/Lms/D2l/D2lLms.php +++ b/src/Lms/D2l/D2lLms.php @@ -381,7 +381,7 @@ public function updateFileItem(Course $course, $file) $this->entityManager->flush(); } - public function postFileItem(FileItem $file) + public function postFileItem(FileItem $file, string $newFileName) { return true; } diff --git a/src/Lms/LmsInterface.php b/src/Lms/LmsInterface.php index c155114db..acc6823ed 100644 --- a/src/Lms/LmsInterface.php +++ b/src/Lms/LmsInterface.php @@ -17,7 +17,7 @@ public function updateCourseData(Course $course, User $user); public function updateFileItem(Course $course, $file); public function updateContentItem(ContentItem $contentItem); public function postContentItem(ContentItem $contentItem); - public function postFileItem(FileItem $file); + public function postFileItem(FileItem $file, string $newFileName); public function getOauthUri(Institution $institution, UserSession $session); public function getAccountData(User $user, $accountId); public function getCourseUrl(Course $course, User $user); diff --git a/src/Services/LmsPostService.php b/src/Services/LmsPostService.php index 3613a90e1..20b98b487 100644 --- a/src/Services/LmsPostService.php +++ b/src/Services/LmsPostService.php @@ -81,7 +81,7 @@ public function saveFileToLms(FileItem $file, UploadedFile $uploadedFile, User $ return; } - return $lms->postFileItem($file); + return $lms->postFileItem($file, $uploadedFile->getClientOriginalName()); } public function replaceContent(Issue $issue, ContentItem $contentItem) From 47d3bd552ca94a410a43cafba0fac138332fda2a Mon Sep 17 00:00:00 2001 From: Daniel Molares Date: Thu, 18 Apr 2024 16:53:16 -0400 Subject: [PATCH 3/4] Added spanish translation for filetype error --- translations/es.json | 1 + 1 file changed, 1 insertion(+) diff --git a/translations/es.json b/translations/es.json index 6e8b3ed84..df847a236 100644 --- a/translations/es.json +++ b/translations/es.json @@ -131,6 +131,7 @@ "msg.new_content": "Escaneo del curso completo. Se ha agregado nuevo contenido.", "msg.sync.started": "Se inició el escaneo del curso.", "msg.sync.failed": "Falló el escaneo del curso. Falta el curso.", + "msg.file.replace.file_type": "El tipo de archivo no es valido. Por favor aporte un archivo de tipo correcto.", "msg.sync.completed": "Escaneo del curso completado.", "msg.sync.course_inactive": "Falló el escaneo del curso. El curso está inactivo.", From c96362404a58c9ebd32b104d283fab5ba04a9311 Mon Sep 17 00:00:00 2001 From: Daniel Molares Date: Thu, 18 Apr 2024 17:08:47 -0400 Subject: [PATCH 4/4] Minor spelling fix to spanish translation addition --- translations/es.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/translations/es.json b/translations/es.json index df847a236..38f4863cd 100644 --- a/translations/es.json +++ b/translations/es.json @@ -131,7 +131,7 @@ "msg.new_content": "Escaneo del curso completo. Se ha agregado nuevo contenido.", "msg.sync.started": "Se inició el escaneo del curso.", "msg.sync.failed": "Falló el escaneo del curso. Falta el curso.", - "msg.file.replace.file_type": "El tipo de archivo no es valido. Por favor aporte un archivo de tipo correcto.", + "msg.file.replace.file_type": "El tipo de archivo no es válido. Por favor aporte un archivo de tipo correcto.", "msg.sync.completed": "Escaneo del curso completado.", "msg.sync.course_inactive": "Falló el escaneo del curso. El curso está inactivo.",