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'),
'galleryitem' => 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'),
'gtar' => array('type' => 'application/x-gtar', 'icon' => 'archive',
'groups' => array('archive'), 'string' => 'archive'),

View file

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

View file

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