Skip to content

Commit

Permalink
✨ Add File Type on URL
Browse files Browse the repository at this point in the history
  • Loading branch information
sugeng-sulistiyawan committed Oct 25, 2024
1 parent c0a77e7 commit cc7e6cb
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
12 changes: 10 additions & 2 deletions src/actions/FileAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
*/
class FileAction extends Action
{
const FILENAME_SEPARATOR = 'fname-';

/**
* @var string filesystem config component (fs)
*/
Expand All @@ -52,7 +54,9 @@ public function run($data = null)
$filesystem = Yii::$app->get($this->component);

try {
$params = Json::decode($filesystem->/** @scrutinizer ignore-call */decrypt($data));
[$dataEncrypt, $filename] = explode(self::FILENAME_SEPARATOR, $data);

$params = Json::decode($filesystem->/** @scrutinizer ignore-call */decrypt($dataEncrypt));

$now = (int) (new DateTimeImmutable())->getTimestamp();
$expires = (int) $params['expires'];
Expand All @@ -61,9 +65,13 @@ public function run($data = null)
throw new NotFoundHttpException(Yii::t('yii', 'Page not found.'));
}

$attachmentName = (string) pathinfo($params['path'], PATHINFO_BASENAME);
if ($filename !== $attachmentName) {
throw new NotFoundHttpException(Yii::t('yii', 'Page not found.'));
}

$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
13 changes: 11 additions & 2 deletions src/traits/UrlGeneratorAdapterTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use DateTimeInterface;
use diecoding\flysystem\AbstractComponent;
use diecoding\flysystem\actions\FileAction;
use League\Flysystem\Config;
use League\Flysystem\PathPrefixer;
use yii\helpers\Json;
Expand Down Expand Up @@ -40,7 +41,7 @@ public function publicUrl(string $path, /** @scrutinizer ignore-unused */Config
'expires' => 0,
];

return Url::toRoute([$this->component->action, 'data' => $this->component->/** @scrutinizer ignore-call */encrypt(Json::encode($params))], true);
return $this->generateUrlAction($params);
}

public function temporaryUrl(string $path, DateTimeInterface $expiresAt, /** @scrutinizer ignore-unused */Config $config): string
Expand All @@ -55,6 +56,14 @@ public function temporaryUrl(string $path, DateTimeInterface $expiresAt, /** @sc
'expires' => (int) $expiresAt->getTimestamp(),
];

return Url::toRoute([$this->component->action, 'data' => $this->component->/** @scrutinizer ignore-call */encrypt(Json::encode($params))], true);
return $this->generateUrlAction($params);
}

private function generateUrlAction(array $params): string
{
$attachmentName = (string) pathinfo($params['path'], PATHINFO_BASENAME);
$data = $this->component->/** @scrutinizer ignore-call */encrypt(Json::encode($params)) . FileAction::FILENAME_SEPARATOR . $attachmentName;

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

0 comments on commit cc7e6cb

Please sign in to comment.