putFileAs($storagePath, new File($path), $name, 'public'); return $disk->url($file); }, random_int(100, 500)); } public static function handleResilientStore($storagePath, $path, $name) { $attempts = 0; return retry(4, function() use($storagePath, $path, $name, $attempts) { self::$attempts++; usleep(100000); $baseDisk = self::$attempts > 1 ? self::getAltDriver() : config('filesystems.cloud'); try { $disk = Storage::disk($baseDisk); $file = $disk->putFileAs($storagePath, new File($path), $name, 'public'); } catch (S3Exception | ClientException | ConnectException | UnableToWriteFile | Exception $e) {} return $disk->url($file); }, function (int $attempt, Exception $exception) { return $attempt * 200; }); } public static function getAltDriver() { $drivers = []; if(config('filesystems.disks.alt-primary.enabled')) { $drivers[] = 'alt-primary'; } if(config('filesystems.disks.alt-secondary.enabled')) { $drivers[] = 'alt-secondary'; } if(empty($drivers)) { return false; } $key = array_rand($drivers, 1); return $drivers[$key]; } }