2026-11-03 12:26:17 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace Uwil\Storage;
|
|
|
|
|
|
|
|
|
|
use Illuminate\Contracts\Foundation\Application;
|
2026-11-03 16:46:43 +01:00
|
|
|
use Illuminate\Support\Arr;
|
2026-11-03 12:26:17 +01:00
|
|
|
use Illuminate\Support\Facades\Config;
|
|
|
|
|
use Illuminate\Support\ServiceProvider;
|
|
|
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
|
use Uwil\Storage\Config\StorageServiceConfig;
|
|
|
|
|
use Uwil\Storage\Contracts\FileSanitizer;
|
|
|
|
|
use Uwil\Storage\Contracts\MimeTypeChecker;
|
|
|
|
|
use Uwil\Storage\Contracts\StorageGateway;
|
|
|
|
|
use Uwil\Storage\Contracts\StorageService;
|
|
|
|
|
use Uwil\Storage\Lib\FileSanitizerImpl;
|
|
|
|
|
use Uwil\Storage\Lib\MimeTypeCheckerImpl;
|
|
|
|
|
use Uwil\Storage\Lib\PathGenerators\DateHashPathGenerator;
|
|
|
|
|
use Uwil\Storage\Lib\PathGenerators\UuidPathGenerator;
|
|
|
|
|
use Uwil\Storage\Lib\StorageGatewayImpl;
|
|
|
|
|
use Uwil\Storage\Lib\StorageServiceImpl;
|
|
|
|
|
|
|
|
|
|
class StorageServiceProvider extends ServiceProvider
|
|
|
|
|
{
|
|
|
|
|
public function boot(): void
|
|
|
|
|
{
|
|
|
|
|
$this->publishes([
|
|
|
|
|
__DIR__ . '/../config/storage-service.php' => config_path('storage-service.php'),
|
|
|
|
|
], 'config');
|
|
|
|
|
|
|
|
|
|
// * Set filter condition
|
|
|
|
|
$this->app->singleton(
|
|
|
|
|
StorageServiceConfig::class,
|
|
|
|
|
function (Application $_) {
|
2026-11-03 16:51:06 +01:00
|
|
|
/** @var array<string, array<string, mixed>> $rawDisks */
|
|
|
|
|
$rawDisks = array_filter(
|
|
|
|
|
Config::array('filesystems.disks', []),
|
|
|
|
|
fn ($diskConf) => is_array($diskConf)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
/** @var array<int, string> $eligibleDisks */
|
|
|
|
|
$eligibleDisks = array_filter(
|
|
|
|
|
Config::array('storage-service.eligibleDisks', []),
|
|
|
|
|
fn ($disk) => is_string($disk)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
/** @var array<int, string> $allowedExtensions */
|
|
|
|
|
$allowedExtensions = array_filter(
|
|
|
|
|
Config::array('storage-service.allowedExtensions', []),
|
|
|
|
|
fn ($ext) => is_string($ext)
|
|
|
|
|
);
|
|
|
|
|
|
2026-11-04 08:11:57 +01:00
|
|
|
$rawPrefix = Config::get('storage-service.prefix');
|
|
|
|
|
$prefix = is_string($rawPrefix) ? $rawPrefix : null;
|
|
|
|
|
|
2026-11-03 12:26:17 +01:00
|
|
|
$disks = [];
|
|
|
|
|
foreach ($rawDisks as $name => $diskConf) {
|
|
|
|
|
$disks[] = (object) [
|
|
|
|
|
'disk' => (string) $name,
|
2026-11-03 16:46:43 +01:00
|
|
|
'driver' => Arr::string($diskConf, 'driver'),
|
2026-11-03 12:26:17 +01:00
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new StorageServiceConfig(
|
|
|
|
|
Config::string('storage-service.default', 'local'),
|
2026-11-03 16:51:06 +01:00
|
|
|
$eligibleDisks,
|
2026-11-03 12:26:17 +01:00
|
|
|
$disks,
|
2026-11-04 08:11:57 +01:00
|
|
|
$allowedExtensions,
|
|
|
|
|
$prefix
|
2026-11-03 12:26:17 +01:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$this->app->bind(MimeTypeChecker::class, MimeTypeCheckerImpl::class);
|
|
|
|
|
$this->app->bind(FileSanitizer::class, FileSanitizerImpl::class);
|
|
|
|
|
|
2026-11-04 08:11:57 +01:00
|
|
|
$this->app->bind(
|
|
|
|
|
DateHashPathGenerator::class,
|
|
|
|
|
fn (Application $app) => new DateHashPathGenerator(
|
|
|
|
|
$app->make(StorageServiceConfig::class)
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$this->app->bind(
|
|
|
|
|
UuidPathGenerator::class,
|
|
|
|
|
fn (Application $app) => new UuidPathGenerator(
|
|
|
|
|
$app->make(StorageServiceConfig::class)
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
|
2026-11-03 12:26:17 +01:00
|
|
|
$this->app->bind(
|
|
|
|
|
StorageGateway::class,
|
|
|
|
|
fn (Application $app) => new StorageGatewayImpl(
|
|
|
|
|
$app->make(LoggerInterface::class),
|
|
|
|
|
$app->make(DateHashPathGenerator::class),
|
|
|
|
|
$app->make(UuidPathGenerator::class),
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$this->app->bind(
|
|
|
|
|
StorageService::class,
|
|
|
|
|
fn (Application $app) => new StorageServiceImpl(
|
|
|
|
|
$app->make(StorageServiceConfig::class),
|
|
|
|
|
$app->make(FileSanitizer::class),
|
|
|
|
|
$app->make(StorageGateway::class)
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function register(): void
|
|
|
|
|
{
|
|
|
|
|
$this->mergeConfigFrom(
|
|
|
|
|
sprintf('%s/../config/storage-service.php', __DIR__),
|
|
|
|
|
'storage-service'
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|