public function listContents($directory = '', $recursive = false) { $filter = function (&$value, $path) use($directory) { if (Util::dirname($path) !== $directory) { $value = null; } }; $list = $this->client->lists($directory, $recursive ? null : $filter); return $list; }
public function rename($path, $newpath) { $location = $this->applyPathPrefix($path); $destination = $this->applyPathPrefix($newpath); $parentDirectory = $this->applyPathPrefix(Util::dirname($newpath)); if (!$this->ensureDirectory($parentDirectory)) { return false; } return rename($location, $destination); }
/** * Checks that a rename is valid. * * @param string $source * @param string $dest * * @return bool */ protected function isValidRename($source, $dest) { $adapter = $this->filesystem->getAdapter(); if (!$adapter->has($source)) { throw new FileNotFoundException($source); } $subdir = Util::dirname($dest); if (strlen($subdir) && !$adapter->has($subdir)) { throw new FileNotFoundException($source); } if (!$adapter->has($dest)) { return true; } return $this->compareTypes($source, $dest); }
/** * {@inheritdoc} */ public function write($path, $contents, Config $config) { $location = $this->applyPathPrefix($path); $dirname = Util::dirname($path); if (!empty($dirname) && !$this->has($dirname)) { $this->createDir($dirname, $config); } if (!$this->archive->addFromString($location, $contents)) { return false; } $result = compact('path', 'contents'); if ($config && $config->get('visibility')) { throw new LogicException(get_class($this) . ' does not support visibility settings.'); } return $result; }
/** * @inheritdoc */ public function listContents($directory = '', $recursive = false) { $directory = Util::normalizePath($directory); $contents = $this->getAdapter()->listContents($directory, $recursive); $filter = function (array $entry) use($directory, $recursive) { if (empty($entry['path']) && $entry['path'] !== '0') { return false; } if ($recursive) { return $directory === '' || strpos($entry['path'], $directory . '/') === 0; } return Util::dirname($entry['path']) === $directory; }; $mapper = function (array $entry) { return $entry + Util::pathinfo($entry['path']); }; $listing = array_values(array_map($mapper, array_filter($contents, $filter))); usort($listing, function ($a, $b) { return strcasecmp($a['path'], $b['path']); }); return $listing; }
/** * {@inheritdoc} */ protected function normalizeObject(DataObject $object) { $name = $object->getName(); $name = $this->removePathPrefix($name); $mimetype = explode('; ', $object->getContentType()); return ['type' => in_array('application/directory', $mimetype) ? 'dir' : 'file', 'dirname' => Util::dirname($name), 'path' => $name, 'timestamp' => strtotime($object->getLastModified()), 'mimetype' => reset($mimetype), 'size' => $object->getContentLength()]; }
/** * Return parent directory path * * @param string $path file path * @return string **/ protected function _dirname($path) { return Util::dirname($path) ?: '/'; }
/** * List the filesystem contents. * * @param string $directory * @param bool $recursive * * @return array contents */ public function listContents($directory = '', $recursive = false) { $directory = Util::normalizePath($directory); $contents = $this->adapter->listContents($directory, $recursive); $mapper = function ($entry) use($directory, $recursive) { $entry = $entry + Util::pathinfo($entry['path']); if (!empty($directory) && strpos($entry['path'], $directory) === false) { return false; } if ($recursive === false && Util::dirname($entry['path']) !== $directory) { return false; } return $entry; }; return array_values(array_filter(array_map($mapper, $contents))); }
/** * Check if the entry is a direct child of the directory. * * @param $entry * * @return bool */ private function isDirectChild(array $entry) { return Util::dirname($entry['path']) === $this->directory; }
/** * {@inheritdoc} */ public function has($path) { if (array_key_exists($path, $this->cache)) { return $this->cache[$path] !== false; } if ($this->isComplete(Util::dirname($path), false)) { return false; } }
/** * Normalize a result from Google * * @param string $object * @param string $path * @return array file metadata */ protected function normalizeObject($object, $path = null) { $newObject = array(); foreach ((array) $object as $key => $value) { if (substr($key, 0, 3) == "*") { $key = substr($key, 3); } $newObject[$key] = $value; } $object = $newObject; if (isset($object["dirname"])) { $result = array('path' => $object['dirname'] . "/" . $object['id']); } else { $result = array('path' => $object['id']); } if (isset($object['modifiedDate'])) { $result['timestamp'] = strtotime($object['modifiedDate']); } $result["basename"] = $object["title"]; if ($object['mimeType'] == 'application/vnd.google-apps.folder') { $result['type'] = 'dir'; $result['path'] = rtrim($result['path'], '/'); $result['dirname'] = Util::dirname($result['path']); return $result; } $result = array_merge($result, Util::map($object, static::$resultMap), array('type' => 'file')); $result['dirname'] = Util::dirname($result['path']); if (isset($result['contents'])) { $result['contents'] = (string) $result['contents']; } return $result; }
/** * @inheritdoc */ public function listContents($directory = '', $recursive = false) { $directory = Util::normalizePath($directory); $adapter = $this->getAdapter(); $separator = $adapter instanceof Local ? DIRECTORY_SEPARATOR : '/'; $contents = $adapter->listContents($directory, $recursive); $mapper = function ($entry) use($directory, $recursive, $separator) { if (strlen($entry['path']) === 0 || !empty($directory) && strpos($entry['path'], $directory . $separator) === false || $recursive === false && Util::dirname($entry['path']) !== $directory) { return false; } return $entry + Util::pathinfo($entry['path']); }; $listing = array_values(array_filter(array_map($mapper, $contents))); usort($listing, function ($a, $b) { return strcasecmp($a['path'], $b['path']); }); return $listing; }
public function writeStream($path, $resource, $config = null) { $this->ensureDirectory(Util::dirname($path)); $config = Util::ensureConfig($config); if (!ftp_fput($this->getConnection(), $path, $resource, FTP_BINARY)) { return false; } if ($visibility = $config->get('visibility')) { $this->setVisibility($path, $visibility); } return compact('path', 'visibility'); }
/** * Ensures that the sub-directories of a directory exist. * * @param string $directory The directory. * @param Config|null $config Optionl configuration. * * @return bool True on success, false on failure. */ protected function ensureSubDirs($directory, Config $config = null) { $config = $config ?: new Config(); return $this->createDir(Util::dirname($directory), $config); }
/** * Upload|Update item * * @param string $path * @param string|resource $contents * @param Config $config * * @return array|false item info array */ protected function upload($path, $contents, Config $config) { list($parentId, $fileName) = $this->splitPath($path); $mode = 'update'; $mime = $config->get('mimetype'); $srcFile = $this->getFileObject($path); $file = new Google_Service_Drive_DriveFile(); if (!$srcFile) { $mode = 'insert'; $file->setName($fileName); $file->setParents([$parentId]); } $isResource = false; if (is_resource($contents)) { $fstat = @fstat($contents); if (!empty($fstat['size'])) { $isResource = true; } if (!$isResource) { $contents = stream_get_contents($contents); } } if ($isResource) { // set chunk size (max: 100MB) $chunkSizeBytes = 100 * 1024 * 1024; $memory = $this->getIniBytes('memory_limit'); if ($memory) { $chunkSizeBytes = min([$chunkSizeBytes, intval($memory / 4 / 256) * 256]); } if ($fstat['size'] < $chunkSizeBytes) { $isResource = false; $contents = stream_get_contents($contents); } } if (!$mime) { $mime = Util::guessMimeType($fileName, $isResource ? '' : $contents); } $file->setMimeType($mime); if ($isResource) { $client = $this->service->getClient(); // Call the API with the media upload, defer so it doesn't immediately return. $client->setDefer(true); if ($mode === 'insert') { $request = $this->service->files->create($file, ['fields' => self::FETCHFIELDS_GET]); } else { $request = $this->service->files->update($srcFile->getId(), $file, ['fields' => self::FETCHFIELDS_GET]); } // Create a media file upload to represent our upload process. $media = new Google_Http_MediaFileUpload($client, $request, $mime, null, true, $chunkSizeBytes); $media->setFileSize($fstat['size']); // Upload the various chunks. $status will be false until the process is // complete. $status = false; $handle = $contents; while (!$status && !feof($handle)) { // read until you get $chunkSizeBytes from TESTFILE // fread will never return more than 8192 bytes if the stream is read buffered and it does not represent a plain file // An example of a read buffered file is when reading from a URL $chunk = $this->readFileChunk($handle, $chunkSizeBytes); $status = $media->nextChunk($chunk); } // The final value of $status will be the data from the API for the object // that has been uploaded. if ($status != false) { $obj = $status; } $client->setDefer(false); } else { $params = ['data' => $contents, 'uploadType' => 'media', 'fields' => self::FETCHFIELDS_GET]; if ($mode === 'insert') { $obj = $this->service->files->create($file, $params); } else { $obj = $this->service->files->update($srcFile->getId(), $file, $params); } } if ($obj instanceof Google_Service_Drive_DriveFile) { $this->cacheFileObjects[$obj->getId()] = $obj; if ($mode === 'insert') { $this->cacheFileObjects[$fileName] = $obj; } $result = $this->normaliseObject($obj, Util::dirname($path)); if ($visibility = $config->get('visibility')) { if ($this->setVisibility($path, $visibility)) { $result['visibility'] = $visibility; } } return $result; } return false; }
/** * Builds the normalized output array from a Blob object. * * @param string $path * @param BlobProperties $properties * * @return array */ protected function normalizeBlobProperties($path, BlobProperties $properties) { if (substr($path, -1) === '/') { return ['type' => 'dir', 'path' => $this->removePathPrefix(rtrim($path, '/'))]; } $path = $this->removePathPrefix($path); return ['path' => $path, 'timestamp' => (int) $properties->getLastModified()->format('U'), 'dirname' => Util::dirname($path), 'mimetype' => $properties->getContentType(), 'size' => $properties->getContentLength(), 'type' => 'file']; }
/** * Upload a file. * * @param string $path * @param string|resource $contents * @param Config $config * @return bool */ public function upload($path, $contents, Config $config) { $connection = $this->getConnection(); $this->ensureDirectory(Util::dirname($path)); $config = Util::ensureConfig($config); if (!$connection->put($path, $contents, SFTP::SOURCE_STRING)) { return false; } if ($config && ($visibility = $config->get('visibility'))) { $this->setVisibility($path, $visibility); } return true; }
/** * Normalize a result from OSS. * * @param OSSObject $object * * @return array file metadata */ protected function normalizeObject(OSSObject $object) { $name = $object->getKey(); $name = $this->removePathPrefix($name); $mimetype = explode('; ', $object->getContentType()); return ['type' => 'file', 'dirname' => Util::dirname($name), 'path' => $name, 'timestamp' => $object->getLastModified()->getTimestamp(), 'mimetype' => reset($mimetype), 'size' => $object->getContentLength()]; }
/** * {@inheritdoc} */ public function getDirname() { return Util::dirname($this->path); }
public function write($path, $contents, $config = null) { $connection = $this->getConnection(); $this->ensureDirectory(Util::dirname($path)); $config = Util::ensureConfig($config); if (!$connection->put($path, $contents, NET_SFTP_STRING)) { return false; } if ($config && ($visibility = $config->get('visibility'))) { $this->setVisibility($path, $visibility); } return compact('contents', 'visibility'); }
/** * Check whether an object has been cached * * @param string $path * @return boolean cached boolean */ public function has($path) { if (!isset($this->cache[$path])) { return $this->isComplete(Util::dirname($path), false) ? false : null; } return $this->cache[$path] !== false; }
/** * Returns a dictionary of object metadata from an object. * * @param \Google_Service_Storage_StorageObject $object * @return array */ protected function normaliseObject(\Google_Service_Storage_StorageObject $object) { return ['type' => 'file', 'dirname' => Util::dirname($object->getName()), 'path' => $object->getName(), 'timestamp' => strtotime($object->getUpdated()), 'mimetype' => $object->getContentType(), 'size' => $object->getSize()]; }
/** * Normalize a result from AWS * * @param string $object * @param string $path * @return array file metadata */ protected function normalizeObject($object, $path = null) { $result = array('path' => $path ?: $object['Key']); if (isset($object['LastModified'])) { $result['timestamp'] = strtotime($object['LastModified']); } $result = array_merge($result, Util::map($object, static::$resultMap), array('type' => 'file')); $result['dirname'] = Util::dirname($result['path']); if (isset($result['contents'])) { $result['contents'] = (string) $result['contents']; } return $result; }
/** * Normalize a DataObject * * @param DataObject $object * @return array file metadata */ protected function normalizeObject(DataObject $object) { $name = $object->getName(); $mimetype = explode('; ', $object->getContentType()); return array('type' => 'file', 'dirname' => Util::dirname($name), 'path' => $name, 'timestamp' => strtotime($object->getLastModified()), 'mimetype' => reset($mimetype), 'size' => $object->getContentLength()); }
/** * {@inheritdoc} */ public function listContents($directory = '', $recursive = false) { $directory = Util::normalizePath($directory); $contents = $this->getAdapter()->listContents($directory, $recursive); $mapper = function ($entry) use($directory, $recursive) { if ($entry['path'] === false && (!empty($directory) && strpos($entry['path'], $directory) === false)) { return false; } $entry = $entry + Util::pathinfo($entry['path']); if ($recursive === false && Util::dirname($entry['path']) !== $directory) { return false; } return $entry; }; $listing = array_values(array_filter(array_map($mapper, $contents))); usort($listing, function ($a, $b) { return strcasecmp($a['path'], $b['path']); }); return $listing; }
/** * Get all the meta data of a file or directory. * * @param string $path * @return array|false * @throws \OSS\Core\OssException */ public function getMetadata($path) { $object = $this->applyPathPrefix($path); try { $result = $this->client->getObjectMeta($this->bucket, $object); } catch (OssException $e) { return false; } return ['type' => 'file', 'dirname' => Util::dirname($path), 'path' => $path, 'timestamp' => strtotime($result['last-modified']), 'mimetype' => $result['content-type'], 'size' => $result['content-length']]; }
/** * Builds the normalized output array from a Blob object. * * @param string $path * @param BlobProperties $properties * * @return array */ protected function normalizeBlobProperties($path, BlobProperties $properties) { return ['path' => $path, 'timestamp' => (int) $properties->getLastModified()->format('U'), 'dirname' => Util::dirname($path), 'mimetype' => $properties->getContentType(), 'size' => $properties->getContentLength(), 'type' => 'file']; }
/** * Normalize a result from AWS. * * @param array $object * @param string $path * * @return array file metadata */ protected function normalizeResponse(array $object, $path = null) { $result = ['path' => $path ?: $this->removePathPrefix($object['Key'])]; $result['dirname'] = Util::dirname($result['path']); if (isset($object['LastModified'])) { $result['timestamp'] = strtotime($object['LastModified']); } if (substr($result['path'], -1) === '/') { $result['type'] = 'dir'; $result['path'] = rtrim($result['path'], '/'); return $result; } $result = array_merge($result, Util::map($object, static::$resultMap), ['type' => 'file']); return $result; }
/** * @inheritdoc */ public function writeStream($path, $resource, Config $config) { $this->ensureDirectory(Util::dirname($path)); if (!ftp_fput($this->getConnection(), $path, $resource, $this->transferMode)) { return false; } if ($visibility = $config->get('visibility')) { $this->setVisibility($path, $visibility); } return compact('path', 'visibility'); }
/** * Returns a dictionary of object metadata from an object. * * @param StorageObject $object * @return array */ protected function normaliseObject(StorageObject $object) { $name = $object->name(); $info = $object->info(); $isDir = substr($name, -1) === '/'; if ($isDir) { $name = rtrim($name, '/'); } return ['type' => $isDir ? 'dir' : 'file', 'dirname' => Util::dirname($name), 'path' => $name, 'timestamp' => strtotime($info['updated']), 'mimetype' => $info['contentType'], 'size' => $info['size']]; }