MDL-24283 improved stored_file encapsulation, this should allow us to hack with store...
authorPetr Skoda <skodak@moodle.org>
Sun, 19 Sep 2010 15:50:18 +0000 (15:50 +0000)
committerPetr Skoda <skodak@moodle.org>
Sun, 19 Sep 2010 15:50:18 +0000 (15:50 +0000)
backup/util/helper/backup_file_manager.class.php
lib/filestorage/file_storage.php
lib/filestorage/stored_file.php

index 8cc292a..c5b0121 100644 (file)
@@ -72,7 +72,7 @@ class backup_file_manager {
         }
 
         $fs = get_file_storage();
-        $file = new stored_file($fs, $filerecorid);
+        $file = $fs->get_file_instance($filerecorid);
 
         // Calculate source and target paths (use same subdirs strategy for both)
         $targetfilepath = self::get_backup_storage_base_dir($backupid) . '/' .
index e233190..d7edac3 100644 (file)
@@ -89,17 +89,6 @@ class file_storage {
         }
     }
 
-    /**
-     * Returns location of filedir (file pool).
-     *
-     * Do not use, this method is intended for stored_file instances only!!!
-     *
-     * @return string pathname
-     */
-    public function get_filedir() {
-        return $this->filedir;
-    }
-
     /**
      * Calculates sha1 hash of unique full path name information.
      *
@@ -153,6 +142,16 @@ class file_storage {
         return $DB->record_exists('files', array('pathnamehash'=>$pathnamehash));
     }
 
+    /**
+     * Create instance of file class from database record.
+     *
+     * @param stdClass $file_record record from the files table
+     * @return stored_file instance of file abstraction class
+     */
+    public function get_file_instance(stdClass $file_record) {
+        return new stored_file($this, $file_record, $this->filedir);
+    }
+
     /**
      * Fetch file using local file id.
      *
@@ -166,7 +165,7 @@ class file_storage {
         global $DB;
 
         if ($file_record = $DB->get_record('files', array('id'=>$fileid))) {
-            return new stored_file($this, $file_record);
+            return $this->get_file_instance($file_record);
         } else {
             return false;
         }
@@ -182,7 +181,7 @@ class file_storage {
         global $DB;
 
         if ($file_record = $DB->get_record('files', array('pathnamehash'=>$pathnamehash))) {
-            return new stored_file($this, $file_record);
+            return $this->get_file_instance($file_record);
         } else {
             return false;
         }
@@ -269,7 +268,7 @@ class file_storage {
             if (!$includedirs and $file_record->filename === '.') {
                 continue;
             }
-            $result[$file_record->pathnamehash] = new stored_file($this, $file_record);
+            $result[$file_record->pathnamehash] = $this->get_file_instance($file_record);
         }
         return $result;
     }
@@ -364,9 +363,9 @@ class file_storage {
             $file_records = $DB->get_records_sql($sql, $params);
             foreach ($file_records as $file_record) {
                 if ($file_record->filename == '.') {
-                    $dirs[$file_record->pathnamehash] = new stored_file($this, $file_record);
+                    $dirs[$file_record->pathnamehash] = $this->get_file_instance($file_record);
                 } else {
-                    $files[$file_record->pathnamehash] = new stored_file($this, $file_record);
+                    $files[$file_record->pathnamehash] = $this->get_file_instance($file_record);
                 }
             }
             $result = array_merge($dirs, $files);
@@ -391,7 +390,7 @@ class file_storage {
                     if (substr_count($file_record->filepath, '/') !== $reqlevel) {
                         continue;
                     }
-                    $result[$file_record->pathnamehash] = new stored_file($this, $file_record);
+                    $result[$file_record->pathnamehash] = $this->get_file_instance($file_record);
                 }
             }
 
@@ -403,7 +402,7 @@ class file_storage {
 
             $file_records = $DB->get_records_sql($sql, $params);
             foreach ($file_records as $file_record) {
-                $result[$file_record->pathnamehash] = new stored_file($this, $file_record);
+                $result[$file_record->pathnamehash] = $this->get_file_instance($file_record);
             }
         }
 
@@ -435,7 +434,7 @@ class file_storage {
 
         $file_records = $DB->get_records('files', $conditions);
         foreach ($file_records as $file_record) {
-            $stored_file = new stored_file($this, $file_record);
+            $stored_file = $this->get_file_instance($file_record);
             $stored_file->delete();
         }
 
@@ -605,7 +604,7 @@ class file_storage {
             // update the existing directory with the new data
             $newrecord->id = $directory->get_id();
             $DB->update_record('files', $newrecord);
-            return new stored_file($this, $newrecord);
+            return $this->get_file_instance($newrecord);
         }
 
         try {
@@ -621,7 +620,7 @@ class file_storage {
 
         $this->create_directory($newrecord->contextid, $newrecord->component, $newrecord->filearea, $newrecord->itemid, $newrecord->filepath, $newrecord->userid);
 
-        return new stored_file($this, $newrecord);
+        return $this->get_file_instance($newrecord);
     }
 
     /**
@@ -768,7 +767,7 @@ class file_storage {
 
         $this->create_directory($newrecord->contextid, $newrecord->component, $newrecord->filearea, $newrecord->itemid, $newrecord->filepath, $newrecord->userid);
 
-        return new stored_file($this, $newrecord);
+        return $this->get_file_instance($newrecord);
     }
 
     /**
@@ -861,7 +860,7 @@ class file_storage {
 
         $this->create_directory($newrecord->contextid, $newrecord->component, $newrecord->filearea, $newrecord->itemid, $newrecord->filepath, $newrecord->userid);
 
-        return new stored_file($this, $newrecord);
+        return $this->get_file_instance($newrecord);
     }
 
     /**
index 7134b54..1cb2e78 100644 (file)
@@ -44,16 +44,20 @@ class stored_file {
     private $fs;
     /** @var object record from the files table */
     private $file_record;
+    /** @var string location of content files */
+    private $filedir;
 
     /**
      * Constructor, this constructor should be called ONLY from the file_storage class!
      *
      * @param file_storage $fs file  storage instance
      * @param object $file_record description of file
+     * @param string $filepool location of file directory with sh1 named content files
      */
-    public function __construct(file_storage $fs, stdClass $file_record) {
+    public function __construct(file_storage $fs, stdClass $file_record, $filedir) {
         $this->fs          = $fs;
         $this->file_record = clone($file_record); // prevent modifications
+        $this->filedir     = $filedir; // keep secret, do not expose!
     }
 
     /**
@@ -93,11 +97,10 @@ class stored_file {
      * @return string full path to pool file with file content
      **/
     protected function get_content_file_location() {
-        $filedir = $this->fs->get_filedir();
         $contenthash = $this->file_record->contenthash;
         $l1 = $contenthash[0].$contenthash[1];
         $l2 = $contenthash[2].$contenthash[3];
-        return "$filedir/$l1/$l2/$contenthash";
+        return "$this->filedir/$l1/$l2/$contenthash";
     }
 
     /**