MDL-58219 googledocs: Use google file extensions

Only rename on export for download.

Part of MDL-58220
This commit is contained in:
Damyon Wiese 2017-03-14 14:20:16 +08:00
parent 72643dc688
commit af28b22892
3 changed files with 34 additions and 8 deletions

View file

@ -100,6 +100,11 @@ abstract class core_filetypes {
'gallery' => array('type' => 'application/x-smarttech-notebook', 'icon' => 'archive'), 'gallery' => array('type' => 'application/x-smarttech-notebook', 'icon' => 'archive'),
'galleryitem' => array('type' => 'application/x-smarttech-notebook', 'icon' => 'archive'), 'galleryitem' => array('type' => 'application/x-smarttech-notebook', 'icon' => 'archive'),
'gallerycollection' => array('type' => 'application/x-smarttech-notebook', 'icon' => 'archive'), 'gallerycollection' => array('type' => 'application/x-smarttech-notebook', 'icon' => 'archive'),
'gdraw' => array('type' => 'application/vnd.google-apps.drawing', 'icon' => 'image', 'groups' => array('image')),
'gdoc' => array('type' => 'application/vnd.google-apps.document', 'icon' => 'document', 'groups' => array('document')),
'gsheet' => array('type' => 'application/vnd.google-apps.spreadsheet', 'icon' => 'spreadsheet',
'groups' => array('spreadsheet')),
'gslides' => array('type' => 'application/vnd.google-apps.presentation', 'icon' => 'powerpoint', 'groups' => array('presentation')),
'gif' => array('type' => 'image/gif', 'icon' => 'gif', 'groups' => array('image', 'web_image'), 'string' => 'image'), 'gif' => array('type' => 'image/gif', 'icon' => 'gif', 'groups' => array('image', 'web_image'), 'string' => 'image'),
'gtar' => array('type' => 'application/x-gtar', 'icon' => 'archive', 'gtar' => array('type' => 'application/x-gtar', 'icon' => 'archive',
'groups' => array('archive'), 'string' => 'archive'), 'groups' => array('archive'), 'string' => 'archive'),

View file

@ -331,7 +331,7 @@ class repository_googledocs extends repository {
switch ($type){ switch ($type){
case 'document': case 'document':
$ext = $config->documentformat; $ext = $config->documentformat;
$title = $gfile->name . '.'. $ext; $title = $gfile->name . '.gdoc';
if ($ext === 'rtf') { if ($ext === 'rtf') {
// Moodle user 'text/rtf' as the MIME type for RTF files. // Moodle user 'text/rtf' as the MIME type for RTF files.
// Google uses 'application/rtf' for the same type of file. // Google uses 'application/rtf' for the same type of file.
@ -343,12 +343,12 @@ class repository_googledocs extends repository {
break; break;
case 'presentation': case 'presentation':
$ext = $config->presentationformat; $ext = $config->presentationformat;
$title = $gfile->name . '.'. $ext; $title = $gfile->name . '.gslides';
$exporttype = $types[$ext]['type']; $exporttype = $types[$ext]['type'];
break; break;
case 'spreadsheet': case 'spreadsheet':
$ext = $config->spreadsheetformat; $ext = $config->spreadsheetformat;
$title = $gfile->name . '.'. $ext; $title = $gfile->name . '.gsheet';
$exporttype = $types[$ext]['type']; $exporttype = $types[$ext]['type'];
break; break;
case 'drawing': case 'drawing':
@ -421,6 +421,7 @@ class repository_googledocs extends repository {
$source = json_decode($reference); $source = json_decode($reference);
$newfilename = false;
if ($source->exportformat == 'download') { if ($source->exportformat == 'download') {
$params = ['alt' => 'media']; $params = ['alt' => 'media'];
$sourceurl = new moodle_url($base . '/files/' . $source->id, $params); $sourceurl = new moodle_url($base . '/files/' . $source->id, $params);
@ -428,20 +429,36 @@ class repository_googledocs extends repository {
} else { } else {
$params = ['mimeType' => $source->exportformat]; $params = ['mimeType' => $source->exportformat];
$sourceurl = new moodle_url($base . '/files/' . $source->id . '/export', $params); $sourceurl = new moodle_url($base . '/files/' . $source->id . '/export', $params);
$types = get_mimetypes_array();
$checktype = $source->exportformat;
if ($checktype == 'application/rtf') {
$checktype = 'text/rtf';
}
foreach ($types as $extension => $info) {
if ($info['type'] == $checktype) {
$newfilename = $source->name . '.' . $extension;
break;
}
}
$source = $sourceurl->out(false); $source = $sourceurl->out(false);
} }
// We use download_one and not the rest API because it has special timeouts etc. // We use download_one and not the rest API because it has special timeouts etc.
$path = $this->prepare_file($filename); $path = $this->prepare_file($filename);
$options = ['filepath' => $path, 'timeout' => 15, 'followlocation' => true, 'maxredirs' => 5]; $options = ['filepath' => $path, 'timeout' => 15, 'followlocation' => true, 'maxredirs' => 5];
$result = $client->download_one($source, null, $options); $success = $client->download_one($source, null, $options);
if ($result) { if ($success) {
@chmod($path, $CFG->filepermissions); @chmod($path, $CFG->filepermissions);
return array(
$result = [
'path' => $path, 'path' => $path,
'url' => $reference 'url' => $reference,
); ];
if (!empty($newfilename)) {
$result['newfilename'] = $newfilename;
}
return $result;
} }
throw new repository_exception('cannotdownload', 'repository'); throw new repository_exception('cannotdownload', 'repository');
} }

View file

@ -278,6 +278,10 @@ switch ($action) {
} else { } else {
// Download file to moodle. // Download file to moodle.
$downloadedfile = $repo->get_file($reference, $saveas_filename); $downloadedfile = $repo->get_file($reference, $saveas_filename);
if (!empty($downloadedfile['newfilename'])) {
$record->filename = $downloadedfile['newfilename'];
}
if (empty($downloadedfile['path'])) { if (empty($downloadedfile['path'])) {
$err->error = get_string('cannotdownload', 'repository'); $err->error = get_string('cannotdownload', 'repository');
die(json_encode($err)); die(json_encode($err));