Skip to content

Commit

Permalink
✨ Improve ZipArchiveAdapter
Browse files Browse the repository at this point in the history
  • Loading branch information
sugeng-sulistiyawan committed Oct 20, 2024
1 parent 1b512a3 commit 59b847e
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 56 deletions.
141 changes: 87 additions & 54 deletions src/adapter/ZipArchiveAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,52 +4,32 @@

namespace diecoding\flysystem\adapter;

use DateTimeInterface;
use diecoding\flysystem\traits\ChecksumAdapterTrait;
use diecoding\flysystem\traits\UrlGeneratorAdapterTrait;
use League\Flysystem\ChecksumProvider;
use League\Flysystem\Config;
use League\Flysystem\FileAttributes;
use League\Flysystem\FilesystemAdapter;
use League\Flysystem\UnixVisibility\VisibilityConverter;
use League\Flysystem\UrlGeneration\PublicUrlGenerator;
use League\Flysystem\UrlGeneration\TemporaryUrlGenerator;
use League\Flysystem\ZipArchive\ZipArchiveAdapter as LeagueZipArchiveAdapter;
use League\Flysystem\ZipArchive\ZipArchiveProvider;
use League\MimeTypeDetection\MimeTypeDetector;
use yii\helpers\Json;
use yii\helpers\Url;

/**
* @method bool fileExists(string $location)
* @method bool directoryExists(string $location)
* @method bool has(string $location) check fileExists or directoryExists
* @method void write(string $location, string $contents, array $config = [])
* @method void writeStream(string $location, $contents, array $config = [])
* @method string read(string $location)
* @method resource readStream(string $location)
* @method void delete(string $location)
* @method void deleteDirectory(string $location)
* @method void createDirectory(string $location, array $config = [])
* @method \League\Flysystem\DirectoryListing listContents(string $location, bool = \League\Flysystem\Filesystem::LIST_SHALLOW)
* @method void move(string $source, string $destination, array $config = [])
* @method void copy(string $source, string $destination, array $config = [])
* @method int lastModified(string $path)
* @method int fileSize(string $path)
* @method string mimeType(string $path)
* @method void setVisibility(string $path, string $visibility)
* @method string visibility(string $path)
*
* @link https://sugengsulistiyawan.my.id/
* @author Sugeng Sulistiyawan <[email protected]>
* @copyright Copyright (c) 2024
*/
final class ZipArchiveAdapter implements ChecksumProvider, PublicUrlGenerator, TemporaryUrlGenerator

final class ZipArchiveAdapter implements FilesystemAdapter, ChecksumProvider, PublicUrlGenerator, TemporaryUrlGenerator
{
use UrlGeneratorAdapterTrait, ChecksumAdapterTrait;

/**
* @var bool
*/
protected $skipPrefixer = true;

/**
* @var LeagueZipArchiveAdapter
*/
protected $_adapter;
private $_adapter;

public function __construct(
private ZipArchiveProvider $zipArchiveProvider,
Expand All @@ -61,16 +41,6 @@ public function __construct(
$this->setAdapter(new LeagueZipArchiveAdapter($zipArchiveProvider, $root, $mimeTypeDetector, $visibility, $detectMimeTypeUsingPath));
}

/**
* @param string $method
* @param array $parameters
* @return mixed
*/
public function __call($method, $parameters)
{
return call_user_func_array([$this->getAdapter(), $method], $parameters);
}

/**
* @return LeagueZipArchiveAdapter
*/
Expand All @@ -88,25 +58,88 @@ public function setAdapter(LeagueZipArchiveAdapter $value)
$this->_adapter = $value;
}

public function publicUrl(string $path, Config $config): string
public function fileExists(string $path): bool
{
return $this->getAdapter()->fileExists($path);
}

public function write(string $path, string $contents, Config $config): void
{
$this->getAdapter()->write($path, $contents, $config);
}

public function writeStream(string $path, $contents, Config $config): void
{
$this->getAdapter()->writeStream($path, $contents, $config);
}

public function read(string $path): string
{
return $this->getAdapter()->read($path);
}

public function readStream(string $path)
{
return $this->getAdapter()->readStream($path);
}

public function delete(string $path): void
{
$this->getAdapter()->delete($path);
}

public function deleteDirectory(string $path): void
{
$this->getAdapter()->deleteDirectory($path);
}

public function createDirectory(string $path, Config $config): void
{
$this->getAdapter()->createDirectory($path, $config);
}

public function directoryExists(string $path): bool
{
return $this->getAdapter()->directoryExists($path);
}

public function setVisibility(string $path, string $visibility): void
{
$this->getAdapter()->setVisibility($path, $visibility);
}

public function visibility(string $path): FileAttributes
{
// TODO: Use absolute path and don't encrypt
$params = [
'path' => $path,
'expires' => 0,
];
return $this->getAdapter()->visibility($path);
}

return Url::toRoute([$this->component?->action, 'data' => $this->component?->encrypt(Json::encode($params))], true);
public function mimeType(string $path): FileAttributes
{
return $this->getAdapter()->mimeType($path);
}

public function temporaryUrl(string $path, DateTimeInterface $expiresAt, Config $config): string
public function lastModified(string $path): FileAttributes
{
// TODO: Use absolute path and don't encrypt
$params = [
'path' => $path,
'expires' => (int) $expiresAt->getTimestamp(),
];
return $this->getAdapter()->lastModified($path);
}

return Url::toRoute([$this->component?->action, 'data' => $this->component?->encrypt(Json::encode($params))], true);
public function fileSize(string $path): FileAttributes
{
return $this->getAdapter()->fileSize($path);
}

public function listContents(string $path, bool $deep): iterable
{
return $this->getAdapter()->listContents($path, $deep);
}

public function move(string $source, string $destination, Config $config): void
{
$this->getAdapter()->move($source, $destination, $config);
}

public function copy(string $source, string $destination, Config $config): void
{
$this->getAdapter()->copy($source, $destination, $config);
}
}
9 changes: 7 additions & 2 deletions src/traits/UrlGeneratorAdapterTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,15 @@ trait UrlGeneratorAdapterTrait
*/
public $component;

/**
* @var bool
*/
protected $skipPrefixer = false;

public function publicUrl(string $path, /** @scrutinizer ignore-unused */Config $config): string
{
// TODO: Use absolute path and don't encrypt
if ($this->component?->prefix) {
if ($this->skipPrefixer !== false && $this->component?->prefix) {
$prefixer = new PathPrefixer((string) $this->component?->prefix);
$path = $prefixer->stripPrefix($path);
}
Expand All @@ -41,7 +46,7 @@ public function publicUrl(string $path, /** @scrutinizer ignore-unused */Config
public function temporaryUrl(string $path, DateTimeInterface $expiresAt, /** @scrutinizer ignore-unused */Config $config): string
{
// TODO: Use absolute path and don't encrypt
if ($this->component?->prefix) {
if ($this->skipPrefixer !== false && $this->component?->prefix) {
$prefixer = new PathPrefixer((string) $this->component?->prefix);
$path = $prefixer->stripPrefix($path);
}
Expand Down

0 comments on commit 59b847e

Please sign in to comment.