Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow UDOIT to accept modern filetypes #900

Merged
merged 6 commits into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading