36
common/Files/Providers/BackblazeServiceProvider.php
Executable file
36
common/Files/Providers/BackblazeServiceProvider.php
Executable file
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace Common\Files\Providers;
|
||||
|
||||
use Illuminate\Filesystem\FilesystemManager;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
use Storage;
|
||||
|
||||
class BackblazeServiceProvider extends ServiceProvider
|
||||
{
|
||||
/**
|
||||
* Perform post-registration booting of services.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function boot()
|
||||
{
|
||||
Storage::extend('backblaze_s3', function ($app, $config) {
|
||||
$config[
|
||||
'endpoint'
|
||||
] = "https://s3.{$config['region']}.backblazeb2.com";
|
||||
|
||||
return app(FilesystemManager::class)->createS3Driver($config);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Register bindings in the container.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register()
|
||||
{
|
||||
//
|
||||
}
|
||||
}
|
||||
31
common/Files/Providers/DigitalOceanServiceProvider.php
Executable file
31
common/Files/Providers/DigitalOceanServiceProvider.php
Executable file
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace Common\Files\Providers;
|
||||
|
||||
use Illuminate\Filesystem\FilesystemManager;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
|
||||
class DigitalOceanServiceProvider extends ServiceProvider
|
||||
{
|
||||
public function boot()
|
||||
{
|
||||
Storage::extend('digitalocean_s3', function ($app, $config) {
|
||||
$config[
|
||||
'endpoint'
|
||||
] = "https://{$config['region']}.digitaloceanspaces.com";
|
||||
|
||||
return app(FilesystemManager::class)->createS3Driver($config);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Register bindings in the container.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register()
|
||||
{
|
||||
//
|
||||
}
|
||||
}
|
||||
81
common/Files/Providers/DropboxServiceProvider.php
Executable file
81
common/Files/Providers/DropboxServiceProvider.php
Executable file
@@ -0,0 +1,81 @@
|
||||
<?php
|
||||
|
||||
namespace Common\Files\Providers;
|
||||
|
||||
use Common\Settings\DotEnvEditor;
|
||||
use GuzzleHttp\Exception\ClientException;
|
||||
use Illuminate\Filesystem\FilesystemAdapter;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
use League\Flysystem\Filesystem;
|
||||
use Spatie\Dropbox\Client as DropboxClient;
|
||||
use Spatie\Dropbox\RefreshableTokenProvider;
|
||||
use Spatie\FlysystemDropbox\DropboxAdapter;
|
||||
use Storage;
|
||||
|
||||
class DropboxServiceProvider extends ServiceProvider
|
||||
{
|
||||
/**
|
||||
* Perform post-registration booting of services.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function boot()
|
||||
{
|
||||
Storage::extend('dropbox', function ($app, $config) {
|
||||
$config = array_merge($config, ['case_sensitive' => false]);
|
||||
|
||||
$tokenProvider = new class ($config) implements
|
||||
RefreshableTokenProvider
|
||||
{
|
||||
protected string|null $token;
|
||||
|
||||
public function __construct(protected array $config)
|
||||
{
|
||||
$this->token = $this->config['access_token'];
|
||||
}
|
||||
|
||||
public function refresh(ClientException $exception): bool
|
||||
{
|
||||
$response = Http::asForm()->post(
|
||||
"https://{$this->config['app_key']}:{$this->config['app_secret']}@api.dropbox.com/oauth2/token",
|
||||
[
|
||||
'grant_type' => 'refresh_token',
|
||||
'refresh_token' => $this->config['refresh_token'],
|
||||
],
|
||||
);
|
||||
$response->throw();
|
||||
|
||||
app(DotEnvEditor::class)->write([
|
||||
'STORAGE_DROPBOX_ACCESS_TOKEN' =>
|
||||
$response['access_token'],
|
||||
]);
|
||||
$this->token = $response['access_token'] ?? null;
|
||||
return $this->token ?: false;
|
||||
}
|
||||
|
||||
public function getToken(): string
|
||||
{
|
||||
return $this->token ?? '';
|
||||
}
|
||||
};
|
||||
|
||||
$adapter = new DropboxAdapter(new DropboxClient($tokenProvider));
|
||||
return new FilesystemAdapter(
|
||||
new Filesystem($adapter, $config),
|
||||
$adapter,
|
||||
$config,
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Register bindings in the container.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register()
|
||||
{
|
||||
//
|
||||
}
|
||||
}
|
||||
68
common/Files/Providers/DynamicStorageDiskProvider.php
Executable file
68
common/Files/Providers/DynamicStorageDiskProvider.php
Executable file
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
|
||||
namespace Common\Files\Providers;
|
||||
|
||||
use Illuminate\Contracts\Filesystem\Filesystem;
|
||||
use Illuminate\Foundation\Application;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Facades\Config;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
|
||||
class DynamicStorageDiskProvider extends ServiceProvider
|
||||
{
|
||||
public function boot()
|
||||
{
|
||||
Storage::extend('dynamic-uploads', function (
|
||||
Application $app,
|
||||
$initialConfig,
|
||||
) {
|
||||
return $this->resolveDisk('uploads', $initialConfig);
|
||||
});
|
||||
|
||||
Storage::extend('dynamic-public', function (
|
||||
Application $app,
|
||||
$initialConfig,
|
||||
) {
|
||||
return $this->resolveDisk('public', $initialConfig);
|
||||
});
|
||||
}
|
||||
|
||||
public function register()
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
private function resolveDisk(string $type, array $initialConfig): Filesystem
|
||||
{
|
||||
$driverName = config("common.site.{$type}_disk_driver") ?? 'local';
|
||||
$config = array_merge(
|
||||
$initialConfig,
|
||||
config("services.$driverName") ?? [],
|
||||
);
|
||||
$config['driver'] = $driverName;
|
||||
|
||||
// set root based on drive type and name
|
||||
$config['root'] =
|
||||
$driverName === 'local'
|
||||
? $config['local_root']
|
||||
: $config['remote_root'];
|
||||
|
||||
// unset "storage" url from remote drives as "$disk->url()" will generate "storage/file_entry.jpg" url
|
||||
if (
|
||||
$driverName !== 'local' &&
|
||||
Arr::get($config, 'url') === $config['remote_root']
|
||||
) {
|
||||
unset($config['url']);
|
||||
}
|
||||
|
||||
if (isset($config['port'])) {
|
||||
$config['port'] = (int) $config['port'];
|
||||
}
|
||||
|
||||
$dynamicConfigKey = "{$type}_{$driverName}";
|
||||
Config::set("filesystems.disks.{$dynamicConfigKey}", $config);
|
||||
|
||||
return Storage::disk($dynamicConfigKey);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user