MDL-34290 repository_boxnet, boxlib use request timeouts

boxlib receives additional argument as request timeout
repository_boxnet::get_file_by_reference respects request timeouts and downloads file into moodle only if it is image
also some improvements to repository_boxnet source display functions;
also do not cache result of request in retrieving of listing, user is unable to see the new files he added to box.
This commit is contained in:
Marina Glancy 2012-07-31 10:54:48 +08:00
parent bc6f241ca2
commit f24b0f69ee
2 changed files with 38 additions and 23 deletions

View File

@ -176,7 +176,7 @@ class boxclient {
$params['action'] = 'get_account_tree';
$params['onelevel'] = 1;
$params['params[]'] = 'nozip';
$c = new curl(array('debug'=>$this->debug, 'cache'=>true, 'module_cache'=>'repository'));
$c = new curl(array('debug'=>$this->debug));
$c->setopt(array('CURLOPT_FOLLOWLOCATION'=>1));
try {
$args = array();
@ -196,23 +196,25 @@ class boxclient {
* Get box.net file info
*
* @param string $fileid
* @return string|null
* @param int $timeout request timeout in seconds
* @return stdClass|null
*/
function get_file_info($fileid) {
function get_file_info($fileid, $timeout = 0) {
$this->_clearErrors();
$params = array();
$params['action'] = 'get_file_info';
$params['file_id'] = $fileid;
$params['auth_token'] = $this->auth_token;
$params['api_key'] = $this->api_key;
$http = new curl(array('debug'=>$this->debug, 'cache'=>true, 'module_cache'=>'repository'));
$xml = $http->get($this->_box_api_url, $params);
$o = simplexml_load_string(trim($xml));
if ($o->status == 's_get_file_info') {
return $o->info;
} else {
return null;
$http = new curl(array('debug'=>$this->debug));
$xml = $http->get($this->_box_api_url, $params, array('timeout' => $timeout));
if (!$http->get_errno()) {
$o = simplexml_load_string(trim($xml));
if ($o->status == 's_get_file_info') {
return $o->info;
}
}
return null;
}
/**

View File

@ -277,12 +277,21 @@ class repository_boxnet extends repository {
* @return null|stdClass with attribute 'filepath'
*/
public function get_file_by_reference($reference) {
$boxnetfile = $this->get_file($reference->reference);
// Please note that here we will ALWAYS receive a file
// If source file has been removed from external server, box.com still returns
// a plain/text file with content 'no such file' (filesize will be 12 bytes)
if (!empty($boxnetfile['path'])) {
return (object)array('filepath' => $boxnetfile['path']);
$array = explode('/', $reference->reference);
$fileid = array_pop($array);
$fileinfo = $this->boxclient->get_file_info($fileid, self::SYNCFILE_TIMEOUT);
if ($fileinfo) {
$size = (int)$fileinfo->size;
if (file_extension_in_typegroup($fileinfo->file_name, 'web_image')) {
// this is an image - download it to moodle
$path = $this->prepare_file('');
$c = new curl;
$result = $c->download_one($reference->reference, null, array('filepath' => $path, 'timeout' => self::SYNCIMAGE_TIMEOUT));
if ($result === true) {
return (object)array('filepath' => $path);
}
}
return (object)array('filesize' => $size);
}
return null;
}
@ -297,13 +306,16 @@ class repository_boxnet extends repository {
*/
public function get_reference_details($reference, $filestatus = 0) {
// Indicate it's from box.net repository + secure URL
$array = explode('/', $reference);
$fileid = array_pop($array);
$fileinfo = $this->boxclient->get_file_info($fileid, self::SYNCFILE_TIMEOUT);
if (!empty($fileinfo)) {
$reference = (string)$fileinfo->file_name;
}
$details = $this->get_name() . ': ' . $reference;
if (!$filestatus) {
if (!empty($fileinfo)) {
return $details;
} else {
// at the moment for box.net files we never can be sure that source is missing
// because box.com never returns 404 error.
// So we never change the status and actually this part is unreachable
return get_string('lostsource', 'repository', $details);
}
}
@ -315,13 +327,14 @@ class repository_boxnet extends repository {
* @return string|null
*/
public function get_file_source_info($url) {
global $USER;
$array = explode('/', $url);
$fileid = array_pop($array);
$fileinfo = $this->boxclient->get_file_info($fileid);
$fileinfo = $this->boxclient->get_file_info($fileid, self::SYNCFILE_TIMEOUT);
if (!empty($fileinfo)) {
return 'Box: ' . (string)$fileinfo->file_name;
return 'Box ('. fullname($USER). '): '. (string)$fileinfo->file_name. ': '. $url;
} else {
return $url;
return 'Box: '. $url;
}
}