Skip to content

Commit

Permalink
Allow UDOIT to accept modern filetypes (#900)
Browse files Browse the repository at this point in the history
* Allow UDOIT to accept modern filetypes

* Add apiDelete function

* Added spanish translation for filetype error

* Minor spelling fix to spanish translation addition

---------

Co-authored-by: Daniel Molares <[email protected]>
  • Loading branch information
taheralfayad and dmols authored Apr 18, 2024
1 parent 5867bb2 commit e04f201
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 10 deletions.
46 changes: 44 additions & 2 deletions assets/js/Components/FilesModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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 (
Expand Down Expand Up @@ -121,7 +163,7 @@ class FilesModal extends React.Component {
<Text display="block" weight="bold">{this.props.t('label.replace')}</Text>
<Text as="p">{this.props.t('label.replace.desc')}</Text>
<FileDrop
accept={activeFile.fileType}
accept={activeFile.acceptType}
onDropAccepted={this.handleDropAccept}
onDropRejected={this.handleDropReject}
renderLabel={
Expand Down
2 changes: 1 addition & 1 deletion assets/js/Components/FilesPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -328,4 +328,4 @@ class FilesPage extends React.Component {
}
}

export default FilesPage;
export default FilesPage;
35 changes: 33 additions & 2 deletions src/Lms/Canvas/CanvasApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,21 +106,24 @@ 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();

// 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'];
Expand Down Expand Up @@ -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;

}

}
4 changes: 2 additions & 2 deletions src/Lms/Canvas/CanvasLms.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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'])) {
Expand Down
2 changes: 1 addition & 1 deletion src/Lms/D2l/D2lLms.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Lms/LmsInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/Services/LmsPostService.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,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)
Expand Down
1 change: 1 addition & 0 deletions translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@
"msg.new_content": "Course scan complete. New content has been added.",
"msg.sync.started": "Course scan started.",
"msg.sync.failed": "Course scan failed. Course is missing.",
"msg.file.replace.file_type": "File type not accepted. Please input a file with the correct filetype.",
"msg.sync.completed": "Course scan completed.",
"msg.sync.course_inactive": "Course scan failed. Course is inactive.",

Expand Down
1 change: 1 addition & 0 deletions translations/es.json
Original file line number Diff line number Diff line change
Expand Up @@ -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 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.",

Expand Down

0 comments on commit e04f201

Please sign in to comment.