00001 <?php
00002
00023 class ElggFile extends ElggObject {
00025 private $filestore;
00026
00028 private $handle;
00029
00035 protected function initializeAttributes() {
00036 parent::initializeAttributes();
00037
00038 $this->attributes['subtype'] = "file";
00039 }
00040
00046 public function __construct($guid = null) {
00047 parent::__construct($guid);
00048
00049
00050 $this->filestore = $this->getFilestore();
00051 }
00052
00060 public function setFilename($name) {
00061 $this->filename = $name;
00062 }
00063
00069 public function getFilename() {
00070 return $this->filename;
00071 }
00072
00079 public function getFilenameOnFilestore() {
00080 return $this->filestore->getFilenameOnFilestore($this);
00081 }
00082
00091 public function getFilestoreSize($prefix = '', $container_guid = 0) {
00092 if (!$container_guid) {
00093 $container_guid = $this->container_guid;
00094 }
00095 $fs = $this->getFilestore();
00096
00097 return $fs->getSize($prefix, $container_guid);
00098 }
00099
00105 public function getMimeType() {
00106 if ($this->mimetype) {
00107 return $this->mimetype;
00108 }
00109
00110
00111 }
00112
00120 public function setMimeType($mimetype) {
00121 return $this->mimetype = $mimetype;
00122 }
00123
00135 public function detectMimeType($file = null, $default = null) {
00136 if (!$file) {
00137 if (isset($this) && $this->filename) {
00138 $file = $this->filename;
00139 } else {
00140 return false;
00141 }
00142 }
00143
00144 $mime = false;
00145
00146
00147 if (function_exists('finfo_file') && defined('FILEINFO_MIME_TYPE')) {
00148 $resource = finfo_open(FILEINFO_MIME_TYPE);
00149 if ($resource) {
00150 $mime = finfo_file($resource, $file);
00151 }
00152 }
00153
00154
00155 if (!$mime && function_exists('mime_content_type')) {
00156 $mime = mime_content_type($file);
00157 }
00158
00159
00160 if (!$mime) {
00161 return $default;
00162 }
00163
00164 return $mime;
00165 }
00166
00174 public function setDescription($description) {
00175 $this->description = $description;
00176 }
00177
00187 public function open($mode) {
00188 if (!$this->getFilename()) {
00189 throw new IOException(elgg_echo('IOException:MissingFileName'));
00190 }
00191
00192
00193
00194
00195
00196 if (
00197 ($mode != "read") &&
00198 ($mode != "write") &&
00199 ($mode != "append")
00200 ) {
00201 $msg = elgg_echo('InvalidParameterException:UnrecognisedFileMode', array($mode));
00202 throw new InvalidParameterException($msg);
00203 }
00204
00205
00206 $fs = $this->getFilestore();
00207
00208
00209
00210
00211
00212 $this->handle = $fs->open($this, $mode);
00213
00214 return $this->handle;
00215 }
00216
00224 public function write($data) {
00225 $fs = $this->getFilestore();
00226
00227 return $fs->write($this->handle, $data);
00228 }
00229
00238 public function read($length, $offset = 0) {
00239 $fs = $this->getFilestore();
00240
00241 return $fs->read($this->handle, $length, $offset);
00242 }
00243
00249 public function grabFile() {
00250 $fs = $this->getFilestore();
00251 return $fs->grabFile($this);
00252 }
00253
00259 public function close() {
00260 $fs = $this->getFilestore();
00261
00262 if ($fs->close($this->handle)) {
00263 $this->handle = NULL;
00264
00265 return true;
00266 }
00267
00268 return false;
00269 }
00270
00276 public function delete() {
00277 $fs = $this->getFilestore();
00278 if ($fs->delete($this)) {
00279 return parent::delete();
00280 }
00281 }
00282
00290 public function seek($position) {
00291 $fs = $this->getFilestore();
00292
00293
00294 return $fs->seek($this->handle, $position);
00295 }
00296
00302 public function tell() {
00303 $fs = $this->getFilestore();
00304
00305 return $fs->tell($this->handle);
00306 }
00307
00313 public function size() {
00314 return $this->filestore->getFileSize($this);
00315 }
00316
00322 public function eof() {
00323 $fs = $this->getFilestore();
00324
00325 return $fs->eof($this->handle);
00326 }
00327
00333 public function exists() {
00334 $fs = $this->getFilestore();
00335
00336 return $fs->exists($this);
00337 }
00338
00346 public function setFilestore(ElggFilestore $filestore) {
00347 $this->filestore = $filestore;
00348 }
00349
00359 protected function getFilestore() {
00360
00361 if ($this->filestore) {
00362 return $this->filestore;
00363 }
00364
00365
00366
00367
00368
00369 if ($this->guid) {
00370 $options = array(
00371 'guid' => $this->guid,
00372 'where' => array("n.string LIKE 'filestore::%'"),
00373 );
00374
00375 $mds = elgg_get_metadata($options);
00376
00377 $parameters = array();
00378 foreach ($mds as $md) {
00379 list($foo, $name) = explode("::", $md->name);
00380 if ($name == 'filestore') {
00381 $filestore = $md->value;
00382 }
00383 $parameters[$name] = $md->value;
00384 }
00385 }
00386
00387
00388
00389 if (isset($filestore)) {
00390 if (!class_exists($filestore)) {
00391 $msg = elgg_echo('ClassNotFoundException:NotFoundNotSavedWithFile',
00392 array($filestore, $this->guid));
00393 throw new ClassNotFoundException($msg);
00394 }
00395
00396 $this->filestore = new $filestore();
00397 $this->filestore->setParameters($parameters);
00398
00399 }
00400
00401
00402 if (!$this->filestore) {
00403 $this->filestore = get_default_filestore();
00404 }
00405
00406 return $this->filestore;
00407 }
00408
00419 public function save() {
00420 if (!parent::save()) {
00421 return false;
00422 }
00423
00424
00425 $params = $this->filestore->getParameters();
00426 foreach ($params as $k => $v) {
00427 $this->setMetaData("filestore::$k", $v);
00428 }
00429
00430
00431 $this->setMetaData("filestore::filestore", get_class($this->filestore));
00432
00433 return true;
00434 }
00435 }