MDL-34290 repository API: add repository function to import referenced file

it must be independed from sync_external_file because sync often does not actually download contents, it is used just to retrieve
the size of the file. Besides the timeouts for get_file and sync requests are very different.

Also add option to send_stored_file() to ignore reference and send cached contents
This commit is contained in:
Marina Glancy 2012-07-31 10:50:05 +08:00
parent 22fa5d9a7c
commit 9001c7242e
4 changed files with 89 additions and 6 deletions

View file

@ -1841,15 +1841,15 @@ class file_storage {
/**
* Convert file alias to local file
*
* @throws moodle_exception if file could not be downloaded
*
* @param stored_file $storedfile a stored_file instances
* @param int $maxbytes throw an exception if file size is bigger than $maxbytes (0 means no limit)
* @return stored_file stored_file
*/
public function import_external_file(stored_file $storedfile) {
public function import_external_file(stored_file $storedfile, $maxbytes = 0) {
global $CFG;
require_once($CFG->dirroot.'/repository/lib.php');
// sync external file
repository::sync_external_file($storedfile);
// Remove file references
$storedfile->import_external_file_contents($maxbytes);
$storedfile->delete_reference();
return $storedfile;
}

View file

@ -928,4 +928,16 @@ class stored_file {
public function send_file($lifetime, $filter, $forcedownload, $options) {
$this->repository->send_file($this, $lifetime, $filter, $forcedownload, $options);
}
/**
* Imports the contents of an external file into moodle filepool.
*
* @throws moodle_exception if file could not be downloaded or is too big
* @param int $maxbytes throw an exception if file size is bigger than $maxbytes (0 means no limit)
*/
public function import_external_file_contents($maxbytes = 0) {
if ($this->repository) {
$this->repository->import_external_file_contents($this, $maxbytes);
}
}
}