MDL-59960 core_files: prevent mimetype update on every file update

Every time a file is updated, the mimetype is re-processed.
But, some mimetypes can only be detected if the file is present
on local disk.  With external file systems, this could cause
the file to be downloaded unnecessarily.  Instead, only update
the mimetype when the file content or the file name has changed.
This commit is contained in:
Mark Nielsen
2017-08-28 11:36:15 -07:00
parent ee8bce2d3b
commit b757c648f3
+9 -2
View File
@@ -138,6 +138,7 @@ class stored_file {
protected function update($dataobject) {
global $DB;
$updatereferencesneeded = false;
$updatemimetype = false;
$keys = array_keys((array)$this->file_record);
$filepreupdate = clone($this->file_record);
foreach ($dataobject as $field => $value) {
@@ -202,6 +203,10 @@ class stored_file {
$updatereferencesneeded = true;
}
if ($updatereferencesneeded || ($field === 'filename' && $this->file_record->filename != $value)) {
$updatemimetype = true;
}
// adding the field
$this->file_record->$field = $value;
} else {
@@ -209,8 +214,10 @@ class stored_file {
}
}
// Validate mimetype field
$mimetype = $this->filesystem->mimetype_from_storedfile($this);
$this->file_record->mimetype = $mimetype;
if ($updatemimetype) {
$mimetype = $this->filesystem->mimetype_from_storedfile($this);
$this->file_record->mimetype = $mimetype;
}
$DB->update_record('files', $this->file_record);
if ($updatereferencesneeded) {