🔧 改进配置方式

This commit is contained in:
Wisp X 2022-01-21 23:31:38 +08:00
parent 9dadc84f21
commit c79123b178
13 changed files with 123 additions and 107 deletions

View File

@ -34,6 +34,6 @@ final class ConfigKey
/** @var string 邮件配置 */
const MailConfigs = 'mail_configs';
/** @var string 访客组配置,同时也为组默认配置,开启游客上传后生效 */
const GuestGroupConfigs = 'guest_group_configs';
/** @var string 角色组默认配置 */
const GroupConfigs = 'group_configs';
}

View File

@ -2,6 +2,7 @@
namespace App\Http\Controllers\Admin;
use App\Enums\ConfigKey;
use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\GroupRequest;
use App\Models\Group;
@ -15,6 +16,7 @@ class GroupController extends Controller
public function __construct()
{
\Illuminate\Support\Facades\View::share([
'default' => config('convention.app.'.ConfigKey::GroupConfigs),
'extensions' => ['jpeg', 'jpg', 'png', 'gif', 'tif', 'bmp', 'ico', 'psd', 'webp'],
'positions' => [
'top-left' => '左上角',

View File

@ -17,7 +17,7 @@ class UserController extends Controller
$user = Auth::user();
// 组配置
$configs = Utils::config(ConfigKey::GuestGroupConfigs);
$configs = Utils::config(ConfigKey::GroupConfigs);
if ($user->group) {
$configs = $user->group->configs;
}

View File

@ -38,19 +38,19 @@ class Group extends Model
];
/**
* 默认组配置,同时也属于访客组默认配置
* 获取访客组默认配置
*
* @return Collection
*/
public static function getDefaultConfigs(): Collection
public static function getGuestConfigs(): Collection
{
return Utils::config(ConfigKey::GuestGroupConfigs);
return Utils::config(ConfigKey::GroupConfigs);
}
protected static function booted()
{
static::creating(function (self $group) {
$group->configs = self::getDefaultConfigs()->merge($group->configs ?: []);
$group->configs = config('convention.app.'.ConfigKey::GroupConfigs)->merge($group->configs ?: []);
});
}

View File

@ -175,7 +175,7 @@ class Image extends Model
{
return $this->belongsTo(Group::class, 'group_id', 'id')->withDefault(function (Group $group) {
$group->name = '系统默认组';
$group->configs = $group::getDefaultConfigs();
$group->configs = $group::getGuestConfigs();
});
}

View File

@ -28,7 +28,7 @@ class AppServiceProvider extends ServiceProvider
{
// 初始化视图中的默认数据
View::composer('*', function (\Illuminate\View\View $view) {
$configs = Auth::check() && Auth::user()->group ? Auth::user()->group->configs : Group::getDefaultConfigs();
$configs = Auth::check() && Auth::user()->group ? Auth::user()->group->configs : Group::getGuestConfigs();
$view->with('groupConfigs', $configs);
});
}

View File

@ -54,7 +54,7 @@ class ImageService
$image = new Image();
// 组配置
$configs = Group::getDefaultConfigs();
$configs = Utils::config(ConfigKey::GroupConfigs);
// 默认使用本地储存策略
$disk = collect([
'driver' => StrategyKey::Local,

View File

@ -30,7 +30,7 @@ class Utils
$value = (bool) $value;
break;
case ConfigKey::MailConfigs:
case ConfigKey::GuestGroupConfigs:
case ConfigKey::GroupConfigs:
$value = collect(json_decode($value, true));
break;
case ConfigKey::UserInitialCapacity:

90
config/convention.php Normal file
View File

@ -0,0 +1,90 @@
<?php
// 惯例配置
use App\Enums\ConfigKey;
use App\Enums\GroupConfigKey;
use App\Enums\Mail\SmtpOption;
use App\Enums\Scan\AliyunOption;
use App\Enums\Watermark\FontOption;
use App\Enums\Watermark\ImageOption;
return [
'app' => [
ConfigKey::SiteName => 'Lsky Pro',
ConfigKey::SiteKeywords => 'Lsky Pro,lsky,兰空图床',
ConfigKey::SiteDescription => 'Lsky Pro, Your photo album on the cloud.',
ConfigKey::IcpNo => '',
ConfigKey::IsEnableRegistration => 1,
ConfigKey::IsEnableGallery => 1,
ConfigKey::IsAllowGuestUpload => 1,
ConfigKey::UserInitialCapacity => 512000,
ConfigKey::IsUserNeedVerify => 1,
ConfigKey::MailConfigs => [
'default' => 'smtp',
'mailers' => [
SmtpOption::Host => '',
SmtpOption::Port => 25,
SmtpOption::Encryption => 'tls',
SmtpOption::Username => '',
SmtpOption::Password => '',
SmtpOption::Timeout => null,
SmtpOption::AuthMode => null,
],
],
ConfigKey::GroupConfigs => [
GroupConfigKey::MaximumFileSize => 5120,
GroupConfigKey::ConcurrentUploadNum => 3,
GroupConfigKey::IsEnableScan => 0,
GroupConfigKey::IsEnableWatermark => 0,
GroupConfigKey::IsEnableOriginalProtection => 0,
GroupConfigKey::ScannedAction => 'mark', // in mark or delete
GroupConfigKey::ScanConfigs => [
'driver' => 'aliyun',
'drivers' => [
'aluyun' => [
AliyunOption::AccessKeyId => '',
AliyunOption::AccessKeySecret => '',
AliyunOption::RegionId => '',
AliyunOption::Scenes => ['porn'],
AliyunOption::BizType => '',
],
],
],
GroupConfigKey::WatermarkConfigs => [
'driver' => 'font',
'drivers' => [
'font' => [
FontOption::Text => 'Lsky Pro',
FontOption::Position => 'bottom-right',
FontOption::Angle => 0,
FontOption::Size => 50,
FontOption::Font => '',
FontOption::Color => '#000000',
FontOption::X => 10,
FontOption::Y => 10,
],
'image' => [
ImageOption::Image => '',
ImageOption::Position => 'bottom-right',
ImageOption::Opacity => 100,
ImageOption::Rotate => 0,
ImageOption::Width => 0,
ImageOption::Height => 0,
ImageOption::X => 10,
ImageOption::Y => 10,
]
],
],
GroupConfigKey::LimitPerMinute => 20,
GroupConfigKey::LimitPerHour => 100,
GroupConfigKey::LimitPerDay => 300,
GroupConfigKey::LimitPerWeek => 600,
GroupConfigKey::LimitPerMonth => 999,
GroupConfigKey::AcceptedFileSuffixes => ['jpeg', 'jpg', 'png', 'gif', 'tif', 'bmp', 'ico', 'psd', 'webp'],
GroupConfigKey::PathNamingRule => '{Y}/{m}/{d}',
GroupConfigKey::FileNamingRule => '{uniqid}',
GroupConfigKey::ImageCacheTtl => 2626560,
],
],
];

View File

@ -23,84 +23,13 @@ class DatabaseSeeder extends Seeder
public function run()
{
$date = Carbon::now()->format('Y-m-d H:i:s');
$array = collect([
ConfigKey::SiteName => 'Lsky Pro',
ConfigKey::SiteKeywords => 'Lsky Pro,lsky,兰空图床',
ConfigKey::SiteDescription => 'Lsky Pro, Your photo album on the cloud.',
ConfigKey::IcpNo => '',
ConfigKey::IsEnableRegistration => true,
ConfigKey::IsEnableGallery => true,
ConfigKey::IsAllowGuestUpload => true,
ConfigKey::UserInitialCapacity => 512000,
ConfigKey::IsUserNeedVerify => true,
ConfigKey::MailConfigs => json_encode([
'default' => 'smtp',
'mailers' => [
SmtpOption::Host => '',
SmtpOption::Port => 25,
SmtpOption::Encryption => 'tls',
SmtpOption::Username => '',
SmtpOption::Password => '',
SmtpOption::Timeout => null,
SmtpOption::AuthMode => null,
],
]),
ConfigKey::GuestGroupConfigs => collect([
GroupConfigKey::MaximumFileSize => 5120,
GroupConfigKey::ConcurrentUploadNum => 3,
GroupConfigKey::IsEnableScan => false,
GroupConfigKey::IsEnableWatermark => false,
GroupConfigKey::IsEnableOriginalProtection => false,
GroupConfigKey::ScannedAction => 'mark', // in mark or delete
GroupConfigKey::ScanConfigs => [
'driver' => 'aliyun',
'drivers' => [
'aluyun' => [
AliyunOption::AccessKeyId => '',
AliyunOption::AccessKeySecret => '',
AliyunOption::RegionId => '',
AliyunOption::Scenes => ['porn'],
AliyunOption::BizType => '',
],
],
],
GroupConfigKey::WatermarkConfigs => [
'driver' => 'font',
'drivers' => [
'font' => [
FontOption::Text => 'Lsky Pro',
FontOption::Position => 'bottom-right',
FontOption::Angle => 0,
FontOption::Size => 50,
FontOption::Font => '',
FontOption::Color => '#000000',
FontOption::X => 10,
FontOption::Y => 10,
],
'image' => [
ImageOption::Image => '',
ImageOption::Position => 'bottom-right',
ImageOption::Opacity => 100,
ImageOption::Rotate => 0,
ImageOption::Width => 0,
ImageOption::Height => 0,
ImageOption::X => 10,
ImageOption::Y => 10,
]
],
],
GroupConfigKey::LimitPerMinute => 20,
GroupConfigKey::LimitPerHour => 100,
GroupConfigKey::LimitPerDay => 300,
GroupConfigKey::LimitPerWeek => 600,
GroupConfigKey::LimitPerMonth => 999,
GroupConfigKey::AcceptedFileSuffixes => ['jpeg', 'jpg', 'png', 'gif', 'tif', 'bmp', 'ico', 'psd', 'webp'],
GroupConfigKey::PathNamingRule => '{Y}/{m}/{d}',
GroupConfigKey::FileNamingRule => '{uniqid}',
GroupConfigKey::ImageCacheTtl => 2626560,
])->toJson(),
])->transform(function ($value, $key) use ($date) {
return ['name' => $key, 'value' => $value, 'updated_at' => $date, 'created_at' => $date];
$array = collect(config('convention.app'))->transform(function ($value, $key) use ($date) {
return [
'name' => $key,
'value' => is_array($value) ? json_encode($value, JSON_UNESCAPED_UNICODE) : $value,
'updated_at' => $date,
'created_at' => $date,
];
})->values()->toArray();
DB::table('configs')->insert($array);
}

View File

@ -35,47 +35,47 @@
<div class="col-span-6">
<label for="maximum_file_size" class="block text-sm font-medium text-gray-700"><span class="text-red-600">*</span>最大文件大小(KB)</label>
<input type="number" name="configs[maximum_file_size]" id="maximum_file_size" autocomplete="maximum_file_size" class="mt-1 focus:ring-indigo-500 focus:border-indigo-500 block w-full shadow-sm sm:text-sm border-gray-300 rounded-md" placeholder="请输入上传文件的最大限制单位kb">
<input type="number" name="configs[maximum_file_size]" id="maximum_file_size" autocomplete="maximum_file_size" class="mt-1 focus:ring-indigo-500 focus:border-indigo-500 block w-full shadow-sm sm:text-sm border-gray-300 rounded-md" placeholder="请输入上传文件的最大限制单位kb" value="5120">
</div>
<div class="col-span-6 sm:col-span-3">
<label for="concurrent_upload_num" class="block text-sm font-medium text-gray-700"><span class="text-red-600">*</span>并发上传限制</label>
<input type="number" name="configs[concurrent_upload_num]" id="concurrent_upload_num" autocomplete="concurrent_upload_num" class="mt-1 focus:ring-indigo-500 focus:border-indigo-500 block w-full shadow-sm sm:text-sm border-gray-300 rounded-md" placeholder="请输入并发上传数量">
<input type="number" name="configs[concurrent_upload_num]" id="concurrent_upload_num" autocomplete="concurrent_upload_num" class="mt-1 focus:ring-indigo-500 focus:border-indigo-500 block w-full shadow-sm sm:text-sm border-gray-300 rounded-md" placeholder="请输入并发上传数量" value="3">
</div>
<div class="col-span-6 sm:col-span-3">
<label for="limit_per_minute" class="block text-sm font-medium text-gray-700"><span class="text-red-600">*</span>每分钟上传限制</label>
<input type="number" name="configs[limit_per_minute]" id="limit_per_minute" autocomplete="limit_per_minute" class="mt-1 focus:ring-indigo-500 focus:border-indigo-500 block w-full shadow-sm sm:text-sm border-gray-300 rounded-md" placeholder="请输入每分钟可以上传的图片数量">
<input type="number" name="configs[limit_per_minute]" id="limit_per_minute" autocomplete="limit_per_minute" class="mt-1 focus:ring-indigo-500 focus:border-indigo-500 block w-full shadow-sm sm:text-sm border-gray-300 rounded-md" placeholder="请输入每分钟可以上传的图片数量" value="0">
</div>
<div class="col-span-6 sm:col-span-3">
<label for="limit_per_hour" class="block text-sm font-medium text-gray-700"><span class="text-red-600">*</span>每小时上传限制</label>
<input type="number" name="configs[limit_per_hour]" id="limit_per_hour" autocomplete="limit_per_hour" class="mt-1 focus:ring-indigo-500 focus:border-indigo-500 block w-full shadow-sm sm:text-sm border-gray-300 rounded-md" placeholder="请输入每小时可以上传的图片数量">
<input type="number" name="configs[limit_per_hour]" id="limit_per_hour" autocomplete="limit_per_hour" class="mt-1 focus:ring-indigo-500 focus:border-indigo-500 block w-full shadow-sm sm:text-sm border-gray-300 rounded-md" placeholder="请输入每小时可以上传的图片数量" value="0">
</div>
<div class="col-span-6 sm:col-span-3">
<label for="limit_per_day" class="block text-sm font-medium text-gray-700"><span class="text-red-600">*</span>每天上传限制</label>
<input type="number" name="configs[limit_per_day]" id="limit_per_day" autocomplete="limit_per_day" class="mt-1 focus:ring-indigo-500 focus:border-indigo-500 block w-full shadow-sm sm:text-sm border-gray-300 rounded-md" placeholder="请输入每天可以上传的图片数量">
<input type="number" name="configs[limit_per_day]" id="limit_per_day" autocomplete="limit_per_day" class="mt-1 focus:ring-indigo-500 focus:border-indigo-500 block w-full shadow-sm sm:text-sm border-gray-300 rounded-md" placeholder="请输入每天可以上传的图片数量" value="0">
</div>
<div class="col-span-6 sm:col-span-3">
<label for="limit_per_week" class="block text-sm font-medium text-gray-700"><span class="text-red-600">*</span>每周上传限制</label>
<input type="number" name="configs[limit_per_week]" id="limit_per_week" autocomplete="limit_per_week" class="mt-1 focus:ring-indigo-500 focus:border-indigo-500 block w-full shadow-sm sm:text-sm border-gray-300 rounded-md" placeholder="请输入每周可以上传的图片数量">
<input type="number" name="configs[limit_per_week]" id="limit_per_week" autocomplete="limit_per_week" class="mt-1 focus:ring-indigo-500 focus:border-indigo-500 block w-full shadow-sm sm:text-sm border-gray-300 rounded-md" placeholder="请输入每周可以上传的图片数量" value="0">
</div>
<div class="col-span-6 sm:col-span-3">
<label for="limit_per_month" class="block text-sm font-medium text-gray-700"><span class="text-red-600">*</span>每月上传限制</label>
<input type="number" name="configs[limit_per_month]" id="limit_per_month" autocomplete="limit_per_month" class="mt-1 focus:ring-indigo-500 focus:border-indigo-500 block w-full shadow-sm sm:text-sm border-gray-300 rounded-md" placeholder="请输入每月可以上传的图片数量">
<input type="number" name="configs[limit_per_month]" id="limit_per_month" autocomplete="limit_per_month" class="mt-1 focus:ring-indigo-500 focus:border-indigo-500 block w-full shadow-sm sm:text-sm border-gray-300 rounded-md" placeholder="请输入每月可以上传的图片数量" value="0">
</div>
<div class="col-span-6 sm:col-span-3">
<label for="path_naming_rule" class="block text-sm font-medium text-gray-700"><span class="text-red-600">*</span>路径命名规则</label>
<input type="number" name="configs[path_naming_rule]" id="path_naming_rule" autocomplete="path_naming_rule" class="mt-1 focus:ring-indigo-500 focus:border-indigo-500 block w-full shadow-sm sm:text-sm border-gray-300 rounded-md" placeholder="请输入路径命名规则">
<input type="text" name="configs[path_naming_rule]" id="path_naming_rule" autocomplete="path_naming_rule" class="mt-1 focus:ring-indigo-500 focus:border-indigo-500 block w-full shadow-sm sm:text-sm border-gray-300 rounded-md" placeholder="请输入路径命名规则" value="{Y}/{m}/{d}">
</div>
<div class="col-span-6 sm:col-span-3">
<label for="file_naming_rule" class="block text-sm font-medium text-gray-700"><span class="text-red-600">*</span>文件命名规则</label>
<input type="number" name="configs[file_naming_rule]" id="file_naming_rule" autocomplete="file_naming_rule" class="mt-1 focus:ring-indigo-500 focus:border-indigo-500 block w-full shadow-sm sm:text-sm border-gray-300 rounded-md" placeholder="请输入文件命名规则">
<input type="text" name="configs[file_naming_rule]" id="file_naming_rule" autocomplete="file_naming_rule" class="mt-1 focus:ring-indigo-500 focus:border-indigo-500 block w-full shadow-sm sm:text-sm border-gray-300 rounded-md" placeholder="请输入文件命名规则" value="{uniqid}">
</div>
<div class="col-span-6">
@ -148,7 +148,7 @@
<div class="col-span-6 mb-4">
<label for="configs[image_cache_ttl]" class="block text-sm font-medium text-gray-700">图片缓存时间()</label>
<input type="number" name="configs[image_cache_ttl]" id="configs[image_cache_ttl]" autocomplete="image_cache_ttl" class="mt-1 focus:ring-indigo-500 focus:border-indigo-500 block w-full shadow-sm sm:text-sm border-gray-300 rounded-md" placeholder="请输入受保护图片的缓存时间不填或填0表示不缓存">
<input type="number" name="configs[image_cache_ttl]" id="configs[image_cache_ttl]" autocomplete="image_cache_ttl" class="mt-1 focus:ring-indigo-500 focus:border-indigo-500 block w-full shadow-sm sm:text-sm border-gray-300 rounded-md" placeholder="请输入受保护图片的缓存时间不填或填0表示不缓存" value="2626560">
</div>
</div>

View File

@ -2,10 +2,11 @@
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Route;
use App\Models\Group;
use App\Enums\GroupConfigKey;
use App\Utils;
use App\Enums\ConfigKey;
$extensions = Group::getDefaultConfigs()->get(GroupConfigKey::AcceptedFileSuffixes);
$extensions = Utils::config(ConfigKey::GroupConfigs)->get(GroupConfigKey::AcceptedFileSuffixes);
Route::middleware('cache.headers:public;max_age=2628000;etag')->group(function () use ($extensions) {
Route::any('{key}.{extension}', [
Controller::class, 'output',

View File

@ -20,12 +20,6 @@ use App\Http\Controllers\User\ProfileController;
use App\Http\Controllers\Admin\GroupController as AdminGroupController;
Route::get('/test', function () {
$group = \App\Models\Group::first();
$group->configs = \App\Utils::config(\App\Enums\ConfigKey::GuestGroupConfigs);
$group->save();
});
Route::get('/', fn () => view('welcome'))->name('/');
Route::post('upload', [Controller::class, 'upload']);
Route::group(['middleware' => ['auth']], function () {