diff --git a/lib/googleapi.php b/lib/googleapi.php index fe2b9335b5989..c2d529fd6b7ab 100644 --- a/lib/googleapi.php +++ b/lib/googleapi.php @@ -185,12 +185,21 @@ public function send_file($file) { * * @param string $url url of file * @param string $path path to save file to + * @param int $timeout request timeout, default 0 which means no timeout * @return array stucture for repository download_file */ - public function download_file($url, $path) { - $content = $this->googleoauth->get($url); - file_put_contents($path, $content); - return array('path'=>$path, 'url'=>$url); + public function download_file($url, $path, $timeout = 0) { + $result = $this->googleoauth->download_one($url, null, array('filepath' => $path, 'timeout' => $timeout)); + if ($result === true) { + $info = $this->googleoauth->get_info(); + if (isset($info['http_code']) && $info['http_code'] == 200) { + return array('path'=>$path, 'url'=>$url); + } else { + throw new moodle_exception('cannotdownload', 'repository'); + } + } else { + throw new moodle_exception('errorwhiledownload', 'repository', '', $result); + } } } diff --git a/repository/googledocs/lib.php b/repository/googledocs/lib.php index c3a00e1a628a9..38299decb73de 100644 --- a/repository/googledocs/lib.php +++ b/repository/googledocs/lib.php @@ -95,7 +95,7 @@ public function get_file($url, $file = '') { $gdocs = new google_docs($this->googleoauth); $path = $this->prepare_file($file); - return $gdocs->download_file($url, $path); + return $gdocs->download_file($url, $path, self::GETFILE_TIMEOUT); } public function supported_filetypes() {