Skip to content

Commit

Permalink
Fixed Bugs Url
Browse files Browse the repository at this point in the history
  • Loading branch information
sugeng-sulistiyawan committed Oct 13, 2023
1 parent 44baeac commit f4593e8
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 15 deletions.
19 changes: 7 additions & 12 deletions src/actions/FileAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,6 @@ class FileAction extends Action
*/
public $component = 'fs';

/**
* @return \diecoding\flysystem\AbstractComponent|mixed
*/
public function getFilesystem()
{
return Yii::$app->get($this->component);
}

/**
* Runs the action.
*
Expand All @@ -56,18 +48,21 @@ public function getFilesystem()
*/
public function run($data = null)
{
/** @var \diecoding\flysystem\AbstractComponent|mixed $filesystem */
$filesystem = Yii::$app->get($this->component);

try {
$params = Json::decode($this->getFilesystem()->decrypt($data));
$params = Json::decode($filesystem->decrypt($data));

$now = (int) (new DateTimeImmutable())->getTimestamp();
$expires = (int) $params['expires'];

if (!$this->getFilesystem()->fileExists($params['path']) || ($expires > 0 && $expires < $now)) {
if (!$filesystem->fileExists($params['path']) || ($expires > 0 && $expires < $now)) {
throw new NotFoundHttpException(Yii::t('yii', 'Page not found.'));
}

$content = $this->getFilesystem()->read($params['path']);
$mimeType = $this->getFilesystem()->mimeType($params['path']);
$content = $filesystem->read($params['path']);
$mimeType = $filesystem->mimeType($params['path']);
$attachmentName = (string) pathinfo($params['path'], PATHINFO_BASENAME);
} catch (\Throwable $th) {
throw new NotFoundHttpException(Yii::t('yii', 'Page not found.'));
Expand Down
37 changes: 35 additions & 2 deletions src/adapter/ZipArchiveAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

namespace diecoding\flysystem\adapter;

use DateTimeInterface;
use diecoding\flysystem\AbstractComponent;
use diecoding\flysystem\traits\ChecksumAdapterTrait;
use diecoding\flysystem\traits\UrlGeneratorAdapterTrait;
use diecoding\flysystem\traits\UrlGeneratorComponentTrait;
use Generator;
use League\Flysystem\ChecksumProvider;
use League\Flysystem\Config;
Expand All @@ -30,6 +32,8 @@
use League\MimeTypeDetection\FinfoMimeTypeDetector;
use League\MimeTypeDetection\MimeTypeDetector;
use Throwable;
use yii\helpers\Json;
use yii\helpers\Url;
use ZipArchive;

use function fclose;
Expand All @@ -42,7 +46,7 @@
*/
final class ZipArchiveAdapter implements FilesystemAdapter, ChecksumProvider, PublicUrlGenerator, TemporaryUrlGenerator
{
use UrlGeneratorAdapterTrait, ChecksumAdapterTrait;
use ChecksumAdapterTrait;

private PathPrefixer $pathPrefixer;
private MimeTypeDetector $mimeTypeDetector;
Expand Down Expand Up @@ -444,4 +448,33 @@ private function setVisibilityAttribute(string $statsName, string $visibility, Z

return $archive->setExternalAttributesName($statsName, ZipArchive::OPSYS_UNIX, $visibility << 16);
}

// =================================================

/**
* @var UrlGeneratorComponentTrait|AbstractComponent
*/
public $component;

public function publicUrl(string $path, Config $config): string
{
// TODO: Use absolute path and don't encrypt
$params = [
'path' => $path,
'expires' => 0,
];

return Url::toRoute([$this->component->action, 'data' => $this->component->encrypt(Json::encode($params))], true);
}

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

return Url::toRoute([$this->component->action, 'data' => $this->component->encrypt(Json::encode($params))], true);
}
}
11 changes: 10 additions & 1 deletion src/traits/UrlGeneratorAdapterTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use DateTimeInterface;
use diecoding\flysystem\AbstractComponent;
use League\Flysystem\Config;
use League\Flysystem\PathPrefixer;
use yii\helpers\Json;
use yii\helpers\Url;

Expand All @@ -18,13 +19,17 @@
trait UrlGeneratorAdapterTrait
{
/**
* @var UrlGeneratorComponentTrait
* @var UrlGeneratorComponentTrait|AbstractComponent
*/
public $component;

public function publicUrl(string $path, /** @scrutinizer ignore-unused */Config $config): string
{
// TODO: Use absolute path and don't encrypt
if ($this->component->prefix) {
$prefixer = new PathPrefixer($this->component->prefix);
$path = $prefixer->stripPrefix($path);
}
$params = [
'path' => $path,
'expires' => 0,
Expand All @@ -36,6 +41,10 @@ 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) {
$prefixer = new PathPrefixer($this->component->prefix);
$path = $prefixer->stripPrefix($path);
}
$params = [
'path' => $path,
'expires' => (int) $expiresAt->getTimestamp(),
Expand Down

0 comments on commit f4593e8

Please sign in to comment.