Skip to content

Commit

Permalink
MDL-74424 repository_googledocs: Respect default import formats
Browse files Browse the repository at this point in the history
Makes sure that the correct file extension is attributed when importing
a file using the Google Drive repository based on the set configuration
values for the default import format settings.
  • Loading branch information
Mihail Geshoski committed Apr 5, 2022
1 parent ad6dc71 commit 63be4ed
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
6 changes: 6 additions & 0 deletions repository/googledocs/classes/local/node/file_node.php
Expand Up @@ -49,6 +49,9 @@ class file_node implements node {
/** @var string The thumbnail of the file. */
private $thumbnail;

/** @var string|null The type of the Google Doc file (document, presentation, etc.), null if it is a regular file. */
private $googledoctype;

/**
* Constructor.
*
Expand All @@ -72,6 +75,8 @@ public function __construct(\stdClass $gdfile) {
// in displaying 16px file thumbnails in the repository. To avoid this, the link can be slightly modified in
// order to get a larger version of the icon as there isn't an option to request this through the API call.
$this->thumbnail = !empty($gdfile->iconLink) ? str_replace('/16/', '/64/', $gdfile->iconLink) : '';
$this->googledoctype = !isset($gdfile->fileExtension) ?
str_replace('application/vnd.google-apps.', '', $gdfile->mimeType) : null;
}

/**
Expand All @@ -97,6 +102,7 @@ public function create_node_array(): ?array {
'name' => $this->name,
'link' => $this->link,
'exportformat' => $this->exportformat,
'googledoctype' => $this->googledoctype,
]
),
'date' => $this->modified,
Expand Down
20 changes: 19 additions & 1 deletion repository/googledocs/lib.php
Expand Up @@ -528,8 +528,26 @@ public function get_file($reference, $filename = '') {
if ($checktype == 'application/rtf') {
$checktype = 'text/rtf';
}
// Determine the relevant default import format config for the given file.
switch ($source->googledoctype) {
case 'document':
$importformatconfig = get_config('googledocs', 'documentformat');
break;
case 'presentation':
$importformatconfig = get_config('googledocs', 'presentationformat');
break;
case 'spreadsheet':
$importformatconfig = get_config('googledocs', 'spreadsheetformat');
break;
case 'drawing':
$importformatconfig = get_config('googledocs', 'drawingformat');
break;
default:
$importformatconfig = null;
}

foreach ($types as $extension => $info) {
if ($info['type'] == $checktype) {
if ($info['type'] == $checktype && $extension === $importformatconfig) {
$newfilename = $source->name . '.' . $extension;
break;
}
Expand Down

0 comments on commit 63be4ed

Please sign in to comment.