Skip to content

Commit

Permalink
MDL-33082 Detecting mimetype based on content bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
Dongsheng Cai authored and marinaglancy committed May 21, 2012
1 parent b456271 commit 8177b7b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
28 changes: 24 additions & 4 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) ? mimeinfo('type', $filerecord->filename) : $filerecord->mimetype;
$newrecord->mimetype = empty($filerecord->mimetype) ? $this->mimetype($pathname) : $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 @@ -1145,14 +1145,16 @@ public function create_file_from_string($filerecord, $content) {

$newrecord->timecreated = $filerecord->timecreated;
$newrecord->timemodified = $filerecord->timemodified;
$newrecord->mimetype = empty($filerecord->mimetype) ? mimeinfo('type', $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;
$newrecord->license = empty($filerecord->license) ? null : $filerecord->license;
$newrecord->sortorder = $filerecord->sortorder;

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->pathnamehash = $this->get_pathname_hash($newrecord->contextid, $newrecord->component, $newrecord->filearea, $newrecord->itemid, $newrecord->filepath, $newrecord->filename);

Expand Down Expand Up @@ -1216,7 +1218,7 @@ public function create_file_from_reference($filerecord, $repositoryid, $referenc
$filerecord->referencefileid = empty($filerecord->referencefileid) ? 0 : $filerecord->referencefileid;
$filerecord->referencelastsync = empty($filerecord->referencelastsync) ? 0 : $filerecord->referencelastsync;
$filerecord->referencelifetime = empty($filerecord->referencelifetime) ? 0 : $filerecord->referencelifetime;
$filerecord->mimetype = empty($filerecord->mimetype) ? mimeinfo('type', $filerecord->filename) : $filerecord->mimetype;
$filerecord->mimetype = empty($filerecord->mimetype) ? $this->mimetype($filerecord->filename) : $filerecord->mimetype;
$filerecord->userid = empty($filerecord->userid) ? null : $filerecord->userid;
$filerecord->source = empty($filerecord->source) ? null : $filerecord->source;
$filerecord->author = empty($filerecord->author) ? null : $filerecord->author;
Expand Down Expand Up @@ -1333,7 +1335,7 @@ public function convert_image($filerecord, $fid, $newwidth = null, $newheight =
}

if (!isset($filerecord['mimetype'])) {
$filerecord['mimetype'] = mimeinfo('type', $filerecord['filename']);
$filerecord['mimetype'] = $imageinfo['mimetype'];
}

$width = $imageinfo['width'];
Expand Down Expand Up @@ -1789,6 +1791,24 @@ public function import_external_file($storedfile) {
return $storedfile;
}

/**
* Return mimetype by given file pathname
*
* This method uses fileinfo module to get mimetype using magic bytes if file exists.
* If not, it will get mimetype based on filename
*
* @param string $pathname
* @return string
*/
public static function mimetype($pathname) {
if (file_exists($pathname)) {
$finfo = new finfo(FILEINFO_MIME_TYPE);
return $finfo->file($pathname);
} else {
return mimeinfo('type', $pathname);
}
}

/**
* Cron cleanup job.
*/
Expand Down
11 changes: 11 additions & 0 deletions lib/filestorage/stored_file.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,17 @@ protected function update($dataobject) {
throw new coding_exception("Invalid field name, $field doesn't exist in file record");
}
}
// Validate mimetype field
$pathname = $this->get_content_file_location();
// try to recover the content from trash
if (!is_readable($pathname)) {
if (!$this->fs->try_content_recovery($this) or !is_readable($pathname)) {
throw new file_exception('storedfilecannotread', '', $pathname);
}
}
$mimetype = $this->fs->mimetype($pathname);
$this->file_record->mimetype = $mimetype;

$DB->update_record('files', $this->file_record);
}

Expand Down

0 comments on commit 8177b7b

Please sign in to comment.