/** * 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'); }
/** * 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()); }
/** * {@inheritdoc} */ public function moveFrom(Pathname $dstPathname, Pathname $srcPathname, $flags) { $dstParentPathname = $dstPathname->parent(); if ($flags & File::OPERATION_PARENTS) { $dstParentPathname->localAdapter()->createDirectory($dstParentPathname, true); } else { if (!$dstParentPathname->localAdapter()->isDirectory($dstParentPathname)) { throw new NotADirectoryException($dstParentPathname); } } $dstExists = $this->exists($dstPathname); $srcIsDirectory = $srcPathname->localAdapter()->isDirectory($srcPathname); $dstIsDirectory = $this->isDirectory($dstPathname); // target not exists if (!$dstExists) { if ($srcIsDirectory) { $dstIsDirectory = true; } else { $dstIsDirectory = false; } // continue move operation } else { if (!$srcIsDirectory && $dstIsDirectory) { // replace directory with file if (!($flags & File::OPERATION_REJECT) && $flags & File::OPERATION_REPLACE) { $this->delete($dstPathname, true, false); $dstIsDirectory = false; // continue move operation } else { if ($flags & File::OPERATION_MERGE) { $dstInsidePathname = $dstPathname->child($srcPathname); $srcPathname->localAdapter()->moveTo($srcPathname, $dstInsidePathname, $flags); return $this; } else { throw new FileOverwriteDirectoryException($srcPathname, $dstPathname); } } } else { if ($srcIsDirectory && !$dstIsDirectory) { if (!($flags & File::OPERATION_REJECT) && $flags & File::OPERATION_REPLACE) { $this->delete($dstPathname, false, false); $this->createDirectory($dstPathname, false); $dstIsDirectory = true; // continue move operation } else { throw new DirectoryOverwriteFileException($srcPathname, $dstPathname); } } } } // move directory -> directory if ($srcIsDirectory && $dstIsDirectory) { // replace target directory if (!$dstExists || !($flags & File::OPERATION_REJECT) && $flags & File::OPERATION_REPLACE) { if ($dstExists) { $this->delete($dstPathname, true, false); } $flags |= File::OPERATION_RECURSIVE; // continue recursive move } // recursive merge directories if ($flags & File::OPERATION_RECURSIVE) { // native move if supported $dstClass = new \ReflectionClass($this); $srcClass = new \ReflectionClass($srcPathname->localAdapter()); if ($dstClass->getName() == $srcClass->getName() || $dstClass->isSubclassOf($srcClass) || $srcClass->isSubclassOf($dstClass)) { if ($this->nativeMove($srcPathname, $dstPathname)) { return $this; } } $iterator = $srcPathname->localAdapter()->getIterator($srcPathname, array()); /** @var Pathname $srcChildPathname */ foreach ($iterator as $srcChildPathname) { $srcPathname->localAdapter()->getRootAdapter()->moveTo($srcChildPathname, $this, $dstPathname->child($srcChildPathname), $flags); } } else { throw new DirectoryOverwriteDirectoryException($srcPathname, $dstPathname); } } else { if (!$srcIsDirectory && !$dstIsDirectory) { if (!$dstExists || !($flags & File::OPERATION_REJECT) && $flags & File::OPERATION_REPLACE) { // native move if supported $dstClass = new \ReflectionClass($this); $srcClass = new \ReflectionClass($srcPathname->localAdapter()); if ($dstClass->getName() == $srcClass->getName() || $dstClass->isSubclassOf($srcClass) || $srcClass->isSubclassOf($dstClass)) { if ($this->nativeMove($srcPathname, $dstPathname)) { return $this; } } // stream move return Util::executeFunction(function () use($srcPathname, $dstPathname) { $srcStream = $srcPathname->localAdapter()->getStream($srcPathname); $srcStream->open(new StreamMode('rb')); $dstStream = $this->getStream($dstPathname); $dstStream->open(new StreamMode('wb')); $result = stream_copy_to_stream($srcStream->getResource(), $dstStream->getResource()); $srcStream->close(); $dstStream->close(); return $result; }, 0, 'Could not move %s to %s.', $srcPathname, $dstPathname); } } else { throw new FilesystemException('Illegal state!'); } } }
/** * {@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); }