/** * {@inheritdoc} */ public function write($key, $content) { $this->initialize(); $path = $this->computePath($key); $this->ensureDirectoryExists(\Gaufrette\Util\Path::dirname($path), true); if ($this->sftp->put($path, $content)) { return $this->sftp->size($path); } return false; }
/** * Returns the url for file * By default absolute path to file is returned * Otherwise when $options array contain key `relative` set as true, relative path will be returned * * @param $key * @param array $options * @return mixed */ public function getUrl($key, array $options = []) { $absolutePathPattern = ':protocol://:username@:host:pwd/:directory/:file'; $protocol = $this->ssl ? 'ftps' : 'ftp'; $pwd = ftp_pwd($this->connection); $url = strtr($absolutePathPattern, [':protocol' => $protocol, ':username' => $this->username, ':host' => $this->host, ':pwd' => $pwd, ':directory' => $this->directory, ':file' => $key]); if (isset($options['relative']) && $options['relative']) { $relativePathPattern = ':pwd/:directory/:file'; $url = strtr($relativePathPattern, [':pwd' => $pwd, ':directory' => $this->directory, ':file' => $key]); } return Path::normalize($url); }
/** * {@inheritdoc} */ public function keys() { $this->initialize(); $results = $this->sftp->listDirectory($this->directory, true); $files = array_map(array($this, 'computeKey'), $results['files']); $dirs = array(); foreach ($files as $file) { if ('.' !== ($dirname = \Gaufrette\Util\Path::dirname($file))) { $dirs[] = $dirname; } } $keys = array_merge($files, $dirs); sort($keys); return $keys; }
/** * {@inheritdoc} */ public function open(StreamMode $mode) { $baseDirPath = \Gaufrette\Util\Path::dirname($this->path); if ($mode->allowsWrite() && !is_dir($baseDirPath)) { @mkdir($baseDirPath, 0755, true); } try { $fileHandle = @fopen($this->path, $mode->getMode()); } catch (\Exception $e) { $fileHandle = false; } if (false === $fileHandle) { throw new \RuntimeException(sprintf('File "%s" cannot be opened', $this->path)); } $this->mode = $mode; $this->fileHandle = $fileHandle; return true; }
/** * Normalizes the given path * * @param string $path * * @return string */ protected function normalizePath($path) { $path = Util\Path::normalize($path); if (0 !== strpos($path, $this->directory)) { throw new \OutOfBoundsException(sprintf('The path "%s" is out of the filesystem.', $path)); } return $path; }
/** * Creates the specified directory and its parent directories. * * @param string $directory Directory to create * * @throws RuntimeException if the directory could not be created */ protected function createDirectory($directory) { // create parent directory if needed $parent = \Gaufrette\Util\Path::dirname($directory); if (!$this->isDir($parent)) { $this->createDirectory($parent); } // create the specified directory $created = ftp_mkdir($this->getConnection(), $directory); if (false === $created) { throw new \RuntimeException(sprintf('Could not create the \'%s\' directory.', $directory)); } }
/** * {@inheritdoc} */ public function keys() { $this->ensureBucketExists(); $list = $this->service->get_object_list($this->bucket); $keys = array(); foreach ($list as $file) { if ('.' !== \Gaufrette\Util\Path::dirname($file)) { $keys[] = \Gaufrette\Util\Path::dirname($file); } $keys[] = $file; } sort($keys); return $keys; }
/** * {@inheritdoc} */ public function keys() { $metadata = $this->client->getMetaData('/', true); if (!isset($metadata['contents'])) { return array(); } $keys = array(); foreach ($metadata['contents'] as $value) { $file = ltrim($value['path'], '/'); $keys[] = $file; if ('.' !== ($dirname = \Gaufrette\Util\Path::dirname($file))) { $keys[] = $dirname; } } sort($keys); return $keys; }
/** * Normalizes the given path * * @param string $path * * @return string */ public function normalizePath($path) { return Util\Path::normalize($path); }