Skip to content
Browse files

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
  • Loading branch information...
1 parent 4631e39 commit 4c2fcbfc85490848269bec3361ece4b1f6d58a82 @marinaglancy marinaglancy committed
Showing with 14 additions and 5 deletions.
  1. +9 −5 lib/filestorage/file_storage.php
  2. +5 −0 question/import_form.php
View
14 lib/filestorage/file_storage.php
@@ -1028,7 +1028,7 @@ public function create_file_from_pathname($filerecord, $pathname) {
$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 @@ public function create_file_from_string($filerecord, $content) {
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 @@ public function import_external_file($storedfile) {
* 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
5 question/import_form.php
@@ -122,6 +122,11 @@ protected function validate_uploaded_file($data, $errors) {
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']);

0 comments on commit 4c2fcbf

Please sign in to comment.
Something went wrong with that request. Please try again.