mirror of
https://github.com/moodle/moodle.git
synced 2025-08-04 16:36:37 +02:00
MDL-42016 repository: Implement new sync method in repositories
This commit is contained in:
parent
873555604f
commit
5e5923ea28
4 changed files with 92 additions and 122 deletions
|
@ -134,7 +134,7 @@ class repository_equella extends repository {
|
|||
* If we received the connection timeout more than 3 times in a row, we don't attemt to
|
||||
* connect to the server any more during this request.
|
||||
*
|
||||
* This function is used by {@link repository_equella::get_file_by_reference()} that
|
||||
* This function is used by {@link repository_equella::sync_reference()} that
|
||||
* synchronises the file size of referenced files.
|
||||
*
|
||||
* @param int $errno omit if we just want to know the return value, the last curl_errno otherwise
|
||||
|
@ -159,18 +159,6 @@ class repository_equella extends repository {
|
|||
return ($countfailures[$sess] < 3);
|
||||
}
|
||||
|
||||
/**
|
||||
* Decide whether or not the file should be synced
|
||||
*
|
||||
* @param stored_file $storedfile
|
||||
* @return bool
|
||||
*/
|
||||
public function sync_individual_file(stored_file $storedfile) {
|
||||
// if we had several unsuccessfull attempts to connect to server - do not try any more
|
||||
return $this->connection_result();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Download a file, this function can be overridden by subclass. {@link curl}
|
||||
*
|
||||
|
@ -202,34 +190,30 @@ class repository_equella extends repository {
|
|||
return array('path'=>$path, 'url'=>$url);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns information about file in this repository by reference
|
||||
*
|
||||
* If the file is an image we download the contents and save it in our filesystem
|
||||
* so we can generate thumbnails. Otherwise we just request the file size.
|
||||
* Returns null if file not found or can not be accessed
|
||||
*
|
||||
* @param stdClass $reference file reference db record
|
||||
* @return stdClass|null contains one of the following:
|
||||
* - 'filesize' (for non-image files or files we failed to retrieve fully because of timeout)
|
||||
* - 'filepath' (for image files that we retrieived and saved)
|
||||
*/
|
||||
public function get_file_by_reference($reference) {
|
||||
public function sync_reference(stored_file $file) {
|
||||
global $USER;
|
||||
$ref = @unserialize(base64_decode($reference->reference));
|
||||
if ($file->get_referencelastsync() + DAYSECS > time() || !$this->connection_result()) {
|
||||
// Synchronise not more often than once a day.
|
||||
// if we had several unsuccessfull attempts to connect to server - do not try any more.
|
||||
return false;
|
||||
}
|
||||
$ref = @unserialize(base64_decode($file->get_reference()));
|
||||
if (!isset($ref->url) || !($url = $this->appendtoken($ref->url))) {
|
||||
// Occurs when the user isn't known..
|
||||
return null;
|
||||
$file->set_missingsource();
|
||||
return true;
|
||||
}
|
||||
|
||||
$return = null;
|
||||
$cookiepathname = $this->prepare_file($USER->id. '_'. uniqid('', true). '.cookie');
|
||||
$c = new curl(array('cookie' => $cookiepathname));
|
||||
if (file_extension_in_typegroup($ref->filename, 'web_image')) {
|
||||
$path = $this->prepare_file('');
|
||||
$result = $c->download_one($url, null, array('filepath' => $path, 'followlocation' => true, 'timeout' => self::SYNCIMAGE_TIMEOUT));
|
||||
if ($result === true) {
|
||||
$return = (object)array('filepath' => $path);
|
||||
$fs = get_file_storage();
|
||||
list($contenthash, $filesize, $newfile) = $fs->add_file_to_pool($path);
|
||||
$file->set_synchronized($contenthash, $filesize);
|
||||
return true;
|
||||
}
|
||||
} else {
|
||||
$result = $c->head($url, array('followlocation' => true, 'timeout' => self::SYNCFILE_TIMEOUT));
|
||||
|
@ -241,13 +225,15 @@ class repository_equella extends repository {
|
|||
|
||||
$this->connection_result($c->get_errno());
|
||||
$curlinfo = $c->get_info();
|
||||
if ($return === null && isset($curlinfo['http_code']) && $curlinfo['http_code'] == 200
|
||||
if (isset($curlinfo['http_code']) && $curlinfo['http_code'] == 200
|
||||
&& array_key_exists('download_content_length', $curlinfo)
|
||||
&& $curlinfo['download_content_length'] >= 0) {
|
||||
// we received a correct header and at least can tell the file size
|
||||
$return = (object)array('filesize' => $curlinfo['download_content_length']);
|
||||
$file->set_synchronized(null, $curlinfo['download_content_length']);
|
||||
return true;
|
||||
}
|
||||
return $return;
|
||||
$file->set_missingsource();
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue