Commit c7e99a9f authored by Richard Mansfield's avatar Richard Mansfield
Browse files

Ensure filetype is set when creating new file artefacts (bug #914574)



When a new file artefact is created from a file that wasn't uploaded
by a browser, the filetype property is not always set.  This causes
warnings when extracting files from archives.  Moving the call to
file_mime_type right before construction of the new artefact type
fixes the problem and allows us to remove some other calls to
file_mime_type during import and upload.

Change-Id: I8e644bfc1e6bc0fbaa90f1e6637288997b0d028c
Signed-off-by: default avatarRichard Mansfield <richard.mansfield@catalyst.net.nz>
parent 52fe3992
......@@ -241,7 +241,6 @@ class LeapImportBlog extends LeapImportArtefactPlugin {
$data = (object)array(
'title' => (string)$blogpostentry->title . ' ' . get_string('attachment', 'artefact.blog'),
'owner' => $importer->get('usr'),
'filetype' => file_mime_type($pathname),
);
return ArtefactTypeFile::save_file($pathname, $data, $importer->get('usrobj'), true);
}
......
......@@ -845,6 +845,19 @@ class ArtefactTypeFile extends ArtefactTypeFileBase {
$data->height = $imageinfo[1];
return new ArtefactTypeImage(0, $data);
}
// If $data->filetype is set here, it's probably the claim made by the
// browser during upload or by a Leap2a file. Generally we believe this
// claim, because it gives better results when serving the file on
// download. If there's no claimed mimetype, use file_mime_type to make
// a guess, and give each file artefact type access to both the claimed
// and guessed mimetypes.
$data->guess = file_mime_type($path);
if (empty($data->filetype) || $data->filetype == 'application/octet-stream') {
$data->filetype = $data->guess;
}
if ($video = ArtefactTypeVideo::new_video($data)) {
return $video;
}
......@@ -880,11 +893,6 @@ class ArtefactTypeFile extends ArtefactTypeFileBase {
$f = self::new_file($pathname, $data);
$f->set('size', $size);
// sometimes the filetype is actually set, assume it is correct
if (empty($data->filetype)) {
$f->set('filetype', file_mime_type($pathname));
}
// if an extension has been provided (only from self::extract() at this stage), use it
if (!empty($data->extension)) {
$f->set('oldextension', $data->extension);
......@@ -973,16 +981,7 @@ class ArtefactTypeFile extends ArtefactTypeFileBase {
}
}
$data->size = $size;
if ($filetype == 'application/octet-stream') {
// the browser wasn't sure, so use file_mime_type to guess
require_once('file.php');
$data->filetype = file_mime_type($tmpname);
}
else {
$data->filetype = $filetype;
}
$data->filetype = $filetype;
$data->oldextension = $um->original_filename_extension();
$f = self::new_file($tmpname, $data);
$f->commit();
......
......@@ -121,11 +121,6 @@ class PluginImportFile extends PluginImport {
'container' => 0,
);
if ($imagesize = getimagesize($uzd . $f->actualfilename)) {
$mime = $imagesize['mime'];
$data->filetype = $mime;
}
$id = ArtefactTypeFile::save_file(
$uzd . $f->actualfilename,
$data,
......
......@@ -1425,7 +1425,6 @@ class PluginImportLeap extends PluginImport {
$data = (object)array(
'title' => (string)$entry->title . ' ' . get_string('attachment'),
'owner' => $this->get('usr'),
'filetype' => file_mime_type($pathname),
);
return ArtefactTypeFile::save_file($pathname, $data, $this->get('usrobj'), true);
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment