MDL-33338 recognising the mimetype for uploaded files

- when file is uploaded in php, the tmpname does not have initial extension and mimetype recognition by extension fails
- another issue is that import form for questions fails with fatal error when it should just say that field is required
This commit is contained in:
Marina Glancy 2012-05-28 15:19:41 +08:00
parent 4631e39533
commit 4c2fcbfc85
2 changed files with 14 additions and 5 deletions

View File

@ -1028,7 +1028,7 @@ class file_storage {
$newrecord->timecreated = $filerecord->timecreated;
$newrecord->timemodified = $filerecord->timemodified;
$newrecord->mimetype = empty($filerecord->mimetype) ? $this->mimetype($pathname) : $filerecord->mimetype;
$newrecord->mimetype = empty($filerecord->mimetype) ? $this->mimetype($pathname, $filerecord->filename) : $filerecord->mimetype;
$newrecord->userid = empty($filerecord->userid) ? null : $filerecord->userid;
$newrecord->source = empty($filerecord->source) ? null : $filerecord->source;
$newrecord->author = empty($filerecord->author) ? null : $filerecord->author;
@ -1154,7 +1154,7 @@ class file_storage {
list($newrecord->contenthash, $newrecord->filesize, $newfile) = $this->add_string_to_pool($content);
$filepathname = $this->path_from_hash($newrecord->contenthash) . '/' . $newrecord->contenthash;
// get mimetype by magic bytes
$newrecord->mimetype = empty($filerecord->mimetype) ? $this->mimetype($filepathname) : $filerecord->mimetype;
$newrecord->mimetype = empty($filerecord->mimetype) ? $this->mimetype($filepathname, $filerecord->filename) : $filerecord->mimetype;
$newrecord->pathnamehash = $this->get_pathname_hash($newrecord->contextid, $newrecord->component, $newrecord->filearea, $newrecord->itemid, $newrecord->filepath, $newrecord->filename);
@ -1801,11 +1801,15 @@ class file_storage {
* If file has a known extension, we return the mimetype based on extension.
* Otherwise (when possible) we try to get the mimetype from file contents.
*
* @param string $pathname
* @param string $pathname full path to the file
* @param string $filename correct file name with extension, if omitted will be taken from $path
* @return string
*/
public static function mimetype($pathname) {
$type = mimeinfo('type', $pathname);
public static function mimetype($pathname, $filename = null) {
if (empty($filename)) {
$filename = $pathname;
}
$type = mimeinfo('type', $filename);
if ($type === 'document/unknown' && class_exists('finfo') && file_exists($pathname)) {
$finfo = new finfo(FILEINFO_MIME_TYPE);
$type = mimeinfo_from_type('type', $finfo->file($pathname));

View File

@ -122,6 +122,11 @@ class question_import_form extends moodleform {
return $errors;
}
if (empty($data['format'])) {
$errors['format'] = get_string('required');
return $errors;
}
$formatfile = 'format/' . $data['format'] . '/format.php';
if (!is_readable($formatfile)) {
throw new moodle_exception('formatnotfound', 'question', '', $data['format']);