Skip to content

Commit

Permalink
Merge branch 'MDL-59960_mimetype' of https://github.com/mrmark/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
danpoltawski committed Sep 19, 2017
2 parents f5cc5b3 + b757c64 commit a1b8b9c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 28 deletions.
18 changes: 3 additions & 15 deletions lib/filestorage/file_system.php
Original file line number Diff line number Diff line change
Expand Up @@ -568,14 +568,13 @@ protected static function get_file_handle_for_path($path, $type = stored_file::F
* @return string The MIME type.
*/
public function mimetype_from_hash($contenthash, $filename) {
$pathname = $this->get_remote_path_from_hash($contenthash);
$pathname = $this->get_local_path_from_hash($contenthash);
$mimetype = file_storage::mimetype($pathname, $filename);

if (!$this->is_file_readable_locally_by_hash($contenthash, false) && $mimetype === 'document/unknown') {
if ($mimetype === 'document/unknown' && !$this->is_file_readable_locally_by_hash($contenthash)) {
// The type is unknown, but the full checks weren't completed because the file isn't locally available.
// Ensure we have a local copy and try again.
$pathname = $this->get_local_path_from_hash($contenthash, true);

$mimetype = file_storage::mimetype_from_file($pathname);
}

Expand All @@ -593,18 +592,7 @@ public function mimetype_from_storedfile($file) {
// Files with an empty filesize are treated as directories and have no mimetype.
return null;
}
$pathname = $this->get_remote_path_from_storedfile($file);
$mimetype = file_storage::mimetype($pathname, $file->get_filename());

if (!$this->is_file_readable_locally_by_storedfile($file) && $mimetype === 'document/unknown') {
// The type is unknown, but the full checks weren't completed because the file isn't locally available.
// Ensure we have a local copy and try again.
$pathname = $this->get_local_path_from_storedfile($file, true);

$mimetype = file_storage::mimetype_from_file($pathname);
}

return $mimetype;
return $this->mimetype_from_hash($file->get_contenthash(), $file->get_filename());
}

/**
Expand Down
11 changes: 9 additions & 2 deletions lib/filestorage/stored_file.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ public function is_controlled_link() {
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) {
Expand Down Expand Up @@ -202,15 +203,21 @@ protected function update($dataobject) {
$updatereferencesneeded = true;
}

if ($updatereferencesneeded || ($field === 'filename' && $this->file_record->filename != $value)) {
$updatemimetype = true;
}

// adding the field
$this->file_record->$field = $value;
} else {
throw new coding_exception("Invalid field name, $field doesn't exist in file record");
}
}
// 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) {
Expand Down
19 changes: 8 additions & 11 deletions lib/filestorage/tests/file_system_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -951,14 +951,13 @@ public function test_mimetype_from_hash_using_filename() {
* a locally available file whose filename does not suggest mimetype.
*/
public function test_mimetype_from_hash_using_file_content() {
$filepath = '/path/to/file/not/currently/on/disk';
$filecontent = 'example content';
$contenthash = file_storage::hash_from_string($filecontent);
$filename = 'example';

$filepath = __DIR__ . "/fixtures/testimage.jpg";
$fs = $this->get_testable_mock(['get_remote_path_from_hash']);
$fs->method('get_remote_path_from_hash')->willReturn($filepath);
$fs = $this->get_testable_mock(['get_local_path_from_hash']);
$fs->method('get_local_path_from_hash')->willReturn($filepath);

$result = $fs->mimetype_from_hash($contenthash, $filename);
$this->assertEquals('image/jpeg', $result);
Expand Down Expand Up @@ -1023,8 +1022,8 @@ public function test_mimetype_from_storedfile_using_filename() {
*/
public function test_mimetype_from_storedfile_using_file_content() {
$filepath = __DIR__ . "/fixtures/testimage.jpg";
$fs = $this->get_testable_mock(['get_remote_path_from_storedfile']);
$fs->method('get_remote_path_from_storedfile')->willReturn($filepath);
$fs = $this->get_testable_mock(['get_local_path_from_hash']);
$fs->method('get_local_path_from_hash')->willReturn($filepath);

$file = $this->get_stored_file('example content');

Expand All @@ -1040,14 +1039,12 @@ public function test_mimetype_from_storedfile_using_file_content_remote() {
$filepath = __DIR__ . "/fixtures/testimage.jpg";

$fs = $this->get_testable_mock([
'get_remote_path_from_storedfile',
'is_file_readable_locally_by_storedfile',
'get_local_path_from_storedfile',
'is_file_readable_locally_by_hash',
'get_local_path_from_hash',
]);

$fs->method('get_remote_path_from_storedfile')->willReturn('/path/to/remote/file');
$fs->method('is_file_readable_locally_by_storedfile')->willReturn(false);
$fs->method('get_local_path_from_storedfile')->willReturn($filepath);
$fs->method('is_file_readable_locally_by_hash')->willReturn(false);
$fs->method('get_local_path_from_hash')->will($this->onConsecutiveCalls('/path/to/remote/file', $filepath));

$file = $this->get_stored_file('example content');

Expand Down

0 comments on commit a1b8b9c

Please sign in to comment.