Merge branch 'MDL-59700-master' of git://github.com/cameron1729/moodle

This commit is contained in:
Andrew Nicols 2017-08-08 10:12:35 +08:00
commit 286a38eeaa
8 changed files with 78 additions and 19 deletions

View file

@ -1318,7 +1318,7 @@ class file_storage {
$newrecord->status = empty($filerecord->status) ? 0 : $filerecord->status; $newrecord->status = empty($filerecord->status) ? 0 : $filerecord->status;
$newrecord->sortorder = $filerecord->sortorder; $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); $newrecord->pathnamehash = $this->get_pathname_hash($newrecord->contextid, $newrecord->component, $newrecord->filearea, $newrecord->itemid, $newrecord->filepath, $newrecord->filename);
@ -1432,7 +1432,7 @@ class file_storage {
$newrecord->status = empty($filerecord->status) ? 0 : $filerecord->status; $newrecord->status = empty($filerecord->status) ? 0 : $filerecord->status;
$newrecord->sortorder = $filerecord->sortorder; $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)) { if (empty($filerecord->mimetype)) {
$newrecord->mimetype = $this->filesystem->mimetype_from_hash($newrecord->contenthash, $newrecord->filename); $newrecord->mimetype = $this->filesystem->mimetype_from_hash($newrecord->contenthash, $newrecord->filename);
} else { } else {
@ -1456,6 +1456,30 @@ class file_storage {
return $this->get_file_instance($newrecord); return $this->get_file_instance($newrecord);
} }
/**
* Synchronise stored file from file.
*
* @param stored_file $file Stored file to synchronise.
* @param string $path Path to the file to synchronise from.
* @param stdClass $filerecord The file record from the database.
*/
public function synchronise_stored_file_from_file(stored_file $file, $path, $filerecord) {
list($contenthash, $filesize) = $this->add_file_to_pool($path, null, $filerecord);
$file->set_synchronized($contenthash, $filesize);
}
/**
* Synchronise stored file from string.
*
* @param stored_file $file Stored file to synchronise.
* @param string $content File content.
* @param stdClass $filerecord The file record from the database.
*/
public function synchronise_stored_file_from_string(stored_file $file, $content, $filerecord) {
list($contenthash, $filesize) = $this->add_string_to_pool($content, $filerecord);
$file->set_synchronized($contenthash, $filesize);
}
/** /**
* Create a new alias/shortcut file from file reference information * Create a new alias/shortcut file from file reference information
* *
@ -1570,7 +1594,7 @@ class file_storage {
} else { } else {
// External file doesn't have content in moodle. // External file doesn't have content in moodle.
// So we create an empty file for it. // 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);
} }
} }
@ -1759,10 +1783,12 @@ class file_storage {
* Add file content to sha1 pool. * Add file content to sha1 pool.
* *
* @param string $pathname path to file * @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) * @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); return $this->filesystem->add_file_from_path($pathname, $contenthash);
} }
@ -1772,10 +1798,27 @@ class file_storage {
* @param string $content file content - binary string * @param string $content file content - binary string
* @return array (contenthash, filesize, newfile) * @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); 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. * Serve file content using X-Sendfile header.
* Please make sure that all headers are already sent * Please make sure that all headers are already sent

View file

@ -588,6 +588,24 @@ class stored_file {
return $this->fs->create_directory($this->file_record->contextid, $this->file_record->component, $this->file_record->filearea, $this->file_record->itemid, $filepath); return $this->fs->create_directory($this->file_record->contextid, $this->file_record->component, $this->file_record->filearea, $this->file_record->itemid, $filepath);
} }
/**
* Set synchronised content from file.
*
* @param string $path Path to the file.
*/
public function set_synchronised_content_from_file($path) {
$this->fs->synchronise_stored_file_from_file($this, $path, $this->file_record);
}
/**
* Set synchronised content from content.
*
* @param string $content File content.
*/
public function set_synchronised_content_from_string($content) {
$this->fs->synchronise_stored_file_from_string($this, $content, $this->file_record);
}
/** /**
* Synchronize file if it is a reference and needs synchronizing * Synchronize file if it is a reference and needs synchronizing
* *

View file

@ -430,9 +430,7 @@ class repository_boxnet extends repository {
$result = $c->download_one($url, null, array('filepath' => $path, 'timeout' => $CFG->repositorysyncimagetimeout)); $result = $c->download_one($url, null, array('filepath' => $path, 'timeout' => $CFG->repositorysyncimagetimeout));
$info = $c->get_info(); $info = $c->get_info();
if ($result === true && isset($info['http_code']) && $info['http_code'] == 200) { if ($result === true && isset($info['http_code']) && $info['http_code'] == 200) {
$fs = get_file_storage(); $file->set_synchronised_content_from_file($path);
list($contenthash, $filesize, $newfile) = $fs->add_file_to_pool($path);
$file->set_synchronized($contenthash, $filesize);
return true; return true;
} }
} }

View file

@ -662,9 +662,7 @@ class repository_dropbox extends repository {
]); ]);
$info = $c->get_info(); $info = $c->get_info();
if ($result === true && isset($info['http_code']) && $info['http_code'] == 200) { if ($result === true && isset($info['http_code']) && $info['http_code'] == 200) {
$fs = get_file_storage(); $file->set_synchronised_content_from_file($saveas);
list($contenthash, $filesize, ) = $fs->add_file_to_pool($saveas);
$file->set_synchronized($contenthash, $filesize);
return true; return true;
} }
} catch (Exception $e) { } catch (Exception $e) {

View file

@ -214,9 +214,7 @@ class repository_equella extends repository {
$path = $this->prepare_file(''); $path = $this->prepare_file('');
$result = $c->download_one($url, null, array('filepath' => $path, 'followlocation' => true, 'timeout' => $CFG->repositorysyncimagetimeout)); $result = $c->download_one($url, null, array('filepath' => $path, 'followlocation' => true, 'timeout' => $CFG->repositorysyncimagetimeout));
if ($result === true) { if ($result === true) {
$fs = get_file_storage(); $file->set_synchronised_content_from_file($path);
list($contenthash, $filesize, $newfile) = $fs->add_file_to_pool($path);
$file->set_synchronized($contenthash, $filesize);
return true; return true;
} }
} else { } else {

View file

@ -577,7 +577,9 @@ class repository_filesystem extends repository {
$filesize = filesize($filepath); $filesize = filesize($filepath);
} else { } else {
// Copy file into moodle filepool (used to generate an image thumbnail). // Copy file into moodle filepool (used to generate an image thumbnail).
list($contenthash, $filesize, $newfile) = $fs->add_file_to_pool($filepath); $file->set_timemodified(filemtime($filepath));
$file->set_synchronised_content_from_file($filepath);
return true;
} }
} else { } else {
// Update only file size so file will NOT be copied into moodle filepool. // Update only file size so file will NOT be copied into moodle filepool.

View file

@ -1757,9 +1757,7 @@ abstract class repository implements cacheable_object {
try { try {
$fileinfo = $this->get_file($file->get_reference()); $fileinfo = $this->get_file($file->get_reference());
if (isset($fileinfo['path'])) { if (isset($fileinfo['path'])) {
list($contenthash, $filesize, $newfile) = $fs->add_file_to_pool($fileinfo['path']); $file->set_synchronised_content_from_file($fileinfo['path']);
// set this file and other similar aliases synchronised
$file->set_synchronized($contenthash, $filesize);
} else { } else {
throw new moodle_exception('errorwhiledownload', 'repository', '', ''); throw new moodle_exception('errorwhiledownload', 'repository', '', '');
} }

View file

@ -3,6 +3,10 @@ information provided here is intended especially for developers. Full
details of the repository API are available on Moodle docs: details of the repository API are available on Moodle docs:
http://docs.moodle.org/dev/Repository_API http://docs.moodle.org/dev/Repository_API
=== 3.4 ===
Repositories should no longer directly call file_system#add_file_to_pool or file_system#add_string_to_pool
instead they should call the stored_file method, set_synchronised_content_from_file or set_synchronised_content_from_string
=== 3.3 === === 3.3 ===
The skydrive repository is deprecated - please migrate to the newer onedrive repository. The skydrive repository is deprecated - please migrate to the newer onedrive repository.