Esempio n. 1
0
 /**
  * Get the mime encoding (e.g. "binary" or "us-ascii" or "utf-8") of the file.
  *
  * @return string
  */
 public function getMIMEEncoding()
 {
     $adapter = $this->file->internalPathname()->localAdapter();
     if ($adapter instanceof MimeAwareAdapterInterface) {
         return $adapter->getMimeEncoding($this->file->internalPathname());
     }
     return Util::executeFunction(function () {
         return finfo_buffer(Util::getFileInfo(), $this->file->getContents(), FILEINFO_MIME_ENCODING);
     }, 'Filicious\\Exception\\PluginException', 0, 'Could not determine mime encoding');
 }
Esempio n. 2
0
 /**
  * Calculate the space.
  *
  * @param string $algorithm
  *
  * @return string
  */
 public function getSpace($algorithm, $binary = false)
 {
     $adapter = $this->file->internalPathname()->localAdapter();
     if ($adapter instanceof SpaceAwareAdapterInterface) {
         return $adapter->getSpace($this->file->internalPathname(), $algorithm, $binary);
     }
     $file = $this->getFile();
     return Util::executeFunction(function () use($algorithm, $file, $binary) {
         return space($algorithm, $file->getContents(), $binary);
     }, 'Filicious\\Exception\\PluginException', 0, 'Could not calculate %s space of file %s', $algorithm, $file->getPathname());
 }
Esempio n. 3
0
 /**
  * {@inheritdoc}
  */
 public function resolveLocal(Pathname $pathname, &$localAdapter, &$local)
 {
     $path = $pathname->full();
     do {
         if (isset($this->mounts[$path])) {
             $localAdapter = $this->mounts[$path];
             $local = substr($pathname->full(), strlen($path));
             return $this;
         }
     } while ('/' !== ($path = Util::dirname($path)));
     throw new AdapterException('No mount found for ' . $pathname->full());
 }
Esempio n. 4
0
 /**
  * Set the date and time at which the file was modified and / or accessed
  * last time.
  * The given $time and $atime parameters are converted to \DateTime objects
  * via Util::createDateTime, with the one exception, that if $atime parameter
  * is set to null, then the date and time given in the $time parameter will
  * be used for $atime.
  *
  * @param int|string|\DateTime $modifyTime The new modify time
  * @param int|string|\DateTime $accessTime The new access time; If null then $time will be used
  * @param bool  $create     Whether to create the file, if it does not already
  *                          exists
  *
  * @return void
  *
  * @throws FileNotFoundException If the file does not exists and $create is set
  *      to false
  *
  * @api
  */
 public function touch($modifyTime = 'now', $accessTime = null, $create = true)
 {
     $eventDispatcher = $this->filesystem->getEventDispatcher();
     if ($eventDispatcher) {
         $exists = $this->pathname->rootAdapter()->exists($this->pathname);
     } else {
         $exists = null;
     }
     $modifyTime = Util::createDateTime($modifyTime);
     $accessTime = $accessTime === null ? $modifyTime : Util::createDateTime($accessTime);
     $this->pathname->rootAdapter()->touch($this->pathname, $modifyTime, $accessTime, $create);
     if ($eventDispatcher) {
         $event = new TouchEvent($this->filesystem, $this, $modifyTime, $accessTime, $create && !$exists);
         $eventDispatcher->dispatch(FiliciousEvents::TOUCH, $event);
     }
 }
Esempio n. 5
0
 /**
  * {@inheritdoc}
  */
 public function getIterator(Pathname $pathname, array $filter)
 {
     if (Util::hasBit($filter, File::LIST_RECURSIVE)) {
         return new \RecursiveIteratorIterator(new RecursivePathnameIterator($pathname, $filter));
     } else {
         return new PathnameIterator($pathname, $filter);
     }
 }
Esempio n. 6
0
 /**
  * Notify about config changes.
  */
 public function notifyConfigChange()
 {
     $host = $this->config->get(FilesystemConfig::HOST);
     $port = $this->config->get(FilesystemConfig::PORT);
     $username = $this->config->get(FilesystemConfig::USERNAME);
     $password = $this->config->get(FilesystemConfig::PASSWORD);
     $key = $this->config->get(self::CONFIG_KEY);
     $keyFile = $this->config->get(self::CONFIG_KEY_FILE);
     $basepath = $this->config->get('/' . FilesystemConfig::BASEPATH);
     if ($basepath) {
         $basepath = Util::normalizePath($basepath);
     }
     $connectUrl = $username;
     if ($keyFile) {
         $connectUrl .= ':' . crypt($keyFile . $password);
     } else {
         if ($key) {
             $connectUrl .= ':' . crypt($key . $password);
         } else {
             if ($password) {
                 $connectUrl .= ':' . crypt($password);
             }
         }
     }
     $connectUrl .= '@' . $host;
     if ($port) {
         $connectUrl .= ':' . $port;
     }
     $connectUrl .= $basepath;
     if ($this->connectionURL != $connectUrl) {
         if ($this->connection) {
             $this->connection->disconnect();
             $this->connection = null;
         }
         $this->connectionURL = $connectUrl;
     }
 }
Esempio n. 7
0
 /**
  * {@inheritdoc}
  */
 public function getMimeEncoding(Pathname $pathname)
 {
     Validator::checkFile($pathname);
     $self = $this;
     return Util::executeFunction(function () use($pathname, $self) {
         return finfo_file(Util::getFileInfo(), $self->getBasePath() . $pathname->local(), FILEINFO_MIME_ENCODING);
     }, 'Filicious\\Exception\\AdapterException', 0, 'Could not get mime encoding of %s.', $pathname);
 }
Esempio n. 8
0
 /**
  * Get a file object for the specific file.
  *
  * @param string $path
  *
  * @return File
  */
 public function getFile($path = null)
 {
     // cheap recreate of File object
     if ($path instanceof Pathname && $path->rootAdapter() == $this->adapter) {
         return new File($path);
     }
     $pathname = implode('/', Util::getPathnameParts($path));
     strlen($pathname) && ($pathname = '/' . $pathname);
     return new File(new Pathname($this->adapter, $pathname));
 }
Esempio n. 9
0
 protected function evaluateFilters($filters)
 {
     if (Util::isTraversable($filters)) {
         // search for File::LIST_RECURSIVE
         foreach ($filters as $arg) {
             if (is_int($arg)) {
                 if ($this->bitmask === null) {
                     $this->bitmask = $arg;
                 } else {
                     $this->bitmask |= $arg;
                 }
             } else {
                 if (is_string($arg)) {
                     $this->globs[] = Util::normalizePath($arg);
                 } else {
                     if (is_callable($arg)) {
                         $this->callables[] = $arg;
                     } else {
                         if (is_array($arg)) {
                             $this->evaluateFilters($arg);
                         } else {
                             if (is_object($arg)) {
                                 $type = get_class($arg);
                             } else {
                                 ob_start();
                                 var_dump($arg);
                                 $type = ob_get_contents();
                                 ob_end_clean();
                             }
                             throw new Exception(sprintf('Can not use %s as listing filter.', $type));
                         }
                     }
                 }
             }
         }
     }
 }