updated backend

This commit is contained in:
Torsten 2020-01-17 10:58:01 +01:00
parent 1fea0f4fe7
commit 11521c2339
3459 changed files with 72735 additions and 168535 deletions

View file

@ -1,4 +1,4 @@
Copyright (c) 2013-2018 Frank de Jonge
Copyright (c) 2013-2019 Frank de Jonge
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View file

@ -1,5 +1,6 @@
{
"name": "league/flysystem",
"type": "library",
"description": "Filesystem abstraction: Many filesystems, one API.",
"keywords": [
"filesystem", "filesystems", "files", "storage", "dropbox", "aws",
@ -14,12 +15,12 @@
}
],
"require": {
"php": ">=5.5.9"
"php": ">=5.5.9",
"ext-fileinfo": "*"
},
"require-dev": {
"ext-fileinfo": "*",
"phpspec/phpspec": "^3.4",
"phpunit/phpunit": "^5.7"
"phpunit/phpunit": "^5.7.10"
},
"autoload": {
"psr-4": {
@ -53,12 +54,12 @@
"conflict": {
"league/flysystem-sftp": "<1.0.6"
},
"config": {
"bin-dir": "bin"
},
"extra": {
"branch-alias": {
"dev-master": "1.1-dev"
}
},
"scripts": {
"phpstan": "php phpstan.php"
}
}

View file

@ -29,6 +29,7 @@ abstract class AbstractAdapter implements AdapterInterface
if ($prefix === '') {
$this->pathPrefix = null;
return;
}

View file

@ -71,16 +71,18 @@ abstract class AbstractFtpAdapter extends AbstractAdapter
*/
protected $systemType;
/**
* @var bool
*/
protected $alternativeRecursion = false;
/**
* @var SafeStorage
*/
protected $safeStorage;
/**
* True to enable timestamps for FTP servers that return unix-style listings.
*
* @var bool
*/
protected $enableTimestampsOnUnixListings = false;
/**
* Constructor.
*
@ -314,6 +316,20 @@ abstract class AbstractFtpAdapter extends AbstractAdapter
return $this;
}
/**
* True to enable timestamps for FTP servers that return unix-style listings.
*
* @param bool $bool
*
* @return $this
*/
public function setEnableTimestampsOnUnixListings($bool = false)
{
$this->enableTimestampsOnUnixListings = $bool;
return $this;
}
/**
* @inheritdoc
*/
@ -394,6 +410,18 @@ abstract class AbstractFtpAdapter extends AbstractAdapter
/**
* Normalize a Unix file entry.
*
* Given $item contains:
* '-rw-r--r-- 1 ftp ftp 409 Aug 19 09:01 file1.txt'
*
* This function will return:
* [
* 'type' => 'file',
* 'path' => 'file1.txt',
* 'visibility' => 'public',
* 'size' => 409,
* 'timestamp' => 1566205260
* ]
*
* @param string $item
* @param string $base
*
@ -407,7 +435,7 @@ abstract class AbstractFtpAdapter extends AbstractAdapter
throw new RuntimeException("Metadata can't be parsed from item '$item' , not enough parts.");
}
list($permissions, /* $number */, /* $owner */, /* $group */, $size, /* $month */, /* $day */, /* $time*/, $name) = explode(' ', $item, 9);
list($permissions, /* $number */, /* $owner */, /* $group */, $size, $month, $day, $timeOrYear, $name) = explode(' ', $item, 9);
$type = $this->detectType($permissions);
$path = $base === '' ? $name : $base . $this->separator . $name;
@ -419,7 +447,44 @@ abstract class AbstractFtpAdapter extends AbstractAdapter
$visibility = $permissions & 0044 ? AdapterInterface::VISIBILITY_PUBLIC : AdapterInterface::VISIBILITY_PRIVATE;
$size = (int) $size;
return compact('type', 'path', 'visibility', 'size');
$result = compact('type', 'path', 'visibility', 'size');
if ($this->enableTimestampsOnUnixListings) {
$timestamp = $this->normalizeUnixTimestamp($month, $day, $timeOrYear);
$result += compact('timestamp');
}
return $result;
}
/**
* Only accurate to the minute (current year), or to the day.
*
* Inadequacies in timestamp accuracy are due to limitations of the FTP 'LIST' command
*
* Note: The 'MLSD' command is a machine-readable replacement for 'LIST'
* but many FTP servers do not support it :(
*
* @param string $month e.g. 'Aug'
* @param string $day e.g. '19'
* @param string $timeOrYear e.g. '09:01' OR '2015'
*
* @return int
*/
protected function normalizeUnixTimestamp($month, $day, $timeOrYear)
{
if (is_numeric($timeOrYear)) {
$year = $timeOrYear;
$hour = '00';
$minute = '00';
$seconds = '00';
} else {
$year = date('Y');
list($hour, $minute) = explode(':', $timeOrYear);
$seconds = '00';
}
$dateTime = DateTime::createFromFormat('Y-M-j-G:i:s', "{$year}-{$month}-{$day}-{$hour}:{$minute}:{$seconds}");
return $dateTime->getTimestamp();
}
/**
@ -492,6 +557,10 @@ abstract class AbstractFtpAdapter extends AbstractAdapter
*/
protected function normalizePermissions($permissions)
{
if (is_numeric($permissions)) {
return $permissions & 0777;
}
// remove the type identifier
$permissions = substr($permissions, 1);

View file

@ -4,7 +4,9 @@
namespace League\Flysystem\Adapter;
/**
* Adapters that implement this interface let the Filesystem know that it files can be overwritten using the write
* Adapters that implement this interface let the Filesystem know that files can be overwritten using the write
* functions and don't need the update function to be called. This can help improve performance when asserts are disabled.
*/
interface CanOverwriteFiles {}
interface CanOverwriteFiles
{
}

View file

@ -6,9 +6,11 @@ use ErrorException;
use League\Flysystem\Adapter\Polyfill\StreamedCopyTrait;
use League\Flysystem\AdapterInterface;
use League\Flysystem\Config;
use League\Flysystem\ConnectionErrorException;
use League\Flysystem\ConnectionRuntimeException;
use League\Flysystem\InvalidRootException;
use League\Flysystem\Util;
use League\Flysystem\Util\MimeType;
use RuntimeException;
class Ftp extends AbstractFtpAdapter
{
@ -53,6 +55,7 @@ class Ftp extends AbstractFtpAdapter
'ignorePassiveAddress',
'recurseManually',
'utf8',
'enableTimestampsOnUnixListings',
];
/**
@ -134,7 +137,7 @@ class Ftp extends AbstractFtpAdapter
}
if ( ! $this->connection) {
throw new RuntimeException('Could not connect to host: ' . $this->getHost() . ', port:' . $this->getPort());
throw new ConnectionRuntimeException('Could not connect to host: ' . $this->getHost() . ', port:' . $this->getPort());
}
$this->login();
@ -152,7 +155,7 @@ class Ftp extends AbstractFtpAdapter
if ($this->utf8) {
$response = ftp_raw($this->connection, "OPTS UTF8 ON");
if (substr($response[0], 0, 3) !== '200') {
throw new RuntimeException(
throw new ConnectionRuntimeException(
'Could not set UTF-8 mode for connection: ' . $this->getHost() . '::' . $this->getPort()
);
}
@ -162,7 +165,7 @@ class Ftp extends AbstractFtpAdapter
/**
* Set the connections to passive mode.
*
* @throws RuntimeException
* @throws ConnectionRuntimeException
*/
protected function setConnectionPassiveMode()
{
@ -171,7 +174,7 @@ class Ftp extends AbstractFtpAdapter
}
if ( ! ftp_pasv($this->connection, $this->passive)) {
throw new RuntimeException(
throw new ConnectionRuntimeException(
'Could not set passive mode for connection: ' . $this->getHost() . '::' . $this->getPort()
);
}
@ -186,7 +189,7 @@ class Ftp extends AbstractFtpAdapter
$connection = $this->connection;
if ($root && ! ftp_chdir($connection, $root)) {
throw new RuntimeException('Root is invalid or does not exist: ' . $this->getRoot());
throw new InvalidRootException('Root is invalid or does not exist: ' . $this->getRoot());
}
// Store absolute path for further reference.
@ -199,11 +202,12 @@ class Ftp extends AbstractFtpAdapter
/**
* Login.
*
* @throws RuntimeException
* @throws ConnectionRuntimeException
*/
protected function login()
{
set_error_handler(function () {});
set_error_handler(function () {
});
$isLoggedIn = ftp_login(
$this->connection,
$this->getUsername(),
@ -213,7 +217,7 @@ class Ftp extends AbstractFtpAdapter
if ( ! $isLoggedIn) {
$this->disconnect();
throw new RuntimeException(
throw new ConnectionRuntimeException(
'Could not login with connection: ' . $this->getHost() . '::' . $this->getPort(
) . ', username: ' . $this->getUsername()
);
@ -248,7 +252,7 @@ class Ftp extends AbstractFtpAdapter
}
$result['contents'] = $contents;
$result['mimetype'] = Util::guessMimeType($path, $contents);
$result['mimetype'] = $config->get('mimetype') ?: Util::guessMimeType($path, $contents);
return $result;
}
@ -380,13 +384,11 @@ class Ftp extends AbstractFtpAdapter
*/
public function getMetadata($path)
{
$connection = $this->getConnection();
if ($path === '') {
return ['type' => 'dir', 'path' => ''];
}
if (@ftp_chdir($connection, $path) === true) {
if (@ftp_chdir($this->getConnection(), $path) === true) {
$this->setConnectionRoot();
return ['type' => 'dir', 'path' => $path];
@ -512,7 +514,9 @@ class Ftp extends AbstractFtpAdapter
foreach ($listing as $item) {
$output[] = $item;
if ($item['type'] !== 'dir') continue;
if ($item['type'] !== 'dir') {
continue;
}
$output = array_merge($output, $this->listDirectoryContentsRecursive($item['path']));
}
@ -523,7 +527,8 @@ class Ftp extends AbstractFtpAdapter
* Check if the connection is open.
*
* @return bool
* @throws ErrorException
*
* @throws ConnectionErrorException
*/
public function isConnected()
{
@ -531,7 +536,7 @@ class Ftp extends AbstractFtpAdapter
return is_resource($this->connection) && ftp_rawlist($this->connection, $this->getRoot()) !== false;
} catch (ErrorException $e) {
if (strpos($e->getMessage(), 'ftp_rawlist') === false) {
throw $e;
throw new ConnectionErrorException($e->getMessage());
}
return false;
@ -563,6 +568,7 @@ class Ftp extends AbstractFtpAdapter
if ($this->isPureFtpd) {
$path = str_replace(' ', '\ ', $path);
}
return ftp_rawlist($connection, $options . ' ' . $path);
}
}

View file

@ -12,6 +12,11 @@ class Ftpd extends Ftp
if ($path === '') {
return ['type' => 'dir', 'path' => ''];
}
if (@ftp_chdir($this->getConnection(), $path) === true) {
$this->setConnectionRoot();
return ['type' => 'dir', 'path' => $path];
}
if ( ! ($object = ftp_raw($this->getConnection(), 'STAT ' . $path)) || count($object) < 3) {
return false;

View file

@ -5,7 +5,6 @@ namespace League\Flysystem\Adapter;
use DirectoryIterator;
use FilesystemIterator;
use finfo as Finfo;
use League\Flysystem\AdapterInterface;
use League\Flysystem\Config;
use League\Flysystem\Exception;
use League\Flysystem\NotSupportedException;
@ -39,7 +38,7 @@ class Local extends AbstractAdapter
'dir' => [
'public' => 0755,
'private' => 0700,
]
],
];
/**
@ -56,6 +55,7 @@ class Local extends AbstractAdapter
* @var int
*/
protected $writeFlags;
/**
* @var int
*/
@ -99,11 +99,17 @@ class Local extends AbstractAdapter
{
if ( ! is_dir($root)) {
$umask = umask(0);
@mkdir($root, $this->permissionMap['dir']['public'], true);
if ( ! @mkdir($root, $this->permissionMap['dir']['public'], true)) {
$mkdirError = error_get_last();
}
umask($umask);
clearstatcache(false, $root);
if ( ! is_dir($root)) {
throw new Exception(sprintf('Impossible to create the root directory "%s".', $root));
$errorMessage = isset($mkdirError['message']) ? $mkdirError['message'] : '';
throw new Exception(sprintf('Impossible to create the root directory "%s". %s', $root, $errorMessage));
}
}
}
@ -150,18 +156,11 @@ class Local extends AbstractAdapter
$this->ensureDirectory(dirname($location));
$stream = fopen($location, 'w+b');
if ( ! $stream) {
return false;
}
stream_copy_to_stream($resource, $stream);
if ( ! fclose($stream)) {
if ( ! $stream || stream_copy_to_stream($resource, $stream) === false || ! fclose($stream)) {
return false;
}
$type = 'file';
$result = compact('type', 'path');
if ($visibility = $config->get('visibility')) {
@ -207,7 +206,7 @@ class Local extends AbstractAdapter
$result = compact('type', 'path', 'size', 'contents');
if ($mimetype = Util::guessMimeType($path, $contents)) {
if ($mimetype = $config->get('mimetype') ?: Util::guessMimeType($path, $contents)) {
$result['mimetype'] = $mimetype;
}
@ -220,7 +219,7 @@ class Local extends AbstractAdapter
public function read($path)
{
$location = $this->applyPathPrefix($path);
$contents = file_get_contents($location);
$contents = @file_get_contents($location);
if ($contents === false) {
return false;
@ -261,7 +260,7 @@ class Local extends AbstractAdapter
{
$location = $this->applyPathPrefix($path);
return unlink($location);
return @unlink($location);
}
/**
@ -288,6 +287,8 @@ class Local extends AbstractAdapter
$result[] = $this->normalizeFileInfo($file);
}
unset($iterator);
return array_filter($result);
}
@ -297,6 +298,7 @@ class Local extends AbstractAdapter
public function getMetadata($path)
{
$location = $this->applyPathPrefix($path);
clearstatcache(false, $location);
$info = new SplFileInfo($location);
return $this->normalizeFileInfo($info);
@ -319,7 +321,7 @@ class Local extends AbstractAdapter
$finfo = new Finfo(FILEINFO_MIME_TYPE);
$mimetype = $finfo->file($location);
if (in_array($mimetype, ['application/octet-stream', 'inode/x-empty'])) {
if (in_array($mimetype, ['application/octet-stream', 'inode/x-empty', 'application/x-empty'])) {
$mimetype = Util\MimeType::detectByFilename($location);
}
@ -342,7 +344,15 @@ class Local extends AbstractAdapter
$location = $this->applyPathPrefix($path);
clearstatcache(false, $location);
$permissions = octdec(substr(sprintf('%o', fileperms($location)), -4));
$visibility = $permissions & 0044 ? AdapterInterface::VISIBILITY_PUBLIC : AdapterInterface::VISIBILITY_PRIVATE;
$type = is_dir($location) ? 'dir' : 'file';
foreach ($this->permissionMap[$type] as $visibility => $visibilityPermissions) {
if ($visibilityPermissions == $permissions) {
return compact('path', 'visibility');
}
}
$visibility = substr(sprintf('%o', fileperms($location)), -4);
return compact('path', 'visibility');
}
@ -371,11 +381,13 @@ class Local extends AbstractAdapter
$location = $this->applyPathPrefix($dirname);
$umask = umask(0);
$visibility = $config->get('visibility', 'public');
$return = ['path' => $dirname, 'type' => 'dir'];
if ( ! is_dir($location) && ! mkdir($location, $this->permissionMap['dir'][$visibility], true)) {
$return = false;
} else {
$return = ['path' => $dirname, 'type' => 'dir'];
if ( ! is_dir($location)) {
if (false === @mkdir($location, $this->permissionMap['dir'][$visibility], true)
|| false === is_dir($location)) {
$return = false;
}
}
umask($umask);
@ -402,6 +414,8 @@ class Local extends AbstractAdapter
$this->deleteFileInfoObject($file);
}
unset($contents);
return rmdir($location);
}

View file

@ -34,15 +34,17 @@ trait StreamedCopyTrait
// Required abstract method
/**
* @param string $path
* @param string $path
*
* @return resource
*/
abstract public function readStream($path);
/**
* @param string $path
* @param resource $resource
* @param Config $config
* @param string $path
* @param resource $resource
* @param Config $config
*
* @return resource
*/
abstract public function writeStream($path, $resource, Config $config);

View file

@ -10,7 +10,7 @@ class Config
protected $settings = [];
/**
* @var Config
* @var Config|null
*/
protected $fallback;

View file

@ -2,7 +2,7 @@
namespace League\Flysystem;
class Exception extends \Exception
class Exception extends \Exception implements FilesystemException
{
//
}

View file

@ -115,7 +115,7 @@ class Filesystem implements FilesystemInterface
$config = $this->prepareConfig($config);
Util::rewindStream($resource);
if ( ! $this->getAdapter() instanceof CanOverwriteFiles &&$this->has($path)) {
if ( ! $this->getAdapter() instanceof CanOverwriteFiles && $this->has($path)) {
return (bool) $this->getAdapter()->updateStream($path, $resource, $config);
}
@ -270,7 +270,8 @@ class Filesystem implements FilesystemInterface
$directory = Util::normalizePath($directory);
$contents = $this->getAdapter()->listContents($directory, $recursive);
return (new ContentListingFormatter($directory, $recursive))->formatListing($contents);
return (new ContentListingFormatter($directory, $recursive, $this->config->get('case_sensitive', true)))
->formatListing($contents);
}
/**
@ -364,7 +365,7 @@ class Filesystem implements FilesystemInterface
if ( ! $handler) {
$metadata = $this->getMetadata($path);
$handler = $metadata['type'] === 'file' ? new File($this, $path) : new Directory($this, $path);
$handler = ($metadata && $metadata['type'] === 'file') ? new File($this, $path) : new Directory($this, $path);
}
$handler->setPath($path);

View file

@ -7,6 +7,6 @@ use LogicException;
/**
* Thrown when the MountManager cannot find a filesystem.
*/
class FilesystemNotFoundException extends LogicException
class FilesystemNotFoundException extends LogicException implements FilesystemException
{
}

View file

@ -60,7 +60,7 @@ abstract class Handler
{
$metadata = $this->filesystem->getMetadata($this->path);
return $metadata['type'];
return $metadata ? $metadata['type'] : 'dir';
}
/**

View file

@ -3,7 +3,6 @@
namespace League\Flysystem;
use InvalidArgumentException;
use League\Flysystem\FilesystemNotFoundException;
use League\Flysystem\Plugin\PluggableTrait;
use League\Flysystem\Plugin\PluginNotFoundException;
@ -14,36 +13,15 @@ use League\Flysystem\Plugin\PluginNotFoundException;
*
* @method AdapterInterface getAdapter($prefix)
* @method Config getConfig($prefix)
* @method bool has($path)
* @method bool write($path, $contents, array $config = [])
* @method bool writeStream($path, $resource, array $config = [])
* @method bool put($path, $contents, $config = [])
* @method bool putStream($path, $contents, $config = [])
* @method string readAndDelete($path)
* @method bool update($path, $contents, $config = [])
* @method bool updateStream($path, $resource, $config = [])
* @method string|false read($path)
* @method resource|false readStream($path)
* @method bool rename($path, $newpath)
* @method bool delete($path)
* @method bool deleteDir($dirname)
* @method bool createDir($dirname, $config = [])
* @method array listFiles($directory = '', $recursive = false)
* @method array listPaths($directory = '', $recursive = false)
* @method array getWithMetadata($path, array $metadata)
* @method string|false getMimetype($path)
* @method int|false getTimestamp($path)
* @method string|false getVisibility($path)
* @method int|false getSize($path);
* @method bool setVisibility($path, $visibility)
* @method array|false getMetadata($path)
* @method Handler get($path, Handler $handler = null)
* @method Filesystem flushCache()
* @method void assertPresent($path)
* @method void assertAbsent($path)
* @method Filesystem addPlugin(PluginInterface $plugin)
*/
class MountManager
class MountManager implements FilesystemInterface
{
use PluggableTrait;
@ -195,6 +173,7 @@ class MountManager
*
* @throws InvalidArgumentException
* @throws FilesystemNotFoundException
* @throws FileExistsException
*
* @return bool
*/
@ -317,4 +296,353 @@ class MountManager
return explode('://', $path, 2);
}
/**
* Check whether a file exists.
*
* @param string $path
*
* @return bool
*/
public function has($path)
{
list($prefix, $path) = $this->getPrefixAndPath($path);
return $this->getFilesystem($prefix)->has($path);
}
/**
* Read a file.
*
* @param string $path The path to the file.
*
* @throws FileNotFoundException
*
* @return string|false The file contents or false on failure.
*/
public function read($path)
{
list($prefix, $path) = $this->getPrefixAndPath($path);
return $this->getFilesystem($prefix)->read($path);
}
/**
* Retrieves a read-stream for a path.
*
* @param string $path The path to the file.
*
* @throws FileNotFoundException
*
* @return resource|false The path resource or false on failure.
*/
public function readStream($path)
{
list($prefix, $path) = $this->getPrefixAndPath($path);
return $this->getFilesystem($prefix)->readStream($path);
}
/**
* Get a file's metadata.
*
* @param string $path The path to the file.
*
* @throws FileNotFoundException
*
* @return array|false The file metadata or false on failure.
*/
public function getMetadata($path)
{
list($prefix, $path) = $this->getPrefixAndPath($path);
return $this->getFilesystem($prefix)->getMetadata($path);
}
/**
* Get a file's size.
*
* @param string $path The path to the file.
*
* @throws FileNotFoundException
*
* @return int|false The file size or false on failure.
*/
public function getSize($path)
{
list($prefix, $path) = $this->getPrefixAndPath($path);
return $this->getFilesystem($prefix)->getSize($path);
}
/**
* Get a file's mime-type.
*
* @param string $path The path to the file.
*
* @throws FileNotFoundException
*
* @return string|false The file mime-type or false on failure.
*/
public function getMimetype($path)
{
list($prefix, $path) = $this->getPrefixAndPath($path);
return $this->getFilesystem($prefix)->getMimetype($path);
}
/**
* Get a file's timestamp.
*
* @param string $path The path to the file.
*
* @throws FileNotFoundException
*
* @return string|false The timestamp or false on failure.
*/
public function getTimestamp($path)
{
list($prefix, $path) = $this->getPrefixAndPath($path);
return $this->getFilesystem($prefix)->getTimestamp($path);
}
/**
* Get a file's visibility.
*
* @param string $path The path to the file.
*
* @throws FileNotFoundException
*
* @return string|false The visibility (public|private) or false on failure.
*/
public function getVisibility($path)
{
list($prefix, $path) = $this->getPrefixAndPath($path);
return $this->getFilesystem($prefix)->getVisibility($path);
}
/**
* Write a new file.
*
* @param string $path The path of the new file.
* @param string $contents The file contents.
* @param array $config An optional configuration array.
*
* @throws FileExistsException
*
* @return bool True on success, false on failure.
*/
public function write($path, $contents, array $config = [])
{
list($prefix, $path) = $this->getPrefixAndPath($path);
return $this->getFilesystem($prefix)->write($path, $contents, $config);
}
/**
* Write a new file using a stream.
*
* @param string $path The path of the new file.
* @param resource $resource The file handle.
* @param array $config An optional configuration array.
*
* @throws InvalidArgumentException If $resource is not a file handle.
* @throws FileExistsException
*
* @return bool True on success, false on failure.
*/
public function writeStream($path, $resource, array $config = [])
{
list($prefix, $path) = $this->getPrefixAndPath($path);
return $this->getFilesystem($prefix)->writeStream($path, $resource, $config);
}
/**
* Update an existing file.
*
* @param string $path The path of the existing file.
* @param string $contents The file contents.
* @param array $config An optional configuration array.
*
* @throws FileNotFoundException
*
* @return bool True on success, false on failure.
*/
public function update($path, $contents, array $config = [])
{
list($prefix, $path) = $this->getPrefixAndPath($path);
return $this->getFilesystem($prefix)->update($path, $contents, $config);
}
/**
* Update an existing file using a stream.
*
* @param string $path The path of the existing file.
* @param resource $resource The file handle.
* @param array $config An optional configuration array.
*
* @throws InvalidArgumentException If $resource is not a file handle.
* @throws FileNotFoundException
*
* @return bool True on success, false on failure.
*/
public function updateStream($path, $resource, array $config = [])
{
list($prefix, $path) = $this->getPrefixAndPath($path);
return $this->getFilesystem($prefix)->updateStream($path, $resource, $config);
}
/**
* Rename a file.
*
* @param string $path Path to the existing file.
* @param string $newpath The new path of the file.
*
* @throws FileExistsException Thrown if $newpath exists.
* @throws FileNotFoundException Thrown if $path does not exist.
*
* @return bool True on success, false on failure.
*/
public function rename($path, $newpath)
{
list($prefix, $path) = $this->getPrefixAndPath($path);
return $this->getFilesystem($prefix)->rename($path, $newpath);
}
/**
* Delete a file.
*
* @param string $path
*
* @throws FileNotFoundException
*
* @return bool True on success, false on failure.
*/
public function delete($path)
{
list($prefix, $path) = $this->getPrefixAndPath($path);
return $this->getFilesystem($prefix)->delete($path);
}
/**
* Delete a directory.
*
* @param string $dirname
*
* @throws RootViolationException Thrown if $dirname is empty.
*
* @return bool True on success, false on failure.
*/
public function deleteDir($dirname)
{
list($prefix, $dirname) = $this->getPrefixAndPath($dirname);
return $this->getFilesystem($prefix)->deleteDir($dirname);
}
/**
* Create a directory.
*
* @param string $dirname The name of the new directory.
* @param array $config An optional configuration array.
*
* @return bool True on success, false on failure.
*/
public function createDir($dirname, array $config = [])
{
list($prefix, $dirname) = $this->getPrefixAndPath($dirname);
return $this->getFilesystem($prefix)->createDir($dirname);
}
/**
* Set the visibility for a file.
*
* @param string $path The path to the file.
* @param string $visibility One of 'public' or 'private'.
*
* @throws FileNotFoundException
*
* @return bool True on success, false on failure.
*/
public function setVisibility($path, $visibility)
{
list($prefix, $path) = $this->getPrefixAndPath($path);
return $this->getFilesystem($prefix)->setVisibility($path, $visibility);
}
/**
* Create a file or update if exists.
*
* @param string $path The path to the file.
* @param string $contents The file contents.
* @param array $config An optional configuration array.
*
* @return bool True on success, false on failure.
*/
public function put($path, $contents, array $config = [])
{
list($prefix, $path) = $this->getPrefixAndPath($path);
return $this->getFilesystem($prefix)->put($path, $contents, $config);
}
/**
* Create a file or update if exists.
*
* @param string $path The path to the file.
* @param resource $resource The file handle.
* @param array $config An optional configuration array.
*
* @throws InvalidArgumentException Thrown if $resource is not a resource.
*
* @return bool True on success, false on failure.
*/
public function putStream($path, $resource, array $config = [])
{
list($prefix, $path) = $this->getPrefixAndPath($path);
return $this->getFilesystem($prefix)->putStream($path, $resource, $config);
}
/**
* Read and delete a file.
*
* @param string $path The path to the file.
*
* @throws FileNotFoundException
*
* @return string|false The file contents, or false on failure.
*/
public function readAndDelete($path)
{
list($prefix, $path) = $this->getPrefixAndPath($path);
return $this->getFilesystem($prefix)->readAndDelete($path);
}
/**
* Get a file/directory handler.
*
* @deprecated
*
* @param string $path The path to the file.
* @param Handler $handler An optional existing handler to populate.
*
* @return Handler Either a file or directory handler.
*/
public function get($path, Handler $handler = null)
{
list($prefix, $path) = $this->getPrefixAndPath($path);
return $this->getFilesystem($prefix)->get($path);
}
}

View file

@ -5,7 +5,7 @@ namespace League\Flysystem;
use RuntimeException;
use SplFileInfo;
class NotSupportedException extends RuntimeException
class NotSupportedException extends RuntimeException implements FilesystemException
{
/**
* Create a new exception for a link.

View file

@ -4,7 +4,7 @@ namespace League\Flysystem;
use LogicException;
class RootViolationException extends LogicException
class RootViolationException extends LogicException implements FilesystemException
{
//
}

View file

@ -137,7 +137,8 @@ class Util
*
* @return string $path
*/
protected static function removeFunkyWhiteSpace($path) {
protected static function removeFunkyWhiteSpace($path)
{
// We do this check in a loop, since removing invalid unicode characters
// can lead to new characters being created.
while (preg_match('#\p{C}+|^\./#u', $path)) {
@ -175,7 +176,7 @@ class Util
/**
* Guess MIME Type based on the path of the file and it's content.
*
* @param string $path
* @param string $path
* @param string|resource $content
*
* @return string|null MIME Type or NULL if no extension detected
@ -266,12 +267,16 @@ class Util
*
* @param resource $resource
*
* @return int stream size
* @return int|null stream size
*/
public static function getStreamSize($resource)
{
$stat = fstat($resource);
if ( ! is_array($stat) || ! isset($stat['size'])) {
return null;
}
return $stat['size'];
}
@ -290,13 +295,13 @@ class Util
$listedDirectories[] = $object['path'];
}
if (empty($object['dirname'])) {
if ( ! isset($object['dirname']) || trim($object['dirname']) === '') {
return [$directories, $listedDirectories];
}
$parent = $object['dirname'];
while ( ! empty($parent) && ! in_array($parent, $directories)) {
while (isset($parent) && trim($parent) !== '' && ! in_array($parent, $directories)) {
$directories[] = $parent;
$parent = static::dirname($parent);
}

View file

@ -13,19 +13,26 @@ class ContentListingFormatter
* @var string
*/
private $directory;
/**
* @var bool
*/
private $recursive;
/**
* @var bool
*/
private $caseSensitive;
/**
* @param string $directory
* @param bool $recursive
*/
public function __construct($directory, $recursive)
public function __construct($directory, $recursive, $caseSensitive = true)
{
$this->directory = $directory;
$this->directory = rtrim($directory, '/');
$this->recursive = $recursive;
$this->caseSensitive = $caseSensitive;
}
/**
@ -37,14 +44,9 @@ class ContentListingFormatter
*/
public function formatListing(array $listing)
{
$listing = array_values(
array_map(
[$this, 'addPathInfo'],
array_filter($listing, [$this, 'isEntryOutOfScope'])
)
);
$listing = array_filter(array_map([$this, 'addPathInfo'], $listing), [$this, 'isEntryOutOfScope']);
return $this->sortListing($listing);
return $this->sortListing(array_values($listing));
}
private function addPathInfo(array $entry)
@ -85,7 +87,9 @@ class ContentListingFormatter
return true;
}
return strpos($entry['path'], $this->directory . '/') === 0;
return $this->caseSensitive
? strpos($entry['path'], $this->directory . '/') === 0
: stripos($entry['path'], $this->directory . '/') === 0;
}
/**
@ -97,7 +101,9 @@ class ContentListingFormatter
*/
private function isDirectChild(array $entry)
{
return Util::dirname($entry['path']) === $this->directory;
return $this->caseSensitive
? $entry['dirname'] === $this->directory
: strcasecmp($this->directory, $entry['dirname']) === 0;
}
/**

View file

@ -2,14 +2,191 @@
namespace League\Flysystem\Util;
use finfo;
use ErrorException;
use finfo;
/**
* @internal
*/
class MimeType
{
protected static $extensionToMimeTypeMap = [
'hqx' => 'application/mac-binhex40',
'cpt' => 'application/mac-compactpro',
'csv' => 'text/csv',
'bin' => 'application/octet-stream',
'dms' => 'application/octet-stream',
'lha' => 'application/octet-stream',
'lzh' => 'application/octet-stream',
'exe' => 'application/octet-stream',
'class' => 'application/octet-stream',
'psd' => 'application/x-photoshop',
'so' => 'application/octet-stream',
'sea' => 'application/octet-stream',
'dll' => 'application/octet-stream',
'oda' => 'application/oda',
'pdf' => 'application/pdf',
'ai' => 'application/pdf',
'eps' => 'application/postscript',
'epub' => 'application/epub+zip',
'ps' => 'application/postscript',
'smi' => 'application/smil',
'smil' => 'application/smil',
'mif' => 'application/vnd.mif',
'xls' => 'application/vnd.ms-excel',
'xlt' => 'application/vnd.ms-excel',
'xla' => 'application/vnd.ms-excel',
'ppt' => 'application/powerpoint',
'pot' => 'application/vnd.ms-powerpoint',
'pps' => 'application/vnd.ms-powerpoint',
'ppa' => 'application/vnd.ms-powerpoint',
'pptx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
'potx' => 'application/vnd.openxmlformats-officedocument.presentationml.template',
'ppsx' => 'application/vnd.openxmlformats-officedocument.presentationml.slideshow',
'ppam' => 'application/vnd.ms-powerpoint.addin.macroEnabled.12',
'pptm' => 'application/vnd.ms-powerpoint.presentation.macroEnabled.12',
'potm' => 'application/vnd.ms-powerpoint.presentation.macroEnabled.12',
'ppsm' => 'application/vnd.ms-powerpoint.slideshow.macroEnabled.12',
'wbxml' => 'application/wbxml',
'wmlc' => 'application/wmlc',
'dcr' => 'application/x-director',
'dir' => 'application/x-director',
'dxr' => 'application/x-director',
'dvi' => 'application/x-dvi',
'gtar' => 'application/x-gtar',
'gz' => 'application/x-gzip',
'gzip' => 'application/x-gzip',
'php' => 'application/x-httpd-php',
'php4' => 'application/x-httpd-php',
'php3' => 'application/x-httpd-php',
'phtml' => 'application/x-httpd-php',
'phps' => 'application/x-httpd-php-source',
'js' => 'application/javascript',
'swf' => 'application/x-shockwave-flash',
'sit' => 'application/x-stuffit',
'tar' => 'application/x-tar',
'tgz' => 'application/x-tar',
'z' => 'application/x-compress',
'xhtml' => 'application/xhtml+xml',
'xht' => 'application/xhtml+xml',
'rdf' => 'application/rdf+xml',
'zip' => 'application/x-zip',
'rar' => 'application/x-rar',
'mid' => 'audio/midi',
'midi' => 'audio/midi',
'mpga' => 'audio/mpeg',
'mp2' => 'audio/mpeg',
'mp3' => 'audio/mpeg',
'aif' => 'audio/x-aiff',
'aiff' => 'audio/x-aiff',
'aifc' => 'audio/x-aiff',
'ram' => 'audio/x-pn-realaudio',
'rm' => 'audio/x-pn-realaudio',
'rpm' => 'audio/x-pn-realaudio-plugin',
'ra' => 'audio/x-realaudio',
'rv' => 'video/vnd.rn-realvideo',
'wav' => 'audio/x-wav',
'jpg' => 'image/jpeg',
'jpeg' => 'image/jpeg',
'jpe' => 'image/jpeg',
'png' => 'image/png',
'gif' => 'image/gif',
'bmp' => 'image/bmp',
'tiff' => 'image/tiff',
'tif' => 'image/tiff',
'svg' => 'image/svg+xml',
'css' => 'text/css',
'html' => 'text/html',
'htm' => 'text/html',
'shtml' => 'text/html',
'txt' => 'text/plain',
'text' => 'text/plain',
'log' => 'text/plain',
'rtx' => 'text/richtext',
'rtf' => 'text/rtf',
'xml' => 'application/xml',
'xsl' => 'application/xml',
'dmn' => 'application/octet-stream',
'bpmn' => 'application/octet-stream',
'mpeg' => 'video/mpeg',
'mpg' => 'video/mpeg',
'mpe' => 'video/mpeg',
'qt' => 'video/quicktime',
'mov' => 'video/quicktime',
'avi' => 'video/x-msvideo',
'movie' => 'video/x-sgi-movie',
'doc' => 'application/msword',
'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'docm' => 'application/vnd.ms-word.template.macroEnabled.12',
'dotm' => 'application/vnd.ms-word.template.macroEnabled.12',
'dot' => 'application/msword',
'dotx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.template',
'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
'xltx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.template',
'xlsm' => 'application/vnd.ms-excel.sheet.macroEnabled.12',
'xltm' => 'application/vnd.ms-excel.template.macroEnabled.12',
'xlam' => 'application/vnd.ms-excel.addin.macroEnabled.12',
'xlsb' => 'application/vnd.ms-excel.sheet.binary.macroEnabled.12',
'word' => 'application/msword',
'xl' => 'application/excel',
'eml' => 'message/rfc822',
'json' => 'application/json',
'pem' => 'application/x-x509-user-cert',
'p10' => 'application/x-pkcs10',
'p12' => 'application/x-pkcs12',
'p7a' => 'application/x-pkcs7-signature',
'p7c' => 'application/pkcs7-mime',
'p7m' => 'application/pkcs7-mime',
'p7r' => 'application/x-pkcs7-certreqresp',
'p7s' => 'application/pkcs7-signature',
'crt' => 'application/x-x509-ca-cert',
'crl' => 'application/pkix-crl',
'der' => 'application/x-x509-ca-cert',
'kdb' => 'application/octet-stream',
'pgp' => 'application/pgp',
'gpg' => 'application/gpg-keys',
'sst' => 'application/octet-stream',
'csr' => 'application/octet-stream',
'rsa' => 'application/x-pkcs7',
'cer' => 'application/pkix-cert',
'3g2' => 'video/3gpp2',
'3gp' => 'video/3gp',
'mp4' => 'video/mp4',
'm4a' => 'audio/x-m4a',
'f4v' => 'video/mp4',
'webm' => 'video/webm',
'aac' => 'audio/x-acc',
'm4u' => 'application/vnd.mpegurl',
'm3u' => 'text/plain',
'xspf' => 'application/xspf+xml',
'vlc' => 'application/videolan',
'wmv' => 'video/x-ms-wmv',
'au' => 'audio/x-au',
'ac3' => 'audio/ac3',
'flac' => 'audio/x-flac',
'ogg' => 'audio/ogg',
'kmz' => 'application/vnd.google-earth.kmz',
'kml' => 'application/vnd.google-earth.kml+xml',
'ics' => 'text/calendar',
'zsh' => 'text/x-scriptzsh',
'7zip' => 'application/x-7z-compressed',
'cdr' => 'application/cdr',
'wma' => 'audio/x-ms-wma',
'jar' => 'application/java-archive',
'tex' => 'application/x-tex',
'latex' => 'application/x-latex',
'odt' => 'application/vnd.oasis.opendocument.text',
'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
'odp' => 'application/vnd.oasis.opendocument.presentation',
'odg' => 'application/vnd.oasis.opendocument.graphics',
'odc' => 'application/vnd.oasis.opendocument.chart',
'odf' => 'application/vnd.oasis.opendocument.formula',
'odi' => 'application/vnd.oasis.opendocument.image',
'odm' => 'application/vnd.oasis.opendocument.text-master',
'odb' => 'application/vnd.oasis.opendocument.database',
'ott' => 'application/vnd.oasis.opendocument.text-template',
];
/**
* Detects MIME Type based on given content.
*
@ -26,8 +203,8 @@ class MimeType
$finfo = new finfo(FILEINFO_MIME_TYPE);
return $finfo->buffer($content) ?: null;
// @codeCoverageIgnoreStart
} catch( ErrorException $e ) {
// @codeCoverageIgnoreStart
} catch (ErrorException $e) {
// This is caused by an array to string conversion error.
}
} // @codeCoverageIgnoreEnd
@ -41,17 +218,9 @@ class MimeType
*/
public static function detectByFileExtension($extension)
{
static $extensionToMimeTypeMap;
if (! $extensionToMimeTypeMap) {
$extensionToMimeTypeMap = static::getExtensionToMimeTypeMap();
}
if (isset($extensionToMimeTypeMap[$extension])) {
return $extensionToMimeTypeMap[$extension];
}
return 'text/plain';
return isset(static::$extensionToMimeTypeMap[$extension])
? static::$extensionToMimeTypeMap[$extension]
: 'text/plain';
}
/**
@ -71,162 +240,6 @@ class MimeType
*/
public static function getExtensionToMimeTypeMap()
{
return [
'hqx' => 'application/mac-binhex40',
'cpt' => 'application/mac-compactpro',
'csv' => 'text/x-comma-separated-values',
'bin' => 'application/octet-stream',
'dms' => 'application/octet-stream',
'lha' => 'application/octet-stream',
'lzh' => 'application/octet-stream',
'exe' => 'application/octet-stream',
'class' => 'application/octet-stream',
'psd' => 'application/x-photoshop',
'so' => 'application/octet-stream',
'sea' => 'application/octet-stream',
'dll' => 'application/octet-stream',
'oda' => 'application/oda',
'pdf' => 'application/pdf',
'ai' => 'application/pdf',
'eps' => 'application/postscript',
'ps' => 'application/postscript',
'smi' => 'application/smil',
'smil' => 'application/smil',
'mif' => 'application/vnd.mif',
'xls' => 'application/vnd.ms-excel',
'ppt' => 'application/powerpoint',
'pptx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
'wbxml' => 'application/wbxml',
'wmlc' => 'application/wmlc',
'dcr' => 'application/x-director',
'dir' => 'application/x-director',
'dxr' => 'application/x-director',
'dvi' => 'application/x-dvi',
'gtar' => 'application/x-gtar',
'gz' => 'application/x-gzip',
'gzip' => 'application/x-gzip',
'php' => 'application/x-httpd-php',
'php4' => 'application/x-httpd-php',
'php3' => 'application/x-httpd-php',
'phtml' => 'application/x-httpd-php',
'phps' => 'application/x-httpd-php-source',
'js' => 'application/javascript',
'swf' => 'application/x-shockwave-flash',
'sit' => 'application/x-stuffit',
'tar' => 'application/x-tar',
'tgz' => 'application/x-tar',
'z' => 'application/x-compress',
'xhtml' => 'application/xhtml+xml',
'xht' => 'application/xhtml+xml',
'zip' => 'application/x-zip',
'rar' => 'application/x-rar',
'mid' => 'audio/midi',
'midi' => 'audio/midi',
'mpga' => 'audio/mpeg',
'mp2' => 'audio/mpeg',
'mp3' => 'audio/mpeg',
'aif' => 'audio/x-aiff',
'aiff' => 'audio/x-aiff',
'aifc' => 'audio/x-aiff',
'ram' => 'audio/x-pn-realaudio',
'rm' => 'audio/x-pn-realaudio',
'rpm' => 'audio/x-pn-realaudio-plugin',
'ra' => 'audio/x-realaudio',
'rv' => 'video/vnd.rn-realvideo',
'wav' => 'audio/x-wav',
'jpg' => 'image/jpeg',
'jpeg' => 'image/jpeg',
'jpe' => 'image/jpeg',
'png' => 'image/png',
'gif' => 'image/gif',
'bmp' => 'image/bmp',
'tiff' => 'image/tiff',
'tif' => 'image/tiff',
'svg' => 'image/svg+xml',
'css' => 'text/css',
'html' => 'text/html',
'htm' => 'text/html',
'shtml' => 'text/html',
'txt' => 'text/plain',
'text' => 'text/plain',
'log' => 'text/plain',
'rtx' => 'text/richtext',
'rtf' => 'text/rtf',
'xml' => 'application/xml',
'xsl' => 'application/xml',
'dmn' => 'application/octet-stream',
'bpmn' => 'application/octet-stream',
'mpeg' => 'video/mpeg',
'mpg' => 'video/mpeg',
'mpe' => 'video/mpeg',
'qt' => 'video/quicktime',
'mov' => 'video/quicktime',
'avi' => 'video/x-msvideo',
'movie' => 'video/x-sgi-movie',
'doc' => 'application/msword',
'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'docm' => 'application/vnd.ms-word.template.macroEnabled.12',
'dot' => 'application/msword',
'dotx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
'word' => 'application/msword',
'xl' => 'application/excel',
'eml' => 'message/rfc822',
'json' => 'application/json',
'pem' => 'application/x-x509-user-cert',
'p10' => 'application/x-pkcs10',
'p12' => 'application/x-pkcs12',
'p7a' => 'application/x-pkcs7-signature',
'p7c' => 'application/pkcs7-mime',
'p7m' => 'application/pkcs7-mime',
'p7r' => 'application/x-pkcs7-certreqresp',
'p7s' => 'application/pkcs7-signature',
'crt' => 'application/x-x509-ca-cert',
'crl' => 'application/pkix-crl',
'der' => 'application/x-x509-ca-cert',
'kdb' => 'application/octet-stream',
'pgp' => 'application/pgp',
'gpg' => 'application/gpg-keys',
'sst' => 'application/octet-stream',
'csr' => 'application/octet-stream',
'rsa' => 'application/x-pkcs7',
'cer' => 'application/pkix-cert',
'3g2' => 'video/3gpp2',
'3gp' => 'video/3gp',
'mp4' => 'video/mp4',
'm4a' => 'audio/x-m4a',
'f4v' => 'video/mp4',
'webm' => 'video/webm',
'aac' => 'audio/x-acc',
'm4u' => 'application/vnd.mpegurl',
'm3u' => 'text/plain',
'xspf' => 'application/xspf+xml',
'vlc' => 'application/videolan',
'wmv' => 'video/x-ms-wmv',
'au' => 'audio/x-au',
'ac3' => 'audio/ac3',
'flac' => 'audio/x-flac',
'ogg' => 'audio/ogg',
'kmz' => 'application/vnd.google-earth.kmz',
'kml' => 'application/vnd.google-earth.kml+xml',
'ics' => 'text/calendar',
'zsh' => 'text/x-scriptzsh',
'7zip' => 'application/x-7z-compressed',
'cdr' => 'application/cdr',
'wma' => 'audio/x-ms-wma',
'jar' => 'application/java-archive',
'tex' => 'application/x-tex',
'latex' => 'application/x-latex',
'odt' => 'application/vnd.oasis.opendocument.text',
'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
'odp' => 'application/vnd.oasis.opendocument.presentation',
'odg' => 'application/vnd.oasis.opendocument.graphics',
'odc' => 'application/vnd.oasis.opendocument.chart',
'odf' => 'application/vnd.oasis.opendocument.formula',
'odi' => 'application/vnd.oasis.opendocument.image',
'odm' => 'application/vnd.oasis.opendocument.text-master',
'odb' => 'application/vnd.oasis.opendocument.database',
'ott' => 'application/vnd.oasis.opendocument.text-template',
];
return static::$extensionToMimeTypeMap;
}
}