From b757c648f36e308011ca32f7e2f177a361c5c1f9 Mon Sep 17 00:00:00 2001 From: Mark Nielsen Date: Mon, 28 Aug 2017 10:30:42 -0700 Subject: [PATCH] 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. --- lib/filestorage/stored_file.php | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/filestorage/stored_file.php b/lib/filestorage/stored_file.php index 1331246c9a6..36f186f918d 100644 --- a/lib/filestorage/stored_file.php +++ b/lib/filestorage/stored_file.php @@ -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) {