Skip to content

Commit

Permalink
MDL-33338 recognising the mimetype for uploaded files
Browse files Browse the repository at this point in the history
- 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
marinaglancy committed May 28, 2012
1 parent 4631e39 commit 4c2fcbf
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
14 changes: 9 additions & 5 deletions lib/filestorage/file_storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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));
Expand Down
5 changes: 5 additions & 0 deletions question/import_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Expand Down

0 comments on commit 4c2fcbf

Please sign in to comment.