feat(media): set media storage directory as configurable

This commit is contained in:
misuzu 2023-03-28 16:13:04 +00:00 committed by Yassine Doghri
parent 4503b05a8a
commit 7e1a470ba4
5 changed files with 54 additions and 18 deletions

View File

@ -162,9 +162,23 @@ email.SMTPPass="your_smtp_password"
| **`SMTPPort`** | number | `25` | | **`SMTPPort`** | number | `25` |
| **`SMTPCrypto`** | [`"tls"` or `"ssl"`] | `"tls"` | | **`SMTPCrypto`** | [`"tls"` or `"ssl"`] | `"tls"` |
### S3 ### Media storage
By default, files are stored in the `public/media` folder using the filesystem. By default, files are saved to the `public/media` folder using the file system.
If you need to relocate the `media` folder to a different location, you can
specify it in your `.env` file as shown below:
```ini
# […]
media.root="media"
media.storage="/mnt/storage"
```
In this example, the files will be saved to the /mnt/storage/media folder. Make
sure to also update your web server configuration to reflect this change.
### S3
If you prefer storing your media files on an S3 compatible storage, you may If you prefer storing your media files on an S3 compatible storage, you may
specify it in your `.env`: specify it in your `.env`:

View File

@ -61,7 +61,7 @@ class SettingsController extends BaseController
helper(['filesystem', 'media']); helper(['filesystem', 'media']);
// delete site folder in media before repopulating it // delete site folder in media before repopulating it
delete_files(media_path('/site')); delete_files(media_path_absolute('/site'));
// save original in disk // save original in disk
$originalFilename = (new FS(config('Media')))->save( $originalFilename = (new FS(config('Media')))->save(
@ -71,9 +71,9 @@ class SettingsController extends BaseController
// convert jpeg image to png if not // convert jpeg image to png if not
if ($siteIconFile->getClientMimeType() !== 'image/png') { if ($siteIconFile->getClientMimeType() !== 'image/png') {
service('image')->withFile(media_path($originalFilename)) service('image')->withFile(media_path_absolute($originalFilename))
->convert(IMAGETYPE_JPEG) ->convert(IMAGETYPE_JPEG)
->save(media_path('/site/icon.png')); ->save(media_path_absolute('/site/icon.png'));
} }
// generate random hash to use as a suffix to renew browser cache // generate random hash to use as a suffix to renew browser cache
@ -81,15 +81,15 @@ class SettingsController extends BaseController
// generate ico // generate ico
$ico_lib = new PHP_ICO(); $ico_lib = new PHP_ICO();
$ico_lib->add_image(media_path('/site/icon.png'), [[32, 32], [64, 64]]); $ico_lib->add_image(media_path_absolute('/site/icon.png'), [[32, 32], [64, 64]]);
$ico_lib->save_ico(media_path("/site/favicon.{$randomHash}.ico")); $ico_lib->save_ico(media_path_absolute("/site/favicon.{$randomHash}.ico"));
// resize original to needed sizes // resize original to needed sizes
foreach ([64, 180, 192, 512] as $size) { foreach ([64, 180, 192, 512] as $size) {
service('image') service('image')
->withFile(media_path('/site/icon.png')) ->withFile(media_path_absolute('/site/icon.png'))
->resize($size, $size) ->resize($size, $size)
->save(media_path("/site/icon-{$size}.{$randomHash}.png")); ->save(media_path_absolute("/site/icon-{$size}.{$randomHash}.png"));
} }
service('settings') service('settings')
@ -109,7 +109,7 @@ class SettingsController extends BaseController
{ {
helper(['filesystem', 'media']); helper(['filesystem', 'media']);
// delete site folder in media // delete site folder in media
delete_files(media_path('/site')); delete_files(media_path_absolute('/site'));
service('settings') service('settings')
->forget('App.siteIcon'); ->forget('App.siteIcon');

View File

@ -55,6 +55,14 @@ class Media extends BaseConfig
*/ */
public string $root = 'media'; public string $root = 'media';
/**
* --------------------------------------------------------------------------
* Media storage folder
* --------------------------------------------------------------------------
* Defines the folder used to store the media root folder
*/
public string $storage = ROOTPATH . 'public';
/** /**
* @var array<string, string> * @var array<string, string>
*/ */

View File

@ -21,11 +21,13 @@ class FS implements FileManagerInterface
*/ */
public function save(File $file, string $path): string | false public function save(File $file, string $path): string | false
{ {
helper('media');
if ((pathinfo($path, PATHINFO_EXTENSION) === '') && (($extension = $file->getExtension()) !== '')) { if ((pathinfo($path, PATHINFO_EXTENSION) === '') && (($extension = $file->getExtension()) !== '')) {
$path = $path . '.' . $extension; $path = $path . '.' . $extension;
} }
$mediaRoot = $this->config->root; $mediaRoot = media_path_absolute();
if (! file_exists(dirname($mediaRoot . '/' . $path))) { if (! file_exists(dirname($mediaRoot . '/' . $path))) {
mkdir(dirname($mediaRoot . '/' . $path), 0777, true); mkdir(dirname($mediaRoot . '/' . $path), 0777, true);
@ -49,7 +51,7 @@ class FS implements FileManagerInterface
{ {
helper('media'); helper('media');
return unlink(media_path($key)); return unlink(media_path_absolute($key));
} }
public function getUrl(string $key): string public function getUrl(string $key): string
@ -68,21 +70,21 @@ class FS implements FileManagerInterface
{ {
helper('media'); helper('media');
return rename(media_path($oldKey), media_path($newKey)); return rename(media_path_absolute($oldKey), media_path_absolute($newKey));
} }
public function getFileContents(string $key): string public function getFileContents(string $key): string
{ {
helper('media'); helper('media');
return (string) file_get_contents(media_path($key)); return (string) file_get_contents(media_path_absolute($key));
} }
public function getFileInput(string $key): string public function getFileInput(string $key): string
{ {
helper('media'); helper('media');
return media_path($key); return media_path_absolute($key);
} }
public function deletePodcastImageSizes(string $podcastHandle): bool public function deletePodcastImageSizes(string $podcastHandle): bool
@ -91,7 +93,7 @@ class FS implements FileManagerInterface
$allPodcastImagesPaths = []; $allPodcastImagesPaths = [];
foreach (['jpg', 'jpeg', 'png', 'webp'] as $ext) { foreach (['jpg', 'jpeg', 'png', 'webp'] as $ext) {
$images = glob(media_path("/podcasts/{$podcastHandle}/*_*{$ext}")); $images = glob(media_path_absolute("/podcasts/{$podcastHandle}/*_*{$ext}"));
if (! $images) { if (! $images) {
return false; return false;
@ -115,7 +117,7 @@ class FS implements FileManagerInterface
$allPersonsImagesPaths = []; $allPersonsImagesPaths = [];
foreach (['jpg', 'jpeg', 'png', 'webp'] as $ext) { foreach (['jpg', 'jpeg', 'png', 'webp'] as $ext) {
$images = glob(media_path("/persons/*_*{$ext}")); $images = glob(media_path_absolute("/persons/*_*{$ext}"));
if (! $images) { if (! $images) {
return false; return false;
@ -137,6 +139,6 @@ class FS implements FileManagerInterface
{ {
helper('media'); helper('media');
return is_really_writable(ROOTPATH . 'public/' . media_path()); return is_really_writable(media_path_absolute());
} }
} }

View File

@ -82,3 +82,15 @@ if (! function_exists('media_path')) {
return config('Media')->root . '/' . $uri; return config('Media')->root . '/' . $uri;
} }
} }
if (! function_exists('media_path_absolute')) {
/**
* Prefixes the absolute storage directory to the media path of a given uri
*
* @param string|string[] $uri URI string or array of URI segments
*/
function media_path_absolute(string | array $uri = ''): string
{
return config('Media')->storage . '/' . media_path($uri);
}
}