Fixes Local Image Issue

Local images should now appear. Needed to specify the public disk to ensure that HTTPS was used.
This commit is contained in:
Refringe 2024-06-23 08:34:22 -04:00
parent a2fb68ea93
commit d34a75523a
Signed by: Refringe
SSH Key Fingerprint: SHA256:t865XsQpfTeqPRBMN2G6+N8wlDjkgUCZF3WGW6O9N/k
4 changed files with 58 additions and 10 deletions

View File

@ -9,6 +9,7 @@ use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str; use Illuminate\Support\Str;
use Laravel\Scout\Searchable; use Laravel\Scout\Searchable;
@ -31,17 +32,10 @@ class Mod extends Model
protected static function booted(): void protected static function booted(): void
{ {
// Apply the global scope to exclude disabled mods.
static::addGlobalScope(new DisabledScope); static::addGlobalScope(new DisabledScope);
} }
protected function slug(): Attribute
{
return Attribute::make(
get: fn (string $value) => strtolower($value),
set: fn (string $value) => Str::slug($value),
);
}
public function user(): BelongsTo public function user(): BelongsTo
{ {
return $this->belongsTo(User::class); return $this->belongsTo(User::class);
@ -57,6 +51,9 @@ class Mod extends Model
return $this->hasMany(ModVersion::class); return $this->hasMany(ModVersion::class);
} }
/**
* Scope a query to include the total number of downloads for a mod.
*/
public function scopeWithTotalDownloads($query) public function scopeWithTotalDownloads($query)
{ {
return $query->addSelect(['total_downloads' => ModVersion::selectRaw('SUM(downloads) AS total_downloads') return $query->addSelect(['total_downloads' => ModVersion::selectRaw('SUM(downloads) AS total_downloads')
@ -110,6 +107,9 @@ class Mod extends Model
->with(['lastUpdatedVersion', 'lastUpdatedVersion.sptVersion']); ->with(['lastUpdatedVersion', 'lastUpdatedVersion.sptVersion']);
} }
/**
* Get the indexable data array for the model.
*/
public function toSearchableArray(): array public function toSearchableArray(): array
{ {
return [ return [
@ -124,8 +124,45 @@ class Mod extends Model
]; ];
} }
/**
* Determine if the model should be searchable.
*/
public function shouldBeSearchable(): bool public function shouldBeSearchable(): bool
{ {
return ! $this->disabled; return ! $this->disabled;
} }
/**
* Get the URL to the thumbnail.
*/
public function thumbnailUrl(): Attribute
{
return Attribute::get(function (): string {
return $this->thumbnail
? Storage::disk($this->thumbnailDisk())->url($this->thumbnail)
: '';
});
}
/**
* Get the disk where the thumbnail is stored.
*/
protected function thumbnailDisk(): string
{
return match (config('app.env')) {
'production' => 'r2', // Cloudflare R2 Storage
default => 'public', // Local
};
}
/**
* Ensure the slug is always lower case when retrieved and slugified when saved.
*/
protected function slug(): Attribute
{
return Attribute::make(
get: fn (string $value) => Str::lower($value),
set: fn (string $value) => Str::slug($value),
);
}
} }

View File

@ -84,4 +84,15 @@ class User extends Authenticatable implements MustVerifyEmail
'password' => 'hashed', 'password' => 'hashed',
]; ];
} }
/**
* Get the disk that profile photos should be stored on.
*/
protected function profilePhotoDisk(): string
{
return match (config('app.env')) {
'production' => 'r2', // Cloudflare R2 Storage
default => 'public', // Local
};
}
} }

View File

@ -10,7 +10,7 @@
<img src="https://placehold.co/450x450/EEE/31343C?font=source-sans-pro&text={{ $mod->name }}" alt="{{ $mod->name }}" class="block dark:hidden h-48 w-full object-cover md:h-full md:w-48 transform group-hover:scale-110 transition-all duration-200"> <img src="https://placehold.co/450x450/EEE/31343C?font=source-sans-pro&text={{ $mod->name }}" alt="{{ $mod->name }}" class="block dark:hidden h-48 w-full object-cover md:h-full md:w-48 transform group-hover:scale-110 transition-all duration-200">
<img src="https://placehold.co/450x450/31343C/EEE?font=source-sans-pro&text={{ $mod->name }}" alt="{{ $mod->name }}" class="hidden dark:block h-48 w-full object-cover md:h-full md:w-48 transform group-hover:scale-110 transition-all duration-200"> <img src="https://placehold.co/450x450/31343C/EEE?font=source-sans-pro&text={{ $mod->name }}" alt="{{ $mod->name }}" class="hidden dark:block h-48 w-full object-cover md:h-full md:w-48 transform group-hover:scale-110 transition-all duration-200">
@else @else
<img src="{{ asset($mod->thumbnail) }}" alt="{{ $mod->name }}" class="h-48 w-full object-cover md:h-full md:w-48 transform group-hover:scale-110 transition-all duration-200"> <img src="{{ $mod->thumbnailUrl }}" alt="{{ $mod->name }}" class="h-48 w-full object-cover md:h-full md:w-48 transform group-hover:scale-110 transition-all duration-200">
@endif @endif
</div> </div>
<div class="flex flex-col w-full justify-between p-5"> <div class="flex flex-col w-full justify-between p-5">

View File

@ -15,7 +15,7 @@
<img src="https://placehold.co/144x144/EEE/31343C?font=source-sans-pro&text={{ $mod->name }}" alt="{{ $mod->name }}" class="block dark:hidden w-36 rounded-lg"> <img src="https://placehold.co/144x144/EEE/31343C?font=source-sans-pro&text={{ $mod->name }}" alt="{{ $mod->name }}" class="block dark:hidden w-36 rounded-lg">
<img src="https://placehold.co/144x144/31343C/EEE?font=source-sans-pro&text={{ $mod->name }}" alt="{{ $mod->name }}" class="hidden dark:block w-36 rounded-lg"> <img src="https://placehold.co/144x144/31343C/EEE?font=source-sans-pro&text={{ $mod->name }}" alt="{{ $mod->name }}" class="hidden dark:block w-36 rounded-lg">
@else @else
<img src="{{ Storage::url($mod->thumbnail) }}" alt="{{ $mod->name }}" class="w-36 rounded-lg"> <img src="{{ $mod->thumbnailUrl }}" alt="{{ $mod->name }}" class="w-36 rounded-lg">
@endif @endif
</div> </div>
<div class="grow flex flex-col justify-center items-center sm:items-start text-gray-800 dark:text-gray-200"> <div class="grow flex flex-col justify-center items-center sm:items-start text-gray-800 dark:text-gray-200">