From eaf40caadb838ff72184f872923c5e60091503dd Mon Sep 17 00:00:00 2001 From: Luke Towers Date: Tue, 21 Feb 2023 15:34:31 -0600 Subject: [PATCH 1/3] Initial work on using native disk methods instead of pseudo "scoped disks" defined in the cms config --- config/cms.php | 54 ------------------------ config/filesystems.php | 28 ++++++++++++- modules/system/ServiceProvider.php | 18 ++++++++ modules/system/classes/DiskManager.php | 0 modules/system/classes/ImageResizer.php | 27 +++++++----- modules/system/classes/MediaLibrary.php | 10 +++-- modules/system/composer.json | 5 ++- modules/system/models/File.php | 56 +++++-------------------- 8 files changed, 81 insertions(+), 117 deletions(-) create mode 100644 modules/system/classes/DiskManager.php diff --git a/config/cms.php b/config/cms.php index 1553b1dc3a..92da812726 100644 --- a/config/cms.php +++ b/config/cms.php @@ -280,60 +280,6 @@ 'themesPath' => '/themes', - /* - |-------------------------------------------------------------------------- - | Resource storage - |-------------------------------------------------------------------------- - | - | Specifies the configuration for resource storage, such as media and - | upload files. These resources are used: - | - | media - generated by the media manager. - | uploads - generated by attachment model relationships. - | resized - generated by System\Classes\ImageResizer or the resize() Twig filter - | - | For each resource you can specify: - | - | disk - filesystem disk, as specified in filesystems.php config. - | folder - a folder prefix for storing all generated files inside. - | path - the public path relative to the application base URL, - | or you can specify a full URL path. - | - | Optionally, you can specify how long temporary URLs to protected files - | in cloud storage (ex. AWS, RackSpace) are valid for by setting - | temporaryUrlTTL to a value in seconds to define a validity period. This - | is only used for the 'uploads' config when using a supported cloud disk - | - | NOTE: If you have installed Winter in a subfolder, are using local - | storage and are not using a linkPolicy of 'force' you should include - | the path to the subfolder in the `path` option for these storage - | configurations. - | - | Example: Winter is installed under https://localhost/projects/winter. - | You should then specify `/projects/winter/storage/app/uploads` as the - | path for the uploads disk and `/projects/winter/storage/app/media` as - | the path for the media disk. - */ - - 'storage' => [ - 'uploads' => [ - 'disk' => 'local', - 'folder' => 'uploads', - 'path' => '/storage/app/uploads', - 'temporaryUrlTTL' => 3600, - ], - 'media' => [ - 'disk' => 'local', - 'folder' => 'media', - 'path' => '/storage/app/media', - ], - 'resized' => [ - 'disk' => 'local', - 'folder' => 'resized', - 'path' => '/storage/app/resized', - ], - ], - /* |-------------------------------------------------------------------------- | Convert Line Endings diff --git a/config/filesystems.php b/config/filesystems.php index 64cd1768b6..b7578b5a0d 100644 --- a/config/filesystems.php +++ b/config/filesystems.php @@ -24,7 +24,7 @@ | may even configure multiple disks of the same driver. Defaults have | been setup for each driver as an example of the required options. | - | Supported Drivers: "local", "ftp", "sftp", "s3" + | Supported Drivers: "local", "ftp", "sftp", "s3", "scoped" | | NOTE: s3's stream_uploads option requires the Winter.DriverAWS plugin | to be installed and enabled. @@ -32,6 +32,32 @@ */ 'disks' => [ + + // The "uploads" disk is used by the System module to store uploaded files (System\Models\File). + 'uploads' => [ + 'driver' => 'scoped', + 'disk' => 'local', + 'prefix' => 'uploads', + 'visibility' => 'public', + 'temporaryUrlTTL' => 3600, + ], + + // The "media" disk is used by the System module's MediaLibrary to store media files. + 'media' => [ + 'driver' => 'scoped', + 'disk' => 'local', + 'prefix' => 'media', + 'visibility' => 'public', + ], + + // The "resized" disk is used by the System module's ImageResizer to store resized images. + 'resized' => [ + 'driver' => 'scoped', + 'disk' => 'local', + 'prefix' => 'uploads', + 'visibility' => 'public', + ], + 'local' => [ 'driver' => 'local', 'root' => storage_path('app'), diff --git a/modules/system/ServiceProvider.php b/modules/system/ServiceProvider.php index 2dc967b27e..4daf80a9e8 100644 --- a/modules/system/ServiceProvider.php +++ b/modules/system/ServiceProvider.php @@ -99,6 +99,24 @@ public function boot() } } + // Polyfill the system disks if they are not defined + $systemDisks = ['uploads', 'media', 'resized']; + foreach ($systemDisks as $disk) { + $oldConfig = Config::get("cms.storage.$disk"); + if (!Config::get("filesystems.disks.$disk") && !empty($oldConfig)) { + $newConfig = [ + 'driver' => 'scoped', + 'disk' => $oldConfig['disk'], + 'prefix' => $oldConfig['folder'], + 'url' => $oldConfig['path'], + ]; + if (!empty($oldConfig['tempoaryUrlTTL'])) { + $newConfig['temporaryUrlTTL'] = $oldConfig['tempoaryUrlTTL']; + } + Config::set("filesystems.disks.$disk", $newConfig); + } + } + /* * Set a default samesite config value for invalid values */ diff --git a/modules/system/classes/DiskManager.php b/modules/system/classes/DiskManager.php new file mode 100644 index 0000000000..e69de29bb2 diff --git a/modules/system/classes/ImageResizer.php b/modules/system/classes/ImageResizer.php index 692f81e2fc..0878a40617 100644 --- a/modules/system/classes/ImageResizer.php +++ b/modules/system/classes/ImageResizer.php @@ -1,16 +1,18 @@ - rtrim(config('cms.pluginsPath', '/plugins'), '/'), ], 'resized' => [ - 'disk' => config('cms.storage.resized.disk', 'local'), + 'disk' => static::DISK, 'folder' => config('cms.storage.resized.folder', 'resized'), 'path' => rtrim(config('cms.storage.resized.path', '/storage/app/resized'), '/'), ], 'media' => [ - 'disk' => config('cms.storage.media.disk', 'local'), + 'disk' => MediaLibrary::DISK, 'folder' => config('cms.storage.media.folder', 'media'), 'path' => rtrim(config('cms.storage.media.path', '/storage/app/media'), '/'), ], @@ -169,9 +176,9 @@ public static function getAvailableSources(): array 'path' => '/modules', ], 'filemodel' => [ - 'disk' => config('cms.storage.uploads.disk', 'local'), + 'disk' => SystemFileModel::DISK, 'folder' => config('cms.storage.uploads.folder', 'uploads'), - 'path' => rtrim(config('cms.storage.uploads.path', '/storage/app/uploads'), '/'), + 'path' => rtrim((new SystemFileModel)->getPublicPath(), '/'), ], ]; diff --git a/modules/system/classes/MediaLibrary.php b/modules/system/classes/MediaLibrary.php index 9bf4eaadb5..db7c2ef553 100644 --- a/modules/system/classes/MediaLibrary.php +++ b/modules/system/classes/MediaLibrary.php @@ -23,6 +23,11 @@ class MediaLibrary { use \Winter\Storm\Support\Traits\Singleton; + /** + * The name of the storage disk used by this class. + */ + public const DISK = 'media'; + const SORT_BY_TITLE = 'title'; const SORT_BY_SIZE = 'size'; const SORT_BY_MODIFIED = 'modified'; @@ -773,7 +778,6 @@ protected function filterItemList(&$itemList, $filter) * This method should always be used instead of trying to access the * $storageDisk property directly as initializing the disc requires * communicating with the remote storage. - * @return mixed Returns the storage disk object. */ public function getStorageDisk(): FilesystemAdapter { @@ -781,9 +785,7 @@ public function getStorageDisk(): FilesystemAdapter return $this->storageDisk; } - return $this->storageDisk = Storage::disk( - Config::get('cms.storage.media.disk', 'local') - ); + return $this->storageDisk = Storage::disk(static::DISK); } /** diff --git a/modules/system/composer.json b/modules/system/composer.json index e8387cfb4f..5ff94183e9 100644 --- a/modules/system/composer.json +++ b/modules/system/composer.json @@ -25,8 +25,9 @@ "require": { "php": "^8.0.2", "composer/installers": "~1.11.0", - "laravel/framework": "^9.1", - "composer/semver": "^3.2" + "composer/semver": "^3.2", + "flysystem-path-prefixing": "^3.0", + "laravel/framework": "^9.1" }, "replace": { "october/system": "1.1.*" diff --git a/modules/system/models/File.php b/modules/system/models/File.php index f7b8d35f6e..e3abb35290 100644 --- a/modules/system/models/File.php +++ b/modules/system/models/File.php @@ -1,10 +1,9 @@ -isPublic() && class_exists(Files::class)) { @@ -56,44 +60,4 @@ public function getPath($fileName = null) return $url; } - - /** - * Define the public address for the storage path. - */ - public function getPublicPath() - { - $uploadsPath = Config::get('cms.storage.uploads.path', '/storage/app/uploads'); - - if ($this->isPublic()) { - $uploadsPath .= '/public'; - } - else { - $uploadsPath .= '/protected'; - } - - return Url::asset($uploadsPath) . '/'; - } - - /** - * Define the internal storage path. - */ - public function getStorageDirectory() - { - $uploadsFolder = Config::get('cms.storage.uploads.folder'); - - if ($this->isPublic()) { - return $uploadsFolder . '/public/'; - } - - return $uploadsFolder . '/protected/'; - } - - /** - * Returns the storage disk the file is stored on - * @return FilesystemAdapter - */ - public function getDisk() - { - return Storage::disk(Config::get('cms.storage.uploads.disk')); - } } From 604e052897e0a682810e9663842143d30911bd3b Mon Sep 17 00:00:00 2001 From: Luke Towers Date: Tue, 21 Feb 2023 15:37:13 -0600 Subject: [PATCH 2/3] Delete DiskManager.php --- modules/system/classes/DiskManager.php | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 modules/system/classes/DiskManager.php diff --git a/modules/system/classes/DiskManager.php b/modules/system/classes/DiskManager.php deleted file mode 100644 index e69de29bb2..0000000000 From d318f3add22c3a50af7b57fb2c979b6aed0dc8f6 Mon Sep 17 00:00:00 2001 From: Luke Towers Date: Tue, 21 Feb 2023 21:17:01 -0600 Subject: [PATCH 3/3] fix composer require --- modules/system/composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/system/composer.json b/modules/system/composer.json index 5ff94183e9..ea1db642ac 100644 --- a/modules/system/composer.json +++ b/modules/system/composer.json @@ -26,7 +26,7 @@ "php": "^8.0.2", "composer/installers": "~1.11.0", "composer/semver": "^3.2", - "flysystem-path-prefixing": "^3.0", + "league/flysystem-path-prefixing": "^3.0", "laravel/framework": "^9.1" }, "replace": {