Skip to content

Commit f4593e8

Browse files
committed
Fixed Bugs Url
1 parent 44baeac commit f4593e8

File tree

3 files changed

+52
-15
lines changed

3 files changed

+52
-15
lines changed

src/actions/FileAction.php

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,6 @@ class FileAction extends Action
3939
*/
4040
public $component = 'fs';
4141

42-
/**
43-
* @return \diecoding\flysystem\AbstractComponent|mixed
44-
*/
45-
public function getFilesystem()
46-
{
47-
return Yii::$app->get($this->component);
48-
}
49-
5042
/**
5143
* Runs the action.
5244
*
@@ -56,18 +48,21 @@ public function getFilesystem()
5648
*/
5749
public function run($data = null)
5850
{
51+
/** @var \diecoding\flysystem\AbstractComponent|mixed $filesystem */
52+
$filesystem = Yii::$app->get($this->component);
53+
5954
try {
60-
$params = Json::decode($this->getFilesystem()->decrypt($data));
55+
$params = Json::decode($filesystem->decrypt($data));
6156

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

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

69-
$content = $this->getFilesystem()->read($params['path']);
70-
$mimeType = $this->getFilesystem()->mimeType($params['path']);
64+
$content = $filesystem->read($params['path']);
65+
$mimeType = $filesystem->mimeType($params['path']);
7166
$attachmentName = (string) pathinfo($params['path'], PATHINFO_BASENAME);
7267
} catch (\Throwable $th) {
7368
throw new NotFoundHttpException(Yii::t('yii', 'Page not found.'));

src/adapter/ZipArchiveAdapter.php

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44

55
namespace diecoding\flysystem\adapter;
66

7+
use DateTimeInterface;
8+
use diecoding\flysystem\AbstractComponent;
79
use diecoding\flysystem\traits\ChecksumAdapterTrait;
8-
use diecoding\flysystem\traits\UrlGeneratorAdapterTrait;
10+
use diecoding\flysystem\traits\UrlGeneratorComponentTrait;
911
use Generator;
1012
use League\Flysystem\ChecksumProvider;
1113
use League\Flysystem\Config;
@@ -30,6 +32,8 @@
3032
use League\MimeTypeDetection\FinfoMimeTypeDetector;
3133
use League\MimeTypeDetection\MimeTypeDetector;
3234
use Throwable;
35+
use yii\helpers\Json;
36+
use yii\helpers\Url;
3337
use ZipArchive;
3438

3539
use function fclose;
@@ -42,7 +46,7 @@
4246
*/
4347
final class ZipArchiveAdapter implements FilesystemAdapter, ChecksumProvider, PublicUrlGenerator, TemporaryUrlGenerator
4448
{
45-
use UrlGeneratorAdapterTrait, ChecksumAdapterTrait;
49+
use ChecksumAdapterTrait;
4650

4751
private PathPrefixer $pathPrefixer;
4852
private MimeTypeDetector $mimeTypeDetector;
@@ -444,4 +448,33 @@ private function setVisibilityAttribute(string $statsName, string $visibility, Z
444448

445449
return $archive->setExternalAttributesName($statsName, ZipArchive::OPSYS_UNIX, $visibility << 16);
446450
}
451+
452+
// =================================================
453+
454+
/**
455+
* @var UrlGeneratorComponentTrait|AbstractComponent
456+
*/
457+
public $component;
458+
459+
public function publicUrl(string $path, Config $config): string
460+
{
461+
// TODO: Use absolute path and don't encrypt
462+
$params = [
463+
'path' => $path,
464+
'expires' => 0,
465+
];
466+
467+
return Url::toRoute([$this->component->action, 'data' => $this->component->encrypt(Json::encode($params))], true);
468+
}
469+
470+
public function temporaryUrl(string $path, DateTimeInterface $expiresAt, Config $config): string
471+
{
472+
// TODO: Use absolute path and don't encrypt
473+
$params = [
474+
'path' => $path,
475+
'expires' => (int) $expiresAt->getTimestamp(),
476+
];
477+
478+
return Url::toRoute([$this->component->action, 'data' => $this->component->encrypt(Json::encode($params))], true);
479+
}
447480
}

src/traits/UrlGeneratorAdapterTrait.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use DateTimeInterface;
66
use diecoding\flysystem\AbstractComponent;
77
use League\Flysystem\Config;
8+
use League\Flysystem\PathPrefixer;
89
use yii\helpers\Json;
910
use yii\helpers\Url;
1011

@@ -18,13 +19,17 @@
1819
trait UrlGeneratorAdapterTrait
1920
{
2021
/**
21-
* @var UrlGeneratorComponentTrait
22+
* @var UrlGeneratorComponentTrait|AbstractComponent
2223
*/
2324
public $component;
2425

2526
public function publicUrl(string $path, /** @scrutinizer ignore-unused */Config $config): string
2627
{
2728
// TODO: Use absolute path and don't encrypt
29+
if ($this->component->prefix) {
30+
$prefixer = new PathPrefixer($this->component->prefix);
31+
$path = $prefixer->stripPrefix($path);
32+
}
2833
$params = [
2934
'path' => $path,
3035
'expires' => 0,
@@ -36,6 +41,10 @@ public function publicUrl(string $path, /** @scrutinizer ignore-unused */Config
3641
public function temporaryUrl(string $path, DateTimeInterface $expiresAt, /** @scrutinizer ignore-unused */Config $config): string
3742
{
3843
// TODO: Use absolute path and don't encrypt
44+
if ($this->component->prefix) {
45+
$prefixer = new PathPrefixer($this->component->prefix);
46+
$path = $prefixer->stripPrefix($path);
47+
}
3948
$params = [
4049
'path' => $path,
4150
'expires' => (int) $expiresAt->getTimestamp(),

0 commit comments

Comments
 (0)