MDL-35773 core_backup: use existing API to recover files

This commit is contained in:
Mark Nelson
2019-07-26 11:59:56 +08:00
parent 88031d35de
commit 94c71056a3
7 changed files with 30 additions and 26 deletions
+18 -4
View File
@@ -1062,13 +1062,27 @@ abstract class restore_dbops {
$foundfile = reset($foundfiles);
$fs->create_file_from_storedfile($file_record, $foundfile->id);
} else {
// Finally try to restore the file from trash.
$filesytem = $fs->get_file_system();
$filesystem = $fs->get_file_system();
$restorefile = $file;
$restorefile->contextid = $newcontextid;
$storedfile = new stored_file($fs, $restorefile);
$trashrecovery = $filesytem->recover_file($storedfile, true);
if (!$trashrecovery) {
// Ok, let's try recover this file.
// 1. We check if the file can be fetched locally without attempting to fetch
// from the trash.
// 2. We check if we can get the remote filepath for the specified stored file.
// 3. We check if the file can be fetched from the trash.
// 4. All failed, say we couldn't find it.
if ($filesystem->is_file_readable_locally_by_storedfile($storedfile)) {
$localpath = $filesystem->get_local_path_from_storedfile($storedfile);
$fs->create_file_from_pathname($file, $localpath);
} else if ($filesystem->is_file_readable_remotely_by_storedfile($storedfile)) {
$url = $filesystem->get_remote_path_from_storedfile($storedfile);
$fs->create_file_from_url($file, $url);
} else if ($filesystem->is_file_readable_locally_by_storedfile($storedfile, true)) {
$localpath = $filesystem->get_local_path_from_storedfile($storedfile, true);
$fs->create_file_from_pathname($file, $localpath);
} else {
// A matching file was not found.
$results[] = self::get_missing_file_result($file);
continue;
+2 -2
View File
@@ -80,7 +80,7 @@ abstract class file_system {
* @param bool $fetchifnotfound Whether to attempt to fetch from the remote path if not found.
* @return string full path to pool file with file content
*/
protected function get_local_path_from_storedfile(stored_file $file, $fetchifnotfound = false) {
public function get_local_path_from_storedfile(stored_file $file, $fetchifnotfound = false) {
return $this->get_local_path_from_hash($file->get_contenthash(), $fetchifnotfound);
}
@@ -94,7 +94,7 @@ abstract class file_system {
* @param stored_file $file The file to serve.
* @return string full path to pool file with file content
*/
protected function get_remote_path_from_storedfile(stored_file $file) {
public function get_remote_path_from_storedfile(stored_file $file) {
return $this->get_remote_path_from_hash($file->get_contenthash(), false);
}
+2 -2
View File
@@ -119,7 +119,7 @@ class file_system_filedir extends file_system {
* @param bool $fetchifnotfound Whether to attempt to fetch from the remote path if not found.
* @return string The full path to the content file
*/
protected function get_local_path_from_storedfile(stored_file $file, $fetchifnotfound = false) {
public function get_local_path_from_storedfile(stored_file $file, $fetchifnotfound = false) {
$filepath = $this->get_local_path_from_hash($file->get_contenthash(), $fetchifnotfound);
// Try content recovery.
@@ -136,7 +136,7 @@ class file_system_filedir extends file_system {
* @param stored_file $file The file to serve.
* @return string full path to pool file with file content
*/
protected function get_remote_path_from_storedfile(stored_file $file) {
public function get_remote_path_from_storedfile(stored_file $file) {
return $this->get_local_path_from_storedfile($file, false);
}
+2 -6
View File
@@ -74,10 +74,8 @@ class core_files_file_storage_testcase extends advanced_testcase {
$this->assertTrue($DB->record_exists('files', array('pathnamehash'=>$pathhash)));
$method = new ReflectionMethod('file_system', 'get_local_path_from_storedfile');
$method->setAccessible(true);
$filesystem = $fs->get_file_system();
$location = $method->invokeArgs($filesystem, array($file, true));
$location = $filesystem->get_local_path_from_storedfile($file, true);
$this->assertFileExists($location);
@@ -149,10 +147,8 @@ class core_files_file_storage_testcase extends advanced_testcase {
$this->assertTrue($DB->record_exists('files', array('pathnamehash'=>$pathhash)));
$method = new ReflectionMethod('file_system', 'get_local_path_from_storedfile');
$method->setAccessible(true);
$filesystem = $fs->get_file_system();
$location = $method->invokeArgs($filesystem, array($file, true));
$location = $filesystem->get_local_path_from_storedfile($file, true);
$this->assertFileExists($location);
@@ -257,9 +257,7 @@ class core_files_file_system_filedir_testcase extends advanced_testcase {
->with($this->equalTo($file));
$file = $this->get_stored_file('example content');
$method = new ReflectionMethod(file_system_filedir::class, 'get_local_path_from_storedfile');
$method->setAccessible(true);
$result = $method->invokeArgs($fs, array($file, true));
$result = $fs->get_local_path_from_storedfile($file, true);
$this->assertEquals($filepath, $result);
}
@@ -287,9 +285,7 @@ class core_files_file_system_filedir_testcase extends advanced_testcase {
->method('recover_file');
$file = $this->get_stored_file('example content');
$method = new ReflectionMethod(file_system_filedir::class, 'get_local_path_from_storedfile');
$method->setAccessible(true);
$result = $method->invokeArgs($fs, array($file, false));
$result = $fs->get_local_path_from_storedfile($file, false);
$this->assertEquals($filepath, $result);
}
+2 -6
View File
@@ -250,9 +250,7 @@ class core_files_file_system_testcase extends advanced_testcase {
$file = $this->get_stored_file($filecontent);
$method = new ReflectionMethod(file_system::class, 'get_local_path_from_storedfile');
$method->setAccessible(true);
$result = $method->invokeArgs($fs, array_merge([$file], $args));
$result = $fs->get_local_path_from_storedfile($file, $fetch);
$this->assertEquals($filepath, $result);
}
@@ -280,9 +278,7 @@ class core_files_file_system_testcase extends advanced_testcase {
$file = $this->get_stored_file($filecontent);
$method = new ReflectionMethod(file_system::class, 'get_remote_path_from_storedfile');
$method->setAccessible(true);
$result = $method->invokeArgs($fs, [$file]);
$result = $fs->get_remote_path_from_storedfile($file);
$this->assertEquals($filepath, $result);
}
+2
View File
@@ -20,6 +20,8 @@ information provided here is intended especially for developers.
at least a single checkbox item is selected or not.
* Final deprecation (removal) of the core/modal_confirm dialogue.
* Upgrade scssphp to v1.0.2, This involves renaming classes from Leafo => ScssPhp as the repo has changed.
* The methods get_local_path_from_storedfile and get_remote_path_from_storedfile in lib/filestore/file_system.php
are now public. If you are overriding these then you will need to change your methods to public in your class.
=== 3.7 ===