. * * @link https://www.librenms.org * * @copyright 2023 Steven Wilton * @author Steven Wilton */ namespace App\Http\Controllers\Maps; use App\Http\Controllers\Controller; use App\Models\CustomMap; use App\Models\CustomMapBackground; use Illuminate\Foundation\Http\FormRequest; use Illuminate\Support\Facades\Storage; class CustomMapBackgroundController extends Controller { public function get(CustomMap $map) { $this->authorize('view', $map); $background = $this->checkImageCache($map); if ($background) { $path = Storage::disk('base')->path('html/images/custommap/background/' . $background); return response()->file($path, [ 'Content-Type' => Storage::mimeType($background), ]); } abort(404); } public function save(FormRequest $request, CustomMap $map) { $this->authorize('update', $map); if ($request->bgimage) { $map->background_suffix = $request->bgimage->extension(); if (! $map->background) { $background = new CustomMapBackground; $background->background_image = $request->bgimage->getContent(); $map->background()->save($background); } else { $map->background->background_image = $request->bgimage->getContent(); $map->background->save(); } $map->background_version++; $map->save(); } elseif ($request->bgclear) { if ($map->background) { $map->background->delete(); } $map->background_suffix = null; $map->save(); } return response()->json([ 'bgimage' => $map->background_suffix ? true : false, 'bgversion' => $map->background_version, ]); } private function checkImageCache(CustomMap $map): ?string { if (! $map->background_suffix) { return null; } $imageName = $map->custom_map_id . '_' . $map->background_version . '.' . $map->background_suffix; if (Storage::disk('base')->missing('html/images/custommap/background/' . $imageName)) { Storage::disk('base')->put('html/images/custommap/background/' . $imageName, $map->background->background_image); } return $imageName; } }