Add download forcing

This commit is contained in:
Ben Goldsworthy 2022-11-29 23:58:11 +00:00
parent 428645245b
commit 172131e1e6
1 changed files with 26 additions and 7 deletions

View File

@ -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 13 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