From 693ef3a8af5fc37f7585327aa59f2d0b1632cfd1 Mon Sep 17 00:00:00 2001 From: Petr Skoda Date: Sun, 19 Sep 2010 15:50:18 +0000 Subject: [PATCH] MDL-24283 improved stored_file encapsulation, this should allow us to hack with stored_file/file_storage internals in the future without breaking BC --- .../util/helper/backup_file_manager.class.php | 2 +- lib/filestorage/file_storage.php | 45 +++++++++---------- lib/filestorage/stored_file.php | 9 ++-- 3 files changed, 29 insertions(+), 27 deletions(-) diff --git a/backup/util/helper/backup_file_manager.class.php b/backup/util/helper/backup_file_manager.class.php index 8cc292a6598..c5b0121f4df 100644 --- a/backup/util/helper/backup_file_manager.class.php +++ b/backup/util/helper/backup_file_manager.class.php @@ -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) . '/' . diff --git a/lib/filestorage/file_storage.php b/lib/filestorage/file_storage.php index e2331903e50..d7edac3d781 100644 --- a/lib/filestorage/file_storage.php +++ b/lib/filestorage/file_storage.php @@ -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); } /** diff --git a/lib/filestorage/stored_file.php b/lib/filestorage/stored_file.php index 7134b54605e..1cb2e78579c 100644 --- a/lib/filestorage/stored_file.php +++ b/lib/filestorage/stored_file.php @@ -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"; } /** -- 2.39.2