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

View file

@ -277,12 +277,21 @@ class repository_boxnet extends repository {
* @return null|stdClass with attribute 'filepath' * @return null|stdClass with attribute 'filepath'
*/ */
public function get_file_by_reference($reference) { public function get_file_by_reference($reference) {
$boxnetfile = $this->get_file($reference->reference); $array = explode('/', $reference->reference);
// Please note that here we will ALWAYS receive a file $fileid = array_pop($array);
// If source file has been removed from external server, box.com still returns $fileinfo = $this->boxclient->get_file_info($fileid, self::SYNCFILE_TIMEOUT);
// a plain/text file with content 'no such file' (filesize will be 12 bytes) if ($fileinfo) {
if (!empty($boxnetfile['path'])) { $size = (int)$fileinfo->size;
return (object)array('filepath' => $boxnetfile['path']); 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; return null;
} }
@ -297,13 +306,16 @@ class repository_boxnet extends repository {
*/ */
public function get_reference_details($reference, $filestatus = 0) { public function get_reference_details($reference, $filestatus = 0) {
// Indicate it's from box.net repository + secure URL // 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; $details = $this->get_name() . ': ' . $reference;
if (!$filestatus) { if (!empty($fileinfo)) {
return $details; return $details;
} else { } 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); return get_string('lostsource', 'repository', $details);
} }
} }
@ -315,13 +327,14 @@ class repository_boxnet extends repository {
* @return string|null * @return string|null
*/ */
public function get_file_source_info($url) { public function get_file_source_info($url) {
global $USER;
$array = explode('/', $url); $array = explode('/', $url);
$fileid = array_pop($array); $fileid = array_pop($array);
$fileinfo = $this->boxclient->get_file_info($fileid); $fileinfo = $this->boxclient->get_file_info($fileid, self::SYNCFILE_TIMEOUT);
if (!empty($fileinfo)) { if (!empty($fileinfo)) {
return 'Box: ' . (string)$fileinfo->file_name; return 'Box ('. fullname($USER). '): '. (string)$fileinfo->file_name. ': '. $url;
} else { } else {
return $url; return 'Box: '. $url;
} }
} }