From 0034c42ddaf5952c4545d8ee875b218eddd65cc9 Mon Sep 17 00:00:00 2001 From: icewing Date: Fri, 18 Apr 2008 18:36:07 +0000 Subject: Marcus Povey * Unfinished filestore code git-svn-id: https://code.elgg.org/elgg/trunk@494 36083f99-b078-4883-b0ff-0f9b5a30f544 --- engine/lib/filestore.php | 299 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 299 insertions(+) create mode 100644 engine/lib/filestore.php (limited to 'engine/lib/filestore.php') diff --git a/engine/lib/filestore.php b/engine/lib/filestore.php new file mode 100644 index 000000000..3b5dd7543 --- /dev/null +++ b/engine/lib/filestore.php @@ -0,0 +1,299 @@ +getParameters(). + */ + abstract public function setParameters(array $parameters); + + } + + /** + * @class ElggDiskFilestore + * This class uses disk storage to save data. + * @author Marcus Povey + */ + class ElggDiskFilestore extends ElggFilestore + { + /** + * Directory root. + */ + private $dir_root; + + /** + * Default depth of file directory matrix + */ + private $matrix_depth = 5; + + /** + * Construct a disk filestore using the given directory root. + * + * @param string $directory_root Root directory, must end in "/" + */ + public function __construct($directory_root) + { + $this->dir_root = $directory_root; + } + + public function open(ElggFile $file, $mode) + { + // Try and create the directory + try { $this->make_directory_root($this->dir_root); } catch (Exception $e){} + + $name = $file->getFilename(); + $matrix = $this->make_file_matrix($name); + + switch ($mode) + { + case "read" : $mode = "r+b"; break; + case "write" : $mode = "w+b"; break; + case "append" : $mode = "a+b"; break; + default: throw new InvalidParameterException("Unrecognised file mode '$mode'"); + } + + return fopen($this->dir_root . $matrix . $name, $mode); + } + + public function write($f, $data) + { + return fwrite($f, $data); + } + + public function read($f, $length = 0, $offset = 0) + { + if ($offset) + $this->seek($f, $offset); + + return fread($f, $length); + } + + public function close($f) + { + return fclose($f); + } + + public function delete(ElggFile $file) + { + $name = $file->getFilename(); + $matrix = $this->make_file_matrix($name); + + $unlink = unlink($this->dir_root . $matrix . $name); + if ($unlink) + return $file->delete(); + + return false; + } + + public function seek($f, $position) + { + return fseek($f, $position); + } + + /** + * Make the directory root. + * + * @param string $dirroot + */ + protected function make_directory_root($dirroot) + { + if (!mkdir($dir, 0700, true)) + throw new IOException("Could not make $dirroot"); + + return true; + } + + /** + * Construct the filename matrix. + * + * @param string $filename + */ + protected function make_file_matrix($filename) + { + $matrix = ""; + + $len = strlen($ident); + if ($len>$this->matrix_depth) + $len = $this->matrix_depth; + + for ($n = 0; $n < strlen($ident); $n++) + $matrix .= $ident[$n] . "/"; + + return $matrix; + } + + public function getParameters() + { + return array("dir_root" => $this->dir_root); + } + + public function setParameters(array $parameters) + { + if (isset($parameters['dir_root'])) + { + $this->dir_root = $parameters['dir_root']; + return true; + } + + return false; + } + } + + /** + * @class ElggFile + * This class represents a physical file. + * @author Marcus Povey + */ + class ElggFile extends ElggObject + { + protected function initialise_attributes() + { + parent::initialise_attributes(); + + $this->attributes['subtype'] = "file"; + } + + function __construct($guid = null) + { + parent::__construct($guid); + + } + + // TODO: Save filestore & filestore parameters - getparameters, save them as name/value with type "$datastoreclassname" + + // TODO: Set name and optional description + + + //get datastore (save with object as meta/ load from create) + + // constrcut + + // initialise (set subtype to elggfile) + + // set name + + + + // read / write / open / close / delete + + // Get name + + + + // getFilestore + + + // if $filestore is blank, try and get from meta + // if meta not found or guid is null then get from default + + } + + + /// Variable holding the default datastore + $DEFAULT_FILE_STORE = NULL; + + /** + * Return the default filestore. + * + * @return ElggFilestore + */ + function get_default_filestore() + { + global $DEFAULT_FILE_STORE; + + return $DEFAULT_FILE_STORE; + } + + /** + * Set the default filestore for the system. + */ + function set_default_filestore(ElggFilestore $filestore) + { + global $DEFAULT_FILE_STORE; + + $DEFAULT_FILE_STORE = $filestore; + + return true; + } + + + // Now register a default filestore + set_default_filestore(new ElggDiskFilestore($CONFIG->dataroot)); +?> \ No newline at end of file -- cgit v1.2.3