Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use native disk methods instead of the pseudo scoped disks #844

Draft
wants to merge 3 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 0 additions & 54 deletions config/cms.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 27 additions & 1 deletion config/filesystems.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,40 @@
| 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.
|
*/

'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'),
Expand Down
18 changes: 18 additions & 0 deletions modules/system/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
27 changes: 17 additions & 10 deletions modules/system/classes/ImageResizer.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
<?php namespace System\Classes;
<?php

namespace System\Classes;

use Url;
use Crypt;
use Cache;
use Event;
use Config;
use Storage;
use Crypt;
use Event;
use Exception;
use SystemException;
use File as FileHelper;
use Illuminate\Filesystem\FilesystemAdapter;
use Storage;
use System\Models\File as SystemFileModel;
use SystemException;
use Url;
use Winter\Storm\Database\Attach\File as FileModel;
use Winter\Storm\Database\Attach\Resizer as DefaultResizer;

Expand Down Expand Up @@ -43,6 +45,11 @@
*/
class ImageResizer
{
/**
* The name of the storage disk used by this class.
*/
public const DISK = 'resized';

/**
* The cache key prefix for resizer configs
*/
Expand Down Expand Up @@ -154,12 +161,12 @@ public static function getAvailableSources(): array
'path' => 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'), '/'),
],
Expand All @@ -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(), '/'),
],
];

Expand Down
10 changes: 6 additions & 4 deletions modules/system/classes/MediaLibrary.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -773,17 +778,14 @@ 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
{
if ($this->storageDisk) {
return $this->storageDisk;
}

return $this->storageDisk = Storage::disk(
Config::get('cms.storage.media.disk', 'local')
);
return $this->storageDisk = Storage::disk(static::DISK);
}

/**
Expand Down
5 changes: 3 additions & 2 deletions modules/system/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
"league/flysystem-path-prefixing": "^3.0",
"laravel/framework": "^9.1"
},
"replace": {
"october/system": "1.1.*"
Expand Down
56 changes: 10 additions & 46 deletions modules/system/models/File.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
<?php namespace System\Models;
<?php

namespace System\Models;

use Url;
use Config;
use Storage;
use Winter\Storm\Database\Attach\File as FileBase;
use Backend\Controllers\Files;
use Winter\Storm\Database\Attach\File as FileBase;

/**
* File attachment model
Expand All @@ -14,6 +13,11 @@
*/
class File extends FileBase
{
/**
* The name of the storage disk used by this class.
*/
public const DISK = 'uploads';

/**
* @var string The database table used by the model.
*/
Expand Down Expand Up @@ -45,7 +49,7 @@ public function getThumb($width, $height, $options = [])
/**
* {@inheritDoc}
*/
public function getPath($fileName = null)
public function getPath(?string $fileName = null): string
{
$url = '';
if (!$this->isPublic() && class_exists(Files::class)) {
Expand All @@ -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'));
}
}