mirror of
https://github.com/henrywhitaker3/Speedtest-Tracker.git
synced 2026-01-04 03:54:51 +01:00
Commented the code
This commit is contained in:
@@ -9,7 +9,14 @@ use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
|
||||
class BackupHelper {
|
||||
public static function backup($format = 'json')
|
||||
|
||||
/**
|
||||
* Generates a backup of all speedtests.
|
||||
*
|
||||
* @param string $format json|csv
|
||||
* @return string $name Returns the filename of the backup.
|
||||
*/
|
||||
public static function backup(String $format = 'json')
|
||||
{
|
||||
$timestamp = new DateTime();
|
||||
$timestamp = $timestamp->format('Y-m-d_H:i:s');
|
||||
@@ -43,6 +50,13 @@ class BackupHelper {
|
||||
return $name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Restore data from a backup in CSV or JSON format
|
||||
*
|
||||
* @param array|string $array Backup data
|
||||
* @param string $format json|csv
|
||||
* @return boolean
|
||||
*/
|
||||
public static function restore($array, $format)
|
||||
{
|
||||
if($format == 'json') {
|
||||
|
||||
@@ -5,6 +5,13 @@ namespace App\Helpers;
|
||||
use App\Setting;
|
||||
|
||||
class SettingsHelper {
|
||||
|
||||
/**
|
||||
* Get a Setting object by name
|
||||
*
|
||||
* @param String $name The name field in the setting table
|
||||
* @return \App\Setting|boolean $name The Setting object. Returns false if no mathcing obj.
|
||||
*/
|
||||
public static function get(String $name)
|
||||
{
|
||||
$name = Setting::where('name', $name)->get();
|
||||
@@ -19,6 +26,13 @@ class SettingsHelper {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create / update value for Setting object.
|
||||
*
|
||||
* @param String $name Name of setting
|
||||
* @param String $value Value of setting
|
||||
* @return \App\Setting
|
||||
*/
|
||||
public static function set(String $name, String $value)
|
||||
{
|
||||
$setting = SettingsHelper::get($name);
|
||||
@@ -36,6 +50,11 @@ class SettingsHelper {
|
||||
return $setting;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the app's base path
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function getBase()
|
||||
{
|
||||
$base = env('BASE_PATH', '/');
|
||||
|
||||
@@ -13,6 +13,13 @@ use JsonException;
|
||||
use SimpleXMLElement;
|
||||
|
||||
class SpeedtestHelper {
|
||||
|
||||
/**
|
||||
* Runs/processes speedtest output to created a Speedtest object
|
||||
*
|
||||
* @param boolean|string $output If false, new speedtest runs. If anything else, will try to parse as JSON for speedtest results.
|
||||
* @return \App\Speedtest|boolean
|
||||
*/
|
||||
public static function runSpeedtest($output = false)
|
||||
{
|
||||
if($output === false) {
|
||||
@@ -36,6 +43,11 @@ class SpeedtestHelper {
|
||||
return (isset($test)) ? $test : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the output of executing speedtest binary.
|
||||
*
|
||||
* @return boolean|string
|
||||
*/
|
||||
public static function output()
|
||||
{
|
||||
$server = SettingsHelper::get('server')['value'];
|
||||
@@ -54,10 +66,21 @@ class SpeedtestHelper {
|
||||
return shell_exec($binPath . ' -f json');
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts bytes/s to Mbps
|
||||
*
|
||||
* @param int|float $bytes
|
||||
* @return int|float
|
||||
*/
|
||||
public static function convert($bytes) {
|
||||
return ( $bytes * 8 ) / 1000000;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the latest speedtest object.
|
||||
*
|
||||
* @return boolean|\App\Speedtest
|
||||
*/
|
||||
public static function latest()
|
||||
{
|
||||
$data = Speedtest::latest()->get();
|
||||
@@ -69,6 +92,12 @@ class SpeedtestHelper {
|
||||
return $data->first();
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses network speeds and return converted to Mbps
|
||||
*
|
||||
* @param array $input
|
||||
* @return array
|
||||
*/
|
||||
public static function parseUnits($input)
|
||||
{
|
||||
$input = explode(' ', $input);
|
||||
|
||||
@@ -10,10 +10,39 @@ use RecursiveIteratorIterator;
|
||||
use ZipArchive;
|
||||
|
||||
class UpdateHelper {
|
||||
/**
|
||||
* URL of updates
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $url;
|
||||
|
||||
/**
|
||||
* Current app version number
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $currentVersion;
|
||||
|
||||
/**
|
||||
* Username of GitHub repo
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $user;
|
||||
|
||||
/**
|
||||
* Name of GitHub repo
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $repo;
|
||||
|
||||
/**
|
||||
* Branch of GitHub repo
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $branch;
|
||||
|
||||
function __construct() {
|
||||
@@ -25,6 +54,11 @@ class UpdateHelper {
|
||||
$this->download = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns data on new version available
|
||||
*
|
||||
* @return boolean|array false|[ version, changelog ]
|
||||
*/
|
||||
public function check()
|
||||
{
|
||||
Log::info('Checking for new version');
|
||||
@@ -50,6 +84,11 @@ class UpdateHelper {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the latest version number from GitHub
|
||||
*
|
||||
* @return array [ repo, branch, version ]
|
||||
*/
|
||||
public function checkLatestVersion()
|
||||
{
|
||||
$url = 'https://raw.githubusercontent.com/'
|
||||
@@ -78,6 +117,11 @@ class UpdateHelper {
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the latest changelog from GitHub
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getChangelog()
|
||||
{
|
||||
$url = 'https://raw.githubusercontent.com/'
|
||||
@@ -97,6 +141,11 @@ class UpdateHelper {
|
||||
return $changelog;
|
||||
}
|
||||
|
||||
/**
|
||||
* Downloads the latest version from GitHub
|
||||
*
|
||||
* @return boolean|Exception
|
||||
*/
|
||||
public function downloadLatest()
|
||||
{
|
||||
Log::info('Downloading the latest version from GitHub');
|
||||
@@ -121,6 +170,11 @@ class UpdateHelper {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts zip archive from update
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function extractFiles()
|
||||
{
|
||||
Log::info('Extracting the update');
|
||||
@@ -137,6 +191,11 @@ class UpdateHelper {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Replace existing files with newly downloaded files
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function updateFiles()
|
||||
{
|
||||
Log::info('Applying update');
|
||||
@@ -151,6 +210,14 @@ class UpdateHelper {
|
||||
Log::info('Successfully applied update');
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes default templates from updated files.
|
||||
* This is for things like .env so that user specified files are not
|
||||
* overwritten.
|
||||
*
|
||||
* @param string $path
|
||||
* @return void
|
||||
*/
|
||||
private function deleteExcluded($path)
|
||||
{
|
||||
Log::info('Deleting excluded items from update directory');
|
||||
@@ -172,6 +239,11 @@ class UpdateHelper {
|
||||
Log::info('Excluded items deleted from update directory');
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a ZIP backup of current installation
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function backupCurrent()
|
||||
{
|
||||
Log::info('Backing up current installation');
|
||||
@@ -209,6 +281,11 @@ class UpdateHelper {
|
||||
Log::info('Backup created at: ' . $backupZip);
|
||||
}
|
||||
|
||||
/**
|
||||
* Move updated files into server dir.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function moveFiles()
|
||||
{
|
||||
$new = array_filter(glob('/tmp/'.$this->repo.'-update/*'), 'is_dir');
|
||||
@@ -242,6 +319,11 @@ class UpdateHelper {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Make a copy of excluded, user customised files
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function tempStoreExcludedFiles()
|
||||
{
|
||||
Log::info('Temporarily moving exluded files from root directory');
|
||||
@@ -263,6 +345,11 @@ class UpdateHelper {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Restore user cusotmised files from the copy
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function restoreExcludedFiles()
|
||||
{
|
||||
Log::info('Restoring exluded files to root directory');
|
||||
@@ -284,6 +371,11 @@ class UpdateHelper {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete update files from download dir.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function clearup()
|
||||
{
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user