From 65d0f2242c0857f22168156e1bf999ecfbbd77f4 Mon Sep 17 00:00:00 2001 From: Sam Wilson Date: Mon, 6 Sep 2021 16:30:18 +0800 Subject: [PATCH] Move filesystem config into .env instead of Settings --- .env | 9 +++++ config/services.yaml | 13 +++++- docs/config.rst | 55 ++++++++++++++++++++++--- src/Filesystems.php | 67 ++++++++++++++++++++++++------- src/Settings.php | 45 --------------------- templates/setting/index.html.twig | 44 -------------------- 6 files changed, 122 insertions(+), 111 deletions(-) diff --git a/.env b/.env index aa04cb0..27b52a9 100644 --- a/.env +++ b/.env @@ -35,6 +35,15 @@ MAILER_DSN=smtp://localhost APP_MAIN_CONTACT=1 APP_REQUIRE_2FA=true +APP_FS_DATA_STORE=local +APP_FS_DATA_DIR= +APP_FS_TEMP_DIR= +APP_FS_AWS_REGION= +APP_FS_AWS_ENDPOINT= +APP_FS_AWS_BUCKET= +APP_FS_AWS_KEY= +APP_FS_AWS_SECRET= + APP_MAIL_SENDER=user@example.org APP_LOG_RECIPIENT=user@example.org diff --git a/config/services.yaml b/config/services.yaml index fc73138..27db40e 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -29,10 +29,21 @@ services: App\Settings: arguments: - $projectDir: '%kernel.project_dir%' $mailFrom: '%env(APP_MAIL_SENDER)%' $mainContactId: '%env(APP_MAIN_CONTACT)%' + App\Filesystems: + arguments: + $projectDir: '%kernel.project_dir%' + $dataStore: '%env(APP_FS_DATA_STORE)%' + $dataDir: '%env(APP_FS_DATA_DIR)%' + $tempDir: '%env(APP_FS_TEMP_DIR)%' + $awsRegion: '%env(APP_FS_AWS_REGION)%' + $awsEndpoint: '%env(APP_FS_AWS_ENDPOINT)%' + $awsBucket: '%env(APP_FS_AWS_BUCKET)%' + $awsKey: '%env(APP_FS_AWS_KEY)%' + $awsSecret: '%env(APP_FS_AWS_SECRET)%' + App\WebRequestProcessor: tags: { name: monolog.processor } autowire: true diff --git a/docs/config.rst b/docs/config.rst index 26651a1..2ae244e 100644 --- a/docs/config.rst +++ b/docs/config.rst @@ -4,12 +4,55 @@ Configuration ============= All the important parts of configuration of a Twyne site -happen in the ``.env.local`` file:: +happen in the ``.env.local`` file. +The minimum that should be set are as follows: - APP_SECRET=a-long-random-string - APP_MAIL_SENDER=admin@example.org - APP_LOG_RECIPIENT=admin@example.org - MAILER_DSN=smtp://username:password@smtp.gmail.com +.. code-block:: shell -Other configuration happens on the Settings page, + APP_SECRET=a-long-random-string + APP_MAIL_SENDER=admin@example.org + APP_LOG_RECIPIENT=admin@example.org + MAILER_DSN=smtp://username:password@smtp.gmail.com + +Other configuration +(that is likely to be changed occasionally during the normal operation of the site) +happens on the Settings page, accessible via the main menu. + +Uploaded files +-------------- + +There are two options for file storage: the local filesystem, and S3_-compatible object stores +(such as those from AWS, Digital Ocean, OVH, Dreamhost, etc.). + +.. _S3: https://en.wikipedia.org/wiki/Amazon_S3 + +The local filesystem is the default, and files will be stored in the ``var/app_data/`` directory. +This location can be changed via ``.env.local``: + +.. code-block:: shell + + APP_FS_DATA_DIR=/path/to/your/data_dir/ + +To use S3-compatible storage, set the following environment variables: + +.. code-block:: shell + + APP_FS_DATA_STORE=aws + APP_FS_AWS_REGION= + APP_FS_AWS_ENDPOINT= + APP_FS_AWS_BUCKET= + APP_FS_AWS_KEY= + APP_FS_AWS_SECRET= + +Uploaded files will have various derivative files created for them, such as thumbnails of images. +These files will be stored in the ``var/app_temp/`` directory by default. +This directory can be changed in ``.env.local``: + +.. code-block:: shell + + APP_FS_TEMP_DIR=/path/to/your/tmp_dir/ + +The temporary directory contains only files that will be regenerated as required +(hence the name 'temporary'; these are however long-lived files). +There is no need to back up this directory. diff --git a/src/Filesystems.php b/src/Filesystems.php index 376ab55..ddd23de 100644 --- a/src/Filesystems.php +++ b/src/Filesystems.php @@ -13,12 +13,49 @@ class Filesystems { - /** @var Settings */ - private $settings; - - public function __construct(Settings $settings) - { - $this->settings = $settings; + /** @var string */ + private $dataStore; + + /** @var string */ + private $dataDir; + + /** @var string */ + private $tempDir; + + /** @var string */ + private $awsRegion; + + /** @var string */ + private $awsEndpoint; + + /** @var string */ + private $awsBucket; + + /** @var string */ + private $awsKey; + + /** @var string */ + private $awsSecret; + + public function __construct( + string $projectDir, + string $dataStore, + string $dataDir, + string $tempDir, + string $awsRegion, + string $awsEndpoint, + string $awsBucket, + string $awsKey, + string $awsSecret + ) { + $this->dataStore = $dataStore; + $this->dataDir = rtrim(!empty($dataDir) ? $dataDir : $projectDir . '/var/app_data', '/') . '/'; + $this->tempDir = rtrim(!empty($tempDir) ? $tempDir : $projectDir . '/var/app_tmp/', '/') . '/'; + $this->awsRegion = $awsRegion; + $this->awsEndpoint = $awsEndpoint; + $this->awsBucket = $awsBucket; + $this->awsKey = $awsKey; + $this->awsSecret = $awsSecret; } private function getDataStoragePath(File $file): string @@ -109,31 +146,31 @@ public function read(File $file, string $size) public function tempRoot(): string { - return $this->settings->tempDir(); + return $this->tempDir; } public function temp(): Filesystem { - $adapter = new Local($this->settings->tempDir()); + $adapter = new Local($this->tempRoot()); return new Filesystem($adapter); } public function data(): Filesystem { - if ($this->settings->dataStore() === 'aws') { + if ($this->dataStore === 'aws') { $options = [ 'version' => '2006-03-01', 'credentials' => [ - 'key' => $this->settings->awsKey(), - 'secret' => $this->settings->awsSecret(), + 'key' => $this->awsKey, + 'secret' => $this->awsSecret, ], - 'endpoint' => $this->settings->awsEndpoint(), - 'region' => $this->settings->awsRegion(), + 'endpoint' => $this->awsEndpoint, + 'region' => $this->awsRegion, ]; $client = new S3Client($options); - $adapter = new AwsS3Adapter($client, $this->settings->awsBucketName()); + $adapter = new AwsS3Adapter($client, $this->awsBucket); } else { - $adapter = new Local($this->settings->dataDir()); + $adapter = new Local($this->dataDir); } return new Filesystem($adapter); } diff --git a/src/Settings.php b/src/Settings.php index b1aecf8..a876e5a 100644 --- a/src/Settings.php +++ b/src/Settings.php @@ -26,9 +26,6 @@ class Settings /** @var EntityManagerInterface */ private $entityManager; - /** @var string */ - private $projectDir; - /** @var string */ private $mailFrom; @@ -40,14 +37,12 @@ public function __construct( ContactRepository $contactRepository, int $mainContactId, EntityManagerInterface $entityManager, - string $projectDir, string $mailFrom ) { $this->settingRepository = $settingRepository; $this->contactRepository = $contactRepository; $this->mainContactId = $mainContactId; $this->entityManager = $entityManager; - $this->projectDir = $projectDir; $this->mailFrom = $mailFrom; } @@ -99,51 +94,11 @@ public function getMailFrom() return $this->mailFrom; } - public function dataStore(): string - { - return $this->getData()['data_store'] ?? 'local'; - } - - public function dataDir(): string - { - return rtrim($this->getData()['data_dir'] ?? $this->projectDir . '/var/app_data', '/') . '/'; - } - - public function tempDir(): string - { - return rtrim($this->getData()['temp_dir'] ?? $this->projectDir . '/var/app_tmp/', '/') . '/'; - } - public function apiKey(): string { return $this->getData()['api_key'] ?? ''; } - public function awsKey(): string - { - return $this->getData()['aws_key'] ?? ''; - } - - public function awsSecret(): string - { - return $this->getData()['aws_secret'] ?? ''; - } - - public function awsRegion(): string - { - return $this->getData()['aws_region'] ?? ''; - } - - public function awsEndpoint(): string - { - return $this->getData()['aws_endpoint'] ?? ''; - } - - public function awsBucketName(): string - { - return $this->getData()['aws_bucket_name'] ?? ''; - } - public function flickrApiKey(): string { return $this->getData()['flickr_api_key'] ?? ''; diff --git a/templates/setting/index.html.twig b/templates/setting/index.html.twig index 36b3854..b1dafa0 100644 --- a/templates/setting/index.html.twig +++ b/templates/setting/index.html.twig @@ -25,50 +25,6 @@

-
- File storage -

- - -

-

- - - - -

-

- - - - -

-

- - - - - - - - -

-

- - - - - - - - - - - - -

-
-
Flickr