]> git.mjollnir.org Git - moodle.git/commitdiff
"REPOSITORY/MDL-13766, create prepare_file function, it will prepare temp file for...
authordongsheng <dongsheng>
Mon, 16 Mar 2009 02:16:50 +0000 (02:16 +0000)
committerdongsheng <dongsheng>
Mon, 16 Mar 2009 02:16:50 +0000 (02:16 +0000)
repository/alfresco/repository.class.php
repository/flickr/repository.class.php
repository/flickr_public/repository.class.php
repository/googledocs/repository.class.php
repository/lib.php
repository/local/repository.class.php
repository/remotemoodle/repository.class.php
repository/s3/repository.class.php
repository/webdav/repository.class.php

index 63700e2759e297957b8d04fa8e492c248f48d7f4..6d06936374b2ead6c791e364ce87bd71680c2de3 100755 (executable)
@@ -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() {
index a6c8af3490448e82198886d43c38917744fcd209..ff81b1674046c2f0fc0075ca03029ee9a7013f95 100755 (executable)
@@ -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;
     }
 
     /**
index 93479fb5db79111bd128026d531219edef4cf142..579685254606a1a6961008f12126f2dd893b7d6b 100644 (file)
@@ -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;
     }
 
     /**
index 18abc15f7879bc08bc30c9d7daf5c33849172cc5..b8a2c2a0091358813a8844cc8602f0a8cd1f1824 100644 (file)
@@ -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() {
index ee4b75109966a02b4f6af425a73e8572dab5a408..ecdcd1f0a24c62ac8f4e6d8ae43577f172e310c0 100644 (file)
@@ -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;
     }
 
     /**
index 486be34485ac90ca7ca00d810face6c762b9211f..ae56f7da70d63cc913c8ec73c752757b79d5f555 100755 (executable)
@@ -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;
     }
 
     /**
index 19c14cfccf981c0f6fcc91f57ddcd855855bbe1f..388e9e4d407917e1ad84b7a4cfe4e97d17cce51f 100644 (file)
@@ -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;
 
     }
 
index c9c5e1658e9d1bd9a564ce21772dd8862970a6dc..f01c526ca9fc3032fdddb14f6bcbf501320208a0 100644 (file)
@@ -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() {
index 83f7a50d1d9756d14dcb0a5a2b7f3fc78a60d51a..ed959540fa0110537129007ae67e11bed8644ded 100644 (file)
@@ -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;