123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480 |
- <?php
- class ElggFile extends \ElggObject {
-
- private $filestore;
-
- private $handle;
-
- protected function initializeAttributes() {
- parent::initializeAttributes();
- $this->attributes['subtype'] = "file";
- }
-
- public function __construct($row = null) {
- parent::__construct($row);
-
- $this->filestore = $this->getFilestore();
- }
-
- public function setFilename($name) {
- $this->filename = $name;
- }
-
- public function getFilename() {
- return $this->filename;
- }
-
- public function getFilenameOnFilestore() {
- return $this->filestore->getFilenameOnFilestore($this);
- }
-
- public function getFilestoreSize($prefix = '', $container_guid = 0) {
- if (!$container_guid) {
- $container_guid = $this->container_guid;
- }
- $fs = $this->getFilestore();
-
- return $fs->getSize($prefix, $container_guid);
- }
-
- public function getMimeType() {
- if ($this->mimetype) {
- return $this->mimetype;
- }
-
- }
-
- public function setMimeType($mimetype) {
- return $this->mimetype = $mimetype;
- }
-
- public function detectMimeType($file = null, $default = null) {
- $class = __CLASS__;
- if (!$file && isset($this) && $this instanceof $class) {
- $file = $this->getFilenameOnFilestore();
- }
- if (!is_readable($file)) {
- return false;
- }
- $mime = $default;
-
- if (function_exists('finfo_file') && defined('FILEINFO_MIME_TYPE')) {
- $resource = finfo_open(FILEINFO_MIME_TYPE);
- if ($resource) {
- $mime = finfo_file($resource, $file);
- }
- }
-
- if (!$mime && function_exists('mime_content_type')) {
- $mime = mime_content_type($file);
- }
- $original_filename = isset($this) && $this instanceof $class ? $this->originalfilename : basename($file);
- $params = array(
- 'filename' => $file,
- 'original_filename' => $original_filename,
- 'default' => $default,
- );
- return _elgg_services()->hooks->trigger('mime_type', 'file', $params, $mime);
- }
-
- public function setDescription($description) {
- $this->description = $description;
- }
-
- public function open($mode) {
- if (!$this->getFilename()) {
- throw new \IOException("You must specify a name before opening a file.");
- }
-
-
-
- if (
- ($mode != "read") &&
- ($mode != "write") &&
- ($mode != "append")
- ) {
- $msg = "Unrecognized file mode '" . $mode . "'";
- throw new \InvalidParameterException($msg);
- }
-
- $fs = $this->getFilestore();
-
-
-
- $this->handle = $fs->open($this, $mode);
- return $this->handle;
- }
-
- public function write($data) {
- $fs = $this->getFilestore();
- return $fs->write($this->handle, $data);
- }
-
- public function read($length, $offset = 0) {
- $fs = $this->getFilestore();
- return $fs->read($this->handle, $length, $offset);
- }
-
- public function grabFile() {
- $fs = $this->getFilestore();
- return $fs->grabFile($this);
- }
-
- public function close() {
- $fs = $this->getFilestore();
- if ($fs->close($this->handle)) {
- $this->handle = null;
- return true;
- }
- return false;
- }
-
- public function delete() {
- $fs = $this->getFilestore();
-
- $result = $fs->delete($this);
-
- if ($this->getGUID() && $result) {
- $result = parent::delete();
- }
-
- return $result;
- }
-
- public function seek($position) {
- $fs = $this->getFilestore();
-
- return $fs->seek($this->handle, $position);
- }
-
- public function tell() {
- $fs = $this->getFilestore();
- return $fs->tell($this->handle);
- }
-
- public function getSize() {
- return $this->filestore->getFileSize($this);
- }
-
- public function size() {
- elgg_deprecated_notice("Use \ElggFile::getSize() instead of \ElggFile::size()", 1.9);
- return $this->getSize();
- }
-
- public function eof() {
- $fs = $this->getFilestore();
- return $fs->eof($this->handle);
- }
-
- public function exists() {
- $fs = $this->getFilestore();
- return $fs->exists($this);
- }
-
- public function setFilestore(\ElggFilestore $filestore) {
- $this->filestore = $filestore;
- }
-
- protected function getFilestore() {
-
- if ($this->filestore) {
- return $this->filestore;
- }
-
-
-
-
- if ($this->guid) {
- $options = array(
- 'guid' => $this->guid,
- 'where' => array("n.string LIKE 'filestore::%'"),
- );
- $mds = elgg_get_metadata($options);
- $parameters = array();
- foreach ($mds as $md) {
- list( , $name) = explode("::", $md->name);
- if ($name == 'filestore') {
- $filestore = $md->value;
- }
- $parameters[$name] = $md->value;
- }
- }
-
-
- if (isset($filestore)) {
- if (!class_exists($filestore)) {
- $msg = "Unable to load filestore class " . $filestore . " for file " . $this->guid;
- throw new \ClassNotFoundException($msg);
- }
- $this->filestore = new $filestore();
- $this->filestore->setParameters($parameters);
-
- }
-
- if (!$this->filestore) {
- $this->filestore = get_default_filestore();
- }
- return $this->filestore;
- }
-
- public function save() {
- if (!parent::save()) {
- return false;
- }
-
- $params = $this->filestore->getParameters();
- foreach ($params as $k => $v) {
- $this->setMetadata("filestore::$k", $v);
- }
-
- $this->setMetadata("filestore::filestore", get_class($this->filestore));
- return true;
- }
-
- public function __sleep() {
- return array_diff(array_keys(get_object_vars($this)), array(
-
-
- 'filestore',
-
- 'handle',
- ));
- }
-
- public function __wakeup() {
- $this->getFilestore();
- }
- }
|