From 172131e1e63c9b2f79bcd132cf5772f044f17f8a Mon Sep 17 00:00:00 2001 From: Rumperuu Date: Tue, 29 Nov 2022 23:58:11 +0000 Subject: [PATCH] Add download forcing --- app/Http/Controllers/TrackerController.php | 33 +++++++++++++++++----- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/app/Http/Controllers/TrackerController.php b/app/Http/Controllers/TrackerController.php index 20bffb6..c28e552 100644 --- a/app/Http/Controllers/TrackerController.php +++ b/app/Http/Controllers/TrackerController.php @@ -36,15 +36,28 @@ class TrackerController extends Controller $viewMode = $request->input('show', null); $fromCheckin = $request->input('from', null); $toCheckin = $request->input('to', null); - - $tripData = $this->get_trip_data($tripId); + $forceDownload = $request->input('force', false); + + $tripData = $this->get_trip_data($tripId, $forceDownload); if ($fromCheckin) { - $tripData->checkins = array_filter($tripData->checkins, function($key) use ($fromCheckin) { return $key >= $fromCheckin; }, ARRAY_FILTER_USE_KEY); + $tripData->checkins = array_filter( + $tripData->checkins, + function($key) use ($fromCheckin) { + return $key >= $fromCheckin; + }, + ARRAY_FILTER_USE_KEY + ); } if ($toCheckin) { - $tripData->checkins = array_filter($tripData->checkins, function($key) use ($toCheckin) { return $key <= $toCheckin; }, ARRAY_FILTER_USE_KEY); + $tripData->checkins = array_filter( + $tripData->checkins, + function($key) use ($toCheckin) { + return $key <= $toCheckin; + }, + ARRAY_FILTER_USE_KEY + ); } return view( @@ -75,15 +88,16 @@ class TrackerController extends Controller * 3. If new trip data is downloaded, overwrite any cached data. * * @param string $tripId + * @param bool $forceDownload * @return string */ - public function get_trip_data(string $tripId) { + public function get_trip_data(string $tripId, bool $forceDownload = false) { $tripFileName = ( $tripId ?? config('app.current_trip_id') ) . '.json'; // Returns the cached trip data if the trip is inactive (i.e., finished) // or less than 1–3 hours old, depending on whether it was tracking // at last check or not. - if (Storage::disk('local')->exists($tripFileName)) { + if (!$forceDownload && Storage::disk('local')->exists($tripFileName)) { Log::debug("Cached trip file '{$tripFileName}' found..."); $cachedData = json_decode(Storage::disk('local')->get($tripFileName))->trip; @@ -106,7 +120,12 @@ class TrackerController extends Controller } // Otherwise, download the trip data from the Wayward API. - Log::debug("No cached trip file found for '{$tripFileName}'."); + if ($forceDownload) { + Log::debug("Forcing download for '{$tripFileName}'."); + } else { + Log::debug("No cached trip file found for '{$tripFileName}'."); + } + $client = new Client([ 'base_uri' => 'https://app.wayward.travel/', 'timeout' => 3.0