Files
FlightsAPI/app/Http/Controllers/LogoController.php
T
2026-04-17 00:02:47 +10:00

154 lines
4.9 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Models\Airline;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
class LogoController extends Controller
{
const array CONDOR_LOGOS = ['BEACH', 'ISLAND', 'PASSION', 'SEA', 'SUNSHINE'];
public function getAirlineLogo(?Airline $airline)
{
$logoFile = $airline?->logo ?? 'blank.png';
$cacheLimit = 60 * 60 * 24;
if ($airline?->internal_name == 'condor') {
$logoKey = array_rand(self::CONDOR_LOGOS);
$logoFile = 'DE_' . self::CONDOR_LOGOS[$logoKey] . '.png';
$cacheLimit = 1;
}
$path = 'images/logos/tail/' . $logoFile;
if (!Storage::disk('local')->exists($path)) {
$path = 'images/logos/tail/blank.png';
}
$fullPath = Storage::disk('local')->path($path);
$lastModified = filemtime($fullPath);
return response()->file($fullPath, [
'Content-Type' => 'image/png',
'Cache-Control' => 'public, max-age='.$cacheLimit, // 24 hours
'Last-Modified' => gmdate('D, d M Y H:i:s', $lastModified) . ' GMT',
'ETag' => md5($path . $lastModified),
]);
}
public function getLogoById($id){
$airline = Airline::where('id', $id)
->first();
//Blank Airline Logo
if(!$airline) return $this->getLogoByCode('2H');
return $this->getAirlineLogo($airline);
}
public function getLogoByCode(string $code){
$column = strlen($code) == 2
? 'IATA_code'
: 'ICAO_code';
$airline = Airline::where($column, strtoupper($code))
->whereNotNull('logo')
->where('active', true)
->latest('id')
->first();
if (!$airline) {
$airline = Airline::where($column, strtoupper($code))
->whereNotNull('logo')
->latest('id')
->first();
}
return $this->getAirlineLogo($airline);
}
public static function deduplicateLogo(string $logo, array $correctInternalNames)
{
$log = fn(string $message) => print($message . PHP_EOL);
if (empty($correctInternalNames)) {
$nulled = Airline::where('logo', $logo)->get(['name', 'internal_name']);
Airline::where('logo', $logo)->update(['logo' => null]);
foreach ($nulled as $airline) {
$log(" Logo nulled: {$airline->name} ({$airline->internal_name})");
}
return;
}
$existing = Airline::whereIn('internal_name', $correctInternalNames)
->pluck('internal_name')
->toArray();
$missing = array_diff($correctInternalNames, $existing);
if (!empty($missing)) {
throw new \InvalidArgumentException(
'The following internal names do not exist in the database: ' . implode(', ', $missing)
);
}
$nulled = Airline::where('logo', $logo)
->whereNotIn('internal_name', $correctInternalNames)
->get(['name', 'internal_name']);
Airline::where('logo', $logo)
->whereNotIn('internal_name', $correctInternalNames)
->update(['logo' => null]);
foreach ($nulled as $airline) {
$log(" Logo nulled: {$airline->name} ({$airline->internal_name})");
}
}
public static function nullMissingLogoFiles()
{
$log = fn(string $message) => print($message . PHP_EOL);
$airlines = Airline::whereNotNull('logo')->get(['id', 'name', 'logo']);
foreach ($airlines as $airline) {
$path = storage_path('app/private/images/logos/tail/' . $airline->logo);
if (!file_exists($path)) {
$airline->update(['logo' => null]);
$log(" Logo nulled (file missing): {$airline->name}{$airline->logo}");
}
}
}
public static function renameLogoFiles()
{
$log = fn(string $message) => print($message . PHP_EOL);
$airlines = Airline::whereNotNull('logo')->get(['id', 'name', 'internal_name', 'logo']);
foreach ($airlines as $airline) {
$expectedName = $airline->internal_name . '.png';
if ($airline->logo === $expectedName) {
continue;
}
$oldPath = storage_path('app/private/images/logos/tail/' . $airline->logo);
$newPath = storage_path('app/private/images/logos/tail/' . $expectedName);
if (!file_exists($oldPath)) {
$log(" Skipping (file missing): {$airline->name}{$airline->logo}");
continue;
}
rename($oldPath, $newPath);
$airline->update(['logo' => $expectedName]);
$log(" Renamed: {$airline->logo}{$expectedName} ({$airline->name})");
}
}
}