Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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.
  • Loading branch information...
commit a5fe04b62bf48126646841fb8bd03ccf6f31864e 1 parent 9001c72
@marinaglancy marinaglancy authored
Showing with 38 additions and 23 deletions.
  1. +12 −10 lib/boxlib.php
  2. +26 −13 repository/boxnet/lib.php
View
22 lib/boxlib.php
@@ -176,7 +176,7 @@ function getfiletree($path, $params = array()) {
$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 @@ function getfiletree($path, $params = array()) {
* 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
39 repository/boxnet/lib.php
@@ -277,12 +277,21 @@ public function get_file_reference($source) {
* @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 @@ public function get_file_by_reference($reference) {
*/
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 @@ public function get_reference_details($reference, $filestatus = 0) {
* @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;
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.