Skip to content

Commit

Permalink
add(Configuration, extend): something
Browse files Browse the repository at this point in the history
  • Loading branch information
Burial0268 committed Aug 2, 2024
1 parent 2ac6f2a commit 424a473
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 18 deletions.
7 changes: 7 additions & 0 deletions extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@
(new Extend\Event())
->listen(Collecting::class, AdapterRegisterListener::class)
->listen(Instantiate::class, AdapterInstantiateListener::class),

(new Extend\Settings())
->default('gbcl-fof-upload-qcloud.qcloudConfig.secretKey', '')
->default('gbcl-fof-upload-qcloud.qcloudConfig.secretId', '')
->default('gbcl-fof-upload-qcloud.qcloudConfig.bucket', '')
->default('gbcl-fof-upload-qcloud.qcloudConfig.appId', '')
->default('gbcl-fof-upload-qcloud.qcloudConfig.domain', ''),

(new Extend\Frontend('admin'))
->js(__DIR__.'/js/dist/admin.js'),
Expand Down
5 changes: 4 additions & 1 deletion src/Adapters/QcloudFofAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ class QcloudFofAdapter extends Flysystem implements UploadAdapter
*/
public function __construct($pluginConfig)
{
$config = new QcloudConfiguration();
/**
* @var QcloudConfiguration $pluginConfig
*/
$config = app(QcloudConfiguration::class);

$arrConfig = [
'region' => $config->region,
Expand Down
34 changes: 17 additions & 17 deletions src/Configuration/QcloudConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Exception;
use Flarum\Settings\SettingsRepositoryInterface;
use FoF\Upload\File;
use FoF\Upload\Helpers\Util;

class QcloudConfiguration
{
Expand All @@ -23,13 +24,8 @@ class QcloudConfiguration
public string $fileSignatureTokenName;
public $fileSignatureTime;

public function __construct()
public function __construct(private SettingsRepositoryInterface $settings, private Util $util)
{
/**
* @var SettingsRepositoryInterface $settings
*/
$settings = app(SettingsRepositoryInterface::class);

$this->region = $settings->get('gbcl-fof-upload-qcloud.qcloudConfig.region', 'ap-beijing');
$this->secretId = $settings->get('gbcl-fof-upload-qcloud.qcloudConfig.secretId');
$this->secretKey = $settings->get('gbcl-fof-upload-qcloud.qcloudConfig.secretKey');
Expand All @@ -41,17 +37,9 @@ public function __construct()
$this->fileSignatureTokenName = $settings->get('gbcl-fof-upload-qcloud.qcloudConfig.fileRetrievingSignatureTokenName', 'sign');
$this->fileSignatureTime = $settings->get('gbcl-fof-upload-qcloud.qcloudConfig.fileRetrievingSignatureTime', '1800');

if ($this->region == null || strlen($this->region) == 0) {
$this->region = 'ap-beijing';
}
if ($this->fileSignatureTime == null || $this->fileSignatureTime == 0) {
$this->fileSignatureTime = '1800';
}
if ($this->useHttps == 'enableTls') {
$this->useHttps = 'true';
} else {
$this->useHttps = 'false';
}
$this->region = (empty($this->region)) ? 'ap-beijing' : $this->region;
$this->fileSignatureTime = ($this->fileSignatureTime === null || $this->fileSignatureTime === 0) ? '1800' : $this->fileSignatureTime;
$this->useHttps = ($this->useHttps === 'enableTls') ? 'true' : 'false';
}

/**
Expand All @@ -62,6 +50,17 @@ public function needSignature(): bool
return $this->fileSignatureToken != null && strlen($this->fileSignatureToken) > 0;
}

public function isAdapterAndTemplateEnabled(): bool
{
$mimeTypesConfig = $this->util->getMimeTypesConfiguration();
return $mimeTypesConfig->filter(function ($item) {
return is_array($item) &&
isset($item['adapter'], $item['template']) &&
strpos($item['adapter'], 'qcloud') !== false &&
strpos($item['template'], 'qcloud-') === 0;
})->isNotEmpty();
}

/**
* @param $signPath string
*
Expand Down Expand Up @@ -92,6 +91,7 @@ public function signPath(string $signPath): string
public function generateUrl(File $file): string
{
if ($this->needSignature()) {
if (!$this->isAdapterAndTemplateEnabled()) throw new \InvalidArgumentException('You need to enable adapter and template for qcloud');
return $this->cdn.$this->signPath('/'.$file->path);
} else {
return $this->cdn.'/'.$file->path;
Expand Down

0 comments on commit 424a473

Please sign in to comment.