Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

MDL-33082 Detecting mimetype based on content bytes

  • Loading branch information...
commit 8177b7b9f05878cfac92740869d3ea076320e0db 1 parent b456271
@dcai dcai authored marinaglancy committed
Showing with 35 additions and 4 deletions.
  1. +24 −4 lib/filestorage/file_storage.php
  2. +11 −0 lib/filestorage/stored_file.php
View
28 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) ? 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;
@@ -1145,7 +1145,6 @@ 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;
@@ -1153,6 +1152,9 @@ public function create_file_from_string($filerecord, $content) {
$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);
@@ -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;
@@ -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'];
@@ -1790,6 +1792,24 @@ public function import_external_file($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.
*/
public function cron() {
View
11 lib/filestorage/stored_file.php
@@ -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);
}
Please sign in to comment.
Something went wrong with that request. Please try again.