Merge branch 'MDL-77152-master' of https://github.com/sh-csg/moodle

This commit is contained in:
Andrew Nicols 2023-06-24 22:17:33 +08:00
commit 1fee974ed0
No known key found for this signature in database
GPG Key ID: 6D1E3157C8CFBF14
2 changed files with 29 additions and 0 deletions

View File

@ -107,6 +107,12 @@ class file_storage {
* @return string sha1 hash
*/
public static function get_pathname_hash($contextid, $component, $filearea, $itemid, $filepath, $filename) {
if (substr($filepath, 0, 1) != '/') {
$filepath = '/' . $filepath;
}
if (substr($filepath, - 1) != '/') {
$filepath .= '/';
}
return sha1("/$contextid/$component/$filearea/$itemid".$filepath.$filename);
}

View File

@ -2190,6 +2190,29 @@ class file_storage_test extends \advanced_testcase {
$this->assertEquals($expectedmimetype, $mimetype);
}
/**
* Test that get_pathname_hash returns the same file hash for pathnames
* with and without trailing / leading slash.
*
* @covers ::get_pathname_hash
*
*/
public function test_get_pathname_hash(): void {
$contextid = 2;
$component = 'mod_test';
$filearea = 'data';
$itemid = 0;
$filepath1 = '/path';
$filepath2 = '/path/';
$filepath3 = 'path/';
$filename = 'example.jpg';
$hash1 = \file_storage::get_pathname_hash($contextid, $component, $filearea, $itemid, $filepath1, $filename);
$hash2 = \file_storage::get_pathname_hash($contextid, $component, $filearea, $itemid, $filepath2, $filename);
$hash3 = \file_storage::get_pathname_hash($contextid, $component, $filearea, $itemid, $filepath3, $filename);
$this->assertEquals($hash1, $hash2);
$this->assertEquals($hash2, $hash3);
}
}
class test_stored_file_inspection extends stored_file {