Skip to content

Commit 03756f0

Browse files
committed
MDL-59700 filestorage: Rework repositories to avoid add_file_to_pool
For historical reasons repositories need to call add_file_to_pool to sync file records. However now that a before_file_created hook has been added additional information is needed by add_file_to_pool. Ideally add_file_to_pool and friends will become private/protected, so we need to remove all uses of it in core. This patch adds some new methods to the file class to allow syncing to be managed internally by the file and the file_storage class.
1 parent 8243706 commit 03756f0

File tree

8 files changed

+53
-13
lines changed

8 files changed

+53
-13
lines changed

lib/filestorage/file_storage.php

+24
Original file line numberDiff line numberDiff line change
@@ -1456,6 +1456,30 @@ public function create_file_from_string($filerecord, $content) {
14561456
return $this->get_file_instance($newrecord);
14571457
}
14581458

1459+
/**
1460+
* Synchronise stored file from file.
1461+
*
1462+
* @param stored_file $file Stored file to synchronise.
1463+
* @param string $path Path to the file to synchronise from.
1464+
* @param stdClass $filerecord The file record from the database.
1465+
*/
1466+
public function synchronise_stored_file_from_file(stored_file $file, $path, $filerecord) {
1467+
list($contenthash, $filesize) = $this->add_file_to_pool($path, null, $filerecord);
1468+
$file->set_synchronized($contenthash, $filesize);
1469+
}
1470+
1471+
/**
1472+
* Synchronise stored file from string.
1473+
*
1474+
* @param stored_file $file Stored file to synchronise.
1475+
* @param string $content File content.
1476+
* @param stdClass $filerecord The file record from the database.
1477+
*/
1478+
public function synchronise_stored_file_from_string(stored_file $file, $content, $filerecord) {
1479+
list($contenthash, $filesize) = $this->add_string_to_pool($content, $filerecord);
1480+
$file->set_synchronized($contenthash, $filesize);
1481+
}
1482+
14591483
/**
14601484
* Create a new alias/shortcut file from file reference information
14611485
*

lib/filestorage/stored_file.php

+18
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,24 @@ public function get_parent_directory() {
588588
return $this->fs->create_directory($this->file_record->contextid, $this->file_record->component, $this->file_record->filearea, $this->file_record->itemid, $filepath);
589589
}
590590

591+
/**
592+
* Set synchronised content from file.
593+
*
594+
* @param string $path Path to the file.
595+
*/
596+
public function set_synchronised_content_from_file($path) {
597+
$this->fs->synchronise_stored_file_from_file($this, $path, $this->file_record);
598+
}
599+
600+
/**
601+
* Set synchronised content from content.
602+
*
603+
* @param string $content File content.
604+
*/
605+
public function set_synchronised_content_from_string($content) {
606+
$this->fs->synchronise_stored_file_from_string($this, $content, $this->file_record);
607+
}
608+
591609
/**
592610
* Synchronize file if it is a reference and needs synchronizing
593611
*

repository/boxnet/lib.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -430,9 +430,7 @@ public function sync_reference(stored_file $file) {
430430
$result = $c->download_one($url, null, array('filepath' => $path, 'timeout' => $CFG->repositorysyncimagetimeout));
431431
$info = $c->get_info();
432432
if ($result === true && isset($info['http_code']) && $info['http_code'] == 200) {
433-
$fs = get_file_storage();
434-
list($contenthash, $filesize, $newfile) = $fs->add_file_to_pool($path);
435-
$file->set_synchronized($contenthash, $filesize);
433+
$file->set_synchronised_content_from_file($path);
436434
return true;
437435
}
438436
}

repository/dropbox/lib.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -662,9 +662,7 @@ public function sync_reference(stored_file $file) {
662662
]);
663663
$info = $c->get_info();
664664
if ($result === true && isset($info['http_code']) && $info['http_code'] == 200) {
665-
$fs = get_file_storage();
666-
list($contenthash, $filesize, ) = $fs->add_file_to_pool($saveas);
667-
$file->set_synchronized($contenthash, $filesize);
665+
$file->set_synchronised_content_from_file($saveas);
668666
return true;
669667
}
670668
} catch (Exception $e) {

repository/equella/lib.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -214,9 +214,7 @@ public function sync_reference(stored_file $file) {
214214
$path = $this->prepare_file('');
215215
$result = $c->download_one($url, null, array('filepath' => $path, 'followlocation' => true, 'timeout' => $CFG->repositorysyncimagetimeout));
216216
if ($result === true) {
217-
$fs = get_file_storage();
218-
list($contenthash, $filesize, $newfile) = $fs->add_file_to_pool($path);
219-
$file->set_synchronized($contenthash, $filesize);
217+
$file->set_synchronised_content_from_file($path);
220218
return true;
221219
}
222220
} else {

repository/filesystem/lib.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,9 @@ public function sync_reference(stored_file $file) {
577577
$filesize = filesize($filepath);
578578
} else {
579579
// Copy file into moodle filepool (used to generate an image thumbnail).
580-
list($contenthash, $filesize, $newfile) = $fs->add_file_to_pool($filepath);
580+
$file->set_timemodified(filemtime($filepath));
581+
$file->set_synchronised_content_from_file($filepath);
582+
return true;
581583
}
582584
} else {
583585
// Update only file size so file will NOT be copied into moodle filepool.

repository/lib.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -1757,9 +1757,7 @@ public function import_external_file_contents(stored_file $file, $maxbytes = 0)
17571757
try {
17581758
$fileinfo = $this->get_file($file->get_reference());
17591759
if (isset($fileinfo['path'])) {
1760-
list($contenthash, $filesize, $newfile) = $fs->add_file_to_pool($fileinfo['path']);
1761-
// set this file and other similar aliases synchronised
1762-
$file->set_synchronized($contenthash, $filesize);
1760+
$file->set_synchronised_content_from_file($fileinfo['path']);
17631761
} else {
17641762
throw new moodle_exception('errorwhiledownload', 'repository', '', '');
17651763
}

repository/upgrade.txt

+4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ information provided here is intended especially for developers. Full
33
details of the repository API are available on Moodle docs:
44
http://docs.moodle.org/dev/Repository_API
55

6+
=== 3.4 ===
7+
Repositories should no longer directly call file_system#add_file_to_pool or file_system#add_string_to_pool
8+
instead they should call the stored_file method, set_synchronised_content_from_file or set_synchronised_content_from_string
9+
610
=== 3.3 ===
711
The skydrive repository is deprecated - please migrate to the newer onedrive repository.
812

0 commit comments

Comments
 (0)