Skip to content

Commit

Permalink
MDL-59700 filestorage: Add before_file_created hook
Browse files Browse the repository at this point in the history
  • Loading branch information
cameorn1730 committed Aug 7, 2017
1 parent 8146b1f commit 8243706
Showing 1 changed file with 25 additions and 6 deletions.
31 changes: 25 additions & 6 deletions lib/filestorage/file_storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -1318,7 +1318,7 @@ public function create_file_from_pathname($filerecord, $pathname) {
$newrecord->status = empty($filerecord->status) ? 0 : $filerecord->status;
$newrecord->sortorder = $filerecord->sortorder;

list($newrecord->contenthash, $newrecord->filesize, $newfile) = $this->add_file_to_pool($pathname);
list($newrecord->contenthash, $newrecord->filesize, $newfile) = $this->add_file_to_pool($pathname, null, $newrecord);

$newrecord->pathnamehash = $this->get_pathname_hash($newrecord->contextid, $newrecord->component, $newrecord->filearea, $newrecord->itemid, $newrecord->filepath, $newrecord->filename);

Expand Down Expand Up @@ -1432,7 +1432,7 @@ public function create_file_from_string($filerecord, $content) {
$newrecord->status = empty($filerecord->status) ? 0 : $filerecord->status;
$newrecord->sortorder = $filerecord->sortorder;

list($newrecord->contenthash, $newrecord->filesize, $newfile) = $this->add_string_to_pool($content);
list($newrecord->contenthash, $newrecord->filesize, $newfile) = $this->add_string_to_pool($content, $newrecord);
if (empty($filerecord->mimetype)) {
$newrecord->mimetype = $this->filesystem->mimetype_from_hash($newrecord->contenthash, $newrecord->filename);
} else {
Expand Down Expand Up @@ -1570,7 +1570,7 @@ public function create_file_from_reference($filerecord, $repositoryid, $referenc
} else {
// External file doesn't have content in moodle.
// So we create an empty file for it.
list($filerecord->contenthash, $filerecord->filesize, $newfile) = $this->add_string_to_pool(null);
list($filerecord->contenthash, $filerecord->filesize, $newfile) = $this->add_string_to_pool(null, $filerecord);
}
}

Expand Down Expand Up @@ -1759,10 +1759,12 @@ public function convert_image($filerecord, $fid, $newwidth = null, $newheight =
* Add file content to sha1 pool.
*
* @param string $pathname path to file
* @param string $contenthash sha1 hash of content if known (performance only)
* @param string|null $contenthash sha1 hash of content if known (performance only)
* @param stdClass|null $newrecord New file record
* @return array (contenthash, filesize, newfile)
*/
public function add_file_to_pool($pathname, $contenthash = NULL) {
public function add_file_to_pool($pathname, $contenthash = null, $newrecord = null) {
$this->call_before_file_created_plugin_functions($newrecord, $pathname);
return $this->filesystem->add_file_from_path($pathname, $contenthash);
}

Expand All @@ -1772,10 +1774,27 @@ public function add_file_to_pool($pathname, $contenthash = NULL) {
* @param string $content file content - binary string
* @return array (contenthash, filesize, newfile)
*/
public function add_string_to_pool($content) {
public function add_string_to_pool($content, $newrecord = null) {
$this->call_before_file_created_plugin_functions($newrecord, null, $content);
return $this->filesystem->add_file_from_string($content);
}

/**
* before_file_created hook.
*
* @param stdClass|null $newrecord New file record.
* @param string|null $pathname Path to file.
* @param string|null $content File content.
*/
protected function call_before_file_created_plugin_functions($newrecord, $pathname = null, $content = null) {
$pluginsfunction = get_plugins_with_function('before_file_created');
foreach ($pluginsfunction as $plugintype => $plugins) {
foreach ($plugins as $pluginfunction) {
$pluginfunction($newrecord, ['pathname' => $pathname, 'content' => $content]);
}
}
}

/**
* Serve file content using X-Sendfile header.
* Please make sure that all headers are already sent
Expand Down

0 comments on commit 8243706

Please sign in to comment.