Added ability to upload own favicon

Must be either .ico, .jpg or .png format and under 40KB
Upload is done in /settings
Also removed save YABS to text input in settings form
This commit is contained in:
cp6 2023-03-31 11:31:19 +11:00
parent f619f34081
commit 8a6748f1b0
6 changed files with 74 additions and 29 deletions

View File

@ -5,7 +5,7 @@ namespace App\Http\Controllers;
use App\Models\Settings;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
class SettingsController extends Controller
{
@ -32,11 +32,25 @@ class SettingsController extends Controller
'recently_added_amount' => 'required|integer|between:0,12',
'currency' => 'required|string|size:3',
'sort_on' => 'required|integer|between:1,10',
'favicon' => 'sometimes|nullable|mimes:ico,jpg,png|max:40',
]);
$update = DB::table('settings')
->where('id', 1)
->update([
$settings = Settings::where('id', 1)->first();
if ($request->favicon) {//Has a favicon upload
$file = $request->favicon;
$extension = $file->getClientOriginalExtension();
$favicon_filename = "favicon.$extension";
if ($favicon_filename !== $settings->favicon && $settings->favicon !== 'favicon.ico') {
Storage::disk('public_uploads')->delete($settings->favicon);//Delete old favicon
}
$file->storeAs("", $favicon_filename, "public_uploads");//Save into /public
}
$do_update = $settings->update([
'dark_mode' => $request->dark_mode,
'show_versions_footer' => $request->show_versions_footer,
'show_servers_public' => $request->show_servers_public,
@ -53,6 +67,7 @@ class SettingsController extends Controller
'recently_added_amount' => $request->recently_added_amount,
'dashboard_currency' => $request->currency,
'sort_on' => $request->sort_on,
'favicon' => $favicon_filename ?? $settings->favicon
]);
Cache::forget('due_soon');//Main page due_soon cache
@ -71,7 +86,7 @@ class SettingsController extends Controller
Settings::setSettingsToSession(Settings::getSettings());
if ($update){
if ($do_update) {
return redirect()->route('settings.index')
->with('success', 'Settings Updated Successfully.');
}

View File

@ -14,7 +14,7 @@ class Settings extends Model
protected $table = 'settings';
protected $fillable = ['id', 'show_versions_footer', 'show_servers_public'];
protected $fillable = ['id', 'show_versions_footer', 'show_servers_public', 'show_server_value_ip', 'show_server_value_hostname', 'show_server_value_provider', 'show_server_value_location', 'show_server_value_price', 'show_server_value_yabs', 'save_yabs_as_txt', 'default_currency', 'default_server_os', 'due_soon_amount', 'recently_added_amount', 'dark_mode', 'dashboard_currency', 'sort_on', 'favicon'];
public static function getSettings()
{
@ -42,6 +42,7 @@ class Settings extends Model
Session::put('recently_added_amount', $settings->recently_added_amount ?? 6);
Session::put('dashboard_currency', $settings->dashboard_currency ?? 'USD');
Session::put('sort_on', $settings->sort_on ?? 1);
Session::put('favicon', $settings->favicon ?? 'favicon.ico');
Session::save();
}

View File

@ -42,6 +42,11 @@ return [
'visibility' => 'public',
],
'public_uploads' => [
'driver' => 'local',
'root' => public_path(),
],
's3' => [
'driver' => 's3',
'key' => env('AWS_ACCESS_KEY_ID'),

View File

@ -0,0 +1,22 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
Schema::table('settings', function (Blueprint $table) {
$table->string('favicon')->after('sort_on')->default('favicon.ico');
});
}
public function down(): void
{
Schema::table('settings', function (Blueprint $table) {
$table->dropColumn('favicon');
});
}
};

View File

@ -9,6 +9,7 @@
@endif
<title>@yield('title') - @if (config()->has('app.name')) {{ config('app.name') }} @else My idlers @endif</title>
<link rel="icon" type="image" href="{{asset(Session::get('favicon') ?? 'favicon.ico')}}"/>
@if(Session::get('dark_mode'))
<link rel="stylesheet" href="{{ asset('css/dark.css') }}">

View File

@ -10,7 +10,7 @@
<x-slot name="href">{{ route('/') }}</x-slot>
Back to home
</x-back-button>
<form action="{{ route('settings.update', 1) }}" method="POST">
<form action="{{ route('settings.update', 1) }}" method="POST" enctype="multipart/form-data">
@csrf
@method('PUT')
<div class="row mt-2">
@ -70,11 +70,6 @@
</x-currency-select>
</div>
</div>
<div class="row mt-3">
<div class="col-12 col-md-6 mb-3">
<x-yes-no-select title="Save YABS input to txt" name="save_yabs_as_txt" value="{{ $setting->save_yabs_as_txt }}"></x-yes-no-select>
</div>
</div>
<p>Only if <i>Show servers to public</i> is <b>YES</b> do these apply:</p>
<div class="row mt-3">
<div class="col-12 col-md-6 mb-3">
@ -150,6 +145,12 @@
</div>
</div>
</div>
<div class="row mt-3">
<div class="col-12 col-md-6 mb-3">
<label class="form-label pe-2" for="chooseFile">Add custom favicon must be either .ico|.png|.jpg MAX 40KB</label>
<input type="file" name="favicon" class="form-control" id="favicon">
</div>
</div>
<div class="row">
<div class="col-12 col-lg-4">
<x-submit-button>Update settings</x-submit-button>