From: dongsheng Date: Mon, 16 Mar 2009 02:16:50 +0000 (+0000) Subject: "REPOSITORY/MDL-13766, create prepare_file function, it will prepare temp file for... X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=a53d4f45c474d8c69dde500c91f0186e06f3c2c0;p=moodle.git "REPOSITORY/MDL-13766, create prepare_file function, it will prepare temp file for repository api" --- diff --git a/repository/alfresco/repository.class.php b/repository/alfresco/repository.class.php index 63700e2759..6d06936374 100755 --- a/repository/alfresco/repository.class.php +++ b/repository/alfresco/repository.class.php @@ -128,23 +128,11 @@ class repository_alfresco extends repository { global $CFG; $node = $this->sess->getNode($this->store, $uuid); $url = $this->get_url($node); - if (!file_exists($CFG->dataroot.'/repository/download')) { - mkdir($CFG->dataroot.'/repository/download/', 0777, true); - } - if(is_dir($CFG->dataroot.'/repository/download')) { - $dir = $CFG->dataroot.'/repository/download/'; - } - - if (empty($file)) { - $file = $uuid.'_'.time(); - } - if (file_exists($dir.$file)) { - $file = uniqid('al_').$file; - } - $fp = fopen($dir.$file, 'w'); + $path = $this->prepare_file($file); + $fp = fopen($path, 'w'); $c = new curl; $c->download(array(array('url'=>$url, 'file'=>$fp))); - return $dir.$file; + return $path; } public function print_search() { diff --git a/repository/flickr/repository.class.php b/repository/flickr/repository.class.php index a6c8af3490..ff81b16740 100755 --- a/repository/flickr/repository.class.php +++ b/repository/flickr/repository.class.php @@ -236,25 +236,13 @@ class repository_flickr extends repository { } elseif(!empty($result[2])) { $url = $result[2]['source']; } - if (!file_exists($CFG->dataroot.'/repository/download')) { - mkdir($CFG->dataroot.'/repository/download/', 0777, true); - } - if(is_dir($CFG->dataroot.'/repository/download')) { - $dir = $CFG->dataroot.'/repository/download/'; - } - - if(empty($file)) { - $file = $photo_id.'_'.time().'.jpg'; - } - if(file_exists($dir.$file)) { - $file = uniqid('m').$file; - } - $fp = fopen($dir.$file, 'w'); + $path = $this->prepare_file($file); + $fp = fopen($path, 'w'); $c = new curl; $c->download(array( array('url'=>$url, 'file'=>$fp) )); - return $dir.$file; + return $path; } /** diff --git a/repository/flickr_public/repository.class.php b/repository/flickr_public/repository.class.php index 93479fb5db..5796852546 100644 --- a/repository/flickr_public/repository.class.php +++ b/repository/flickr_public/repository.class.php @@ -280,24 +280,12 @@ class repository_flickr_public extends repository { } elseif(!empty($result[2])) { $url = $result[2]['source']; } - if (!file_exists($CFG->dataroot.'/repository/download')) { - mkdir($CFG->dataroot.'/repository/download/', 0777, true); - } - if(is_dir($CFG->dataroot.'/repository/download')) { - $dir = $CFG->dataroot.'/repository/download/'; - } - - if (empty($file)) { - $file = $photo_id.'_'.time().'.jpg'; - } - if (file_exists($dir.$file)) { - $file = uniqid('m').$file; - } - $fp = fopen($dir.$file, 'w'); + $path = $this->prepare_file($file); + $fp = fopen($path, 'w'); $c = new curl; $c->download(array(array('url'=>$url, 'file'=>$fp))); - return $dir.$file; + return $path; } /** diff --git a/repository/googledocs/repository.class.php b/repository/googledocs/repository.class.php index 18abc15f78..b8a2c2a009 100644 --- a/repository/googledocs/repository.class.php +++ b/repository/googledocs/repository.class.php @@ -99,27 +99,13 @@ class repository_googledocs extends repository { public function get_file($url, $file) { global $CFG; + $path = $this->prepare_file($file); - - //FIXME: Why does every repo plugin.. do this mktemp file itself.. - - if (!file_exists($CFG->dataroot.'/repository/download')) { - mkdir($CFG->dataroot.'/repository/download/', 0777, true); - } - - if(is_dir($CFG->dataroot.'/repository/download')) { - $dir = $CFG->dataroot.'/repository/download/'; - } - - if (empty($file)){ - $file = time(); - } - - $fp = fopen($dir.$file, 'w'); + $fp = fopen($path, 'w'); $gdocs = new google_docs(new google_authsub($this->subauthtoken)); $gdocs->download_file($url, $fp); - return $dir.$file; + return $path; } public function supported_filetypes() { diff --git a/repository/lib.php b/repository/lib.php index ee4b751099..ecdcd1f0a2 100644 --- a/repository/lib.php +++ b/repository/lib.php @@ -1147,16 +1147,11 @@ abstract class repository { } /** - * Download a file, this function can be overridden by - * subclass. - * - * @global object $CFG - * @param string $url the url of file - * @param string $file save location - * @return string the location of the file - * @see curl package + * Decide where to save the file, can be + * reused by sub class + * @param string filename */ - public function get_file($url, $file = '') { + public function prepare_file($file) { global $CFG; if (!file_exists($CFG->dataroot.'/temp/download')) { mkdir($CFG->dataroot.'/temp/download/', 0777, true); @@ -1170,12 +1165,29 @@ abstract class repository { if (file_exists($dir.$file)) { $file = uniqid('m').$file; } - $fp = fopen($dir.$file, 'w'); + return $dir.$file; + } + + /** + * Download a file, this function can be overridden by + * subclass. + * + * @global object $CFG + * @param string $url the url of file + * @param string $file save location + * @return string the location of the file + * @see curl package + */ + public function get_file($url, $file = '') { + global $CFG; + + $path = $this->prepare_file($file); + $fp = fopen($path, 'w'); $c = new curl; $c->download(array( array('url'=>$url, 'file'=>$fp) )); - return $dir.$file; + return $path; } /** diff --git a/repository/local/repository.class.php b/repository/local/repository.class.php index 486be34485..ae56f7da70 100755 --- a/repository/local/repository.class.php +++ b/repository/local/repository.class.php @@ -98,18 +98,7 @@ class repository_local extends repository { */ public function get_file($url, $file = '') { global $CFG; - if (!file_exists($CFG->dataroot.'/temp/download')) { - mkdir($CFG->dataroot.'/temp/download/', 0777, true); - } - if (is_dir($CFG->dataroot.'/temp/download')) { - $dir = $CFG->dataroot.'/temp/download/'; - } - if (empty($file)) { - $file = uniqid('repo').'_'.time().'.tmp'; - } - if (file_exists($dir.$file)) { - $file = uniqid('m').$file; - } + $path = $this->prepare_file($file); ///retrieve the file $fileparams = unserialize(base64_decode($url)); @@ -121,11 +110,11 @@ class repository_local extends repository { $fs = get_file_storage(); $sf = $fs->get_file($contextid, $filearea, $itemid, $filepath, $filename); $contents = $sf->get_content(); - $fp = fopen($dir.$file, 'w'); + $fp = fopen($path, 'w'); fwrite($fp,$contents); fclose($fp); - return $dir.$file; + return $path; } /** diff --git a/repository/remotemoodle/repository.class.php b/repository/remotemoodle/repository.class.php index 19c14cfccf..388e9e4d40 100644 --- a/repository/remotemoodle/repository.class.php +++ b/repository/remotemoodle/repository.class.php @@ -256,25 +256,13 @@ class repository_remotemoodle extends repository { $file = $services[1]; //filename ///create a temporary folder with a file - if (!file_exists($CFG->dataroot.'/temp/download')) { - mkdir($CFG->dataroot.'/temp/download/', 0777, true); - } - if (is_dir($CFG->dataroot.'/temp/download')) { - $dir = $CFG->dataroot.'/temp/download/'; - } - if (empty($file)) { - $file = uniqid('repo').'_'.time().'.tmp'; - } - if (file_exists($dir.$file)) { - $file = uniqid('m').$file; - } - + $path = $this->prepare_file($file); ///fill the file with the content - $fp = fopen($dir.$file, 'w'); + $fp = fopen($path, 'w'); fwrite($fp,$content); fclose($fp); - return $dir.$file; + return $path; } diff --git a/repository/s3/repository.class.php b/repository/s3/repository.class.php index c9c5e1658e..f01c526ca9 100644 --- a/repository/s3/repository.class.php +++ b/repository/s3/repository.class.php @@ -36,6 +36,7 @@ class repository_s3 extends repository { ); $tree[] = $folder; } + echo_fb($tree); } else { $contents = $this->s->getBucket($path); foreach ($contents as $file) { @@ -59,14 +60,9 @@ class repository_s3 extends repository { $arr = explode('/', $filepath); $bucket = $arr[0]; $filename = $arr[1]; - if (!file_exists($CFG->dataroot.'/repository/download')) { - mkdir($CFG->dataroot.'/repository/download/', 0777, true); - } - if(is_dir($CFG->dataroot.'/repository/download')) { - $dir = $CFG->dataroot.'/repository/download/'; - } - $this->s->getObject($bucket, $filename, $dir.$file); - return $dir.$file; + $path = $this->prepare_file($file); + $this->s->getObject($bucket, $filename, $path); + return $path; } // login public function check_login() { diff --git a/repository/webdav/repository.class.php b/repository/webdav/repository.class.php index 83f7a50d1d..ed959540fa 100644 --- a/repository/webdav/repository.class.php +++ b/repository/webdav/repository.class.php @@ -34,25 +34,13 @@ class repository_webdav extends repository { } public function get_file($url, $title) { global $CFG; - if (!file_exists($CFG->dataroot.'/temp/download')) { - mkdir($CFG->dataroot.'/temp/download/', 0777, true); - } - if (is_dir($CFG->dataroot.'/temp/download')) { - $dir = $CFG->dataroot.'/temp/download/'; - } - if (empty($file)) { - $file = uniqid('repo').'_'.time().'.tmp'; - } - if (file_exists($dir.$file)) { - $file = uniqid('m').$file; - } - + $path = $this->prepare_file($title); $buffer = ''; $this->wd->open(); $this->wd->get($url, $buffer); - $fp = fopen($dir.$file, 'wb'); + $fp = fopen($path, 'wb'); fwrite($fp, $buffer); - return $dir.$file; + return $path; } public function global_search() { return false;