Primo Committ
This commit is contained in:
122
vendor/league/flysystem/src/Util/ContentListingFormatter.php
vendored
Normal file
122
vendor/league/flysystem/src/Util/ContentListingFormatter.php
vendored
Normal file
@@ -0,0 +1,122 @@
|
||||
<?php
|
||||
|
||||
namespace League\Flysystem\Util;
|
||||
|
||||
use League\Flysystem\Util;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
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, $caseSensitive = true)
|
||||
{
|
||||
$this->directory = rtrim($directory, '/');
|
||||
$this->recursive = $recursive;
|
||||
$this->caseSensitive = $caseSensitive;
|
||||
}
|
||||
|
||||
/**
|
||||
* Format contents listing.
|
||||
*
|
||||
* @param array $listing
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function formatListing(array $listing)
|
||||
{
|
||||
$listing = array_filter(array_map([$this, 'addPathInfo'], $listing), [$this, 'isEntryOutOfScope']);
|
||||
|
||||
return $this->sortListing(array_values($listing));
|
||||
}
|
||||
|
||||
private function addPathInfo(array $entry)
|
||||
{
|
||||
return $entry + Util::pathinfo($entry['path']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the entry is out of scope.
|
||||
*
|
||||
* @param array $entry
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function isEntryOutOfScope(array $entry)
|
||||
{
|
||||
if (empty($entry['path']) && $entry['path'] !== '0') {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($this->recursive) {
|
||||
return $this->residesInDirectory($entry);
|
||||
}
|
||||
|
||||
return $this->isDirectChild($entry);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the entry resides within the parent directory.
|
||||
*
|
||||
* @param array $entry
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function residesInDirectory(array $entry)
|
||||
{
|
||||
if ($this->directory === '') {
|
||||
return true;
|
||||
}
|
||||
|
||||
return $this->caseSensitive
|
||||
? strpos($entry['path'], $this->directory . '/') === 0
|
||||
: stripos($entry['path'], $this->directory . '/') === 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the entry is a direct child of the directory.
|
||||
*
|
||||
* @param array $entry
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function isDirectChild(array $entry)
|
||||
{
|
||||
return $this->caseSensitive
|
||||
? $entry['dirname'] === $this->directory
|
||||
: strcasecmp($this->directory, $entry['dirname']) === 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $listing
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function sortListing(array $listing)
|
||||
{
|
||||
usort($listing, function ($a, $b) {
|
||||
return strcasecmp($a['path'], $b['path']);
|
||||
});
|
||||
|
||||
return $listing;
|
||||
}
|
||||
}
|
||||
80
vendor/league/flysystem/src/Util/MimeType.php
vendored
Normal file
80
vendor/league/flysystem/src/Util/MimeType.php
vendored
Normal file
@@ -0,0 +1,80 @@
|
||||
<?php
|
||||
|
||||
namespace League\Flysystem\Util;
|
||||
|
||||
use League\MimeTypeDetection\FinfoMimeTypeDetector;
|
||||
use League\MimeTypeDetection\GeneratedExtensionToMimeTypeMap;
|
||||
use League\MimeTypeDetection\MimeTypeDetector;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
class MimeType
|
||||
{
|
||||
protected static $extensionToMimeTypeMap = GeneratedExtensionToMimeTypeMap::MIME_TYPES_FOR_EXTENSIONS;
|
||||
protected static $detector;
|
||||
|
||||
public static function useDetector(MimeTypeDetector $detector)
|
||||
{
|
||||
static::$detector = $detector;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return MimeTypeDetector
|
||||
*/
|
||||
protected static function detector()
|
||||
{
|
||||
if ( ! static::$detector instanceof MimeTypeDetector) {
|
||||
static::$detector = new FinfoMimeTypeDetector();
|
||||
}
|
||||
|
||||
return static::$detector;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Detects MIME Type based on given content.
|
||||
*
|
||||
* @param mixed $content
|
||||
*
|
||||
* @return string MIME Type
|
||||
*/
|
||||
public static function detectByContent($content)
|
||||
{
|
||||
if (is_string($content)) {
|
||||
return static::detector()->detectMimeTypeFromBuffer($content);
|
||||
}
|
||||
|
||||
return 'text/plain';
|
||||
}
|
||||
|
||||
/**
|
||||
* Detects MIME Type based on file extension.
|
||||
*
|
||||
* @param string $extension
|
||||
*
|
||||
* @return string MIME Type
|
||||
*/
|
||||
public static function detectByFileExtension($extension)
|
||||
{
|
||||
return static::detector()->detectMimeTypeFromPath('artificial.' . $extension) ?: 'text/plain';
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $filename
|
||||
*
|
||||
* @return string MIME Type
|
||||
*/
|
||||
public static function detectByFilename($filename)
|
||||
{
|
||||
return static::detector()->detectMimeTypeFromPath($filename) ?: 'text/plain';
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array Map of file extension to MIME Type
|
||||
*/
|
||||
public static function getExtensionToMimeTypeMap()
|
||||
{
|
||||
return static::$extensionToMimeTypeMap;
|
||||
}
|
||||
}
|
||||
36
vendor/league/flysystem/src/Util/StreamHasher.php
vendored
Normal file
36
vendor/league/flysystem/src/Util/StreamHasher.php
vendored
Normal file
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace League\Flysystem\Util;
|
||||
|
||||
class StreamHasher
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $algo;
|
||||
|
||||
/**
|
||||
* StreamHasher constructor.
|
||||
*
|
||||
* @param string $algo
|
||||
*/
|
||||
public function __construct($algo)
|
||||
{
|
||||
$this->algo = $algo;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param resource $resource
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function hash($resource)
|
||||
{
|
||||
rewind($resource);
|
||||
$context = hash_init($this->algo);
|
||||
hash_update_stream($context, $resource);
|
||||
fclose($resource);
|
||||
|
||||
return hash_final($context);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user