改进上传服务

This commit is contained in:
WispX 2021-12-17 15:55:30 +08:00
parent d795171461
commit 7505616e3f
6 changed files with 18 additions and 13 deletions

View File

@ -14,6 +14,7 @@ use BenSampo\Enum\Enum;
* @method static static IsAllowGuestUpload()
* @method static static UserInitialCapacity()
* @method static static MailConfigs()
* @method static static GuestGroupConfigs()
*/
final class ConfigKey extends Enum
{

View File

@ -8,6 +8,7 @@ use BenSampo\Enum\Enum;
* @method static static MaximumFileSize()
* @method static static ConcurrentUploadNum()
* @method static static IsUploadNeedsReview()
* @method static static LimitPerMinute()
* @method static static LimitPerHour()
* @method static static LimitPerDay()
* @method static static LimitPerWeek()

View File

@ -2,11 +2,13 @@
namespace App\Models;
use App\Enums\StrategyKey;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
/**
@ -74,9 +76,13 @@ class Image extends Model
public function getUrlAttribute(): string
{
if (! $this->strategy) {
return asset($this->pathname);
return Storage::disk('uploads')->url($this->pathname);
}
$domain = Str::replaceFirst('/', '', $this->strategy->configs->get('domain'));
if (StrategyKey::Local()->is($this->strategy->key)) {
// 本地储存前缀必须是 uploads
$domain .= '/uploads';
}
return $domain.'/'.$this->pathname;
}

View File

@ -49,7 +49,7 @@ class UploadService
// 默认使用本地储存策略
$disk = collect([
'driver' => StrategyKey::Local,
'configs' => collect([LocalOption::Root => config('filesystems.disks.'.env('FILESYSTEM_DRIVER').'.root')]),
'configs' => collect([LocalOption::Root => config('filesystems.disks.uploads.root')]),
]);
if (! is_null($user)) {

View File

@ -42,17 +42,12 @@ return [
'visibility' => 'public',
],
's3' => [
'driver' => 's3',
'key' => env('AWS_ACCESS_KEY_ID'),
'secret' => env('AWS_SECRET_ACCESS_KEY'),
'region' => env('AWS_DEFAULT_REGION'),
'bucket' => env('AWS_BUCKET'),
'url' => env('AWS_URL'),
'endpoint' => env('AWS_ENDPOINT'),
'use_path_style_endpoint' => env('AWS_USE_PATH_STYLE_ENDPOINT', false),
'uploads' => [
'driver' => 'local',
'root' => storage_path('app/uploads'),
'url' => env('APP_URL').'/uploads',
'visibility' => 'public',
],
],
/*
@ -67,7 +62,8 @@ return [
*/
'links' => [
public_path('storage') => storage_path('app/public'),
// public_path('storage') => storage_path('app/public'),
public_path('uploads') => storage_path('app/uploads'),
],
];

1
public/uploads Symbolic link
View File

@ -0,0 +1 @@
/Users/wispx/Develop/Projects/lsky-pro/storage/app/uploads