Skip to content

Commit

Permalink
scope the file and folder identifiers to the local machine IP address
Browse files Browse the repository at this point in the history
  • Loading branch information
arukompas committed Nov 11, 2024
1 parent c20cc0a commit 101eed9
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/LogFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function __construct(string $path, ?string $type = null)
{
$this->path = $path;
$this->name = basename($path);
$this->identifier = Utils::shortMd5($path).'-'.$this->name;
$this->identifier = Utils::shortMd5(Utils::getLocalIP().':'.$path).'-'.$this->name;
$this->type = $type;

// Let's remove the file name because we already know it.
Expand Down Expand Up @@ -94,7 +94,7 @@ public function sizeFormatted(): string

public function subFolderIdentifier(): string
{
return Utils::shortMd5($this->subFolder);
return Utils::shortMd5(Utils::getLocalIP().':'.$this->subFolder);
}

public function downloadUrl(): string
Expand Down
2 changes: 1 addition & 1 deletion src/LogFolder.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function __construct(
public string $path,
mixed $files,
) {
$this->identifier = Utils::shortMd5($path);
$this->identifier = Utils::shortMd5(Utils::getLocalIP().':'.$path);
$this->files = new LogFileCollection($files);
}

Expand Down
35 changes: 35 additions & 0 deletions src/Utils/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

class Utils
{
private static string $_cachedLocalIP;

/**
* Get a human-friendly readable string of the number of bytes provided.
*/
Expand Down Expand Up @@ -85,4 +87,37 @@ public static function glob_recursive($pattern, $flags = 0): array

return $files;
}

public static function getLocalIP(bool $cached = true): string
{
if (isset(self::$_cachedLocalIP) && $cached) {
return self::$_cachedLocalIP;
}

if (isset($_SERVER['SERVER_ADDR'])) {
self::$_cachedLocalIP = $_SERVER['SERVER_ADDR'];
} else {
$os = php_uname('s');

if (stripos($os, 'Linux') !== false) {
$localIP = shell_exec("hostname -I | awk '{print $1}'"); // Linux systems
} elseif (stripos($os, 'Darwin') !== false) {
$localIP = shell_exec("ifconfig | grep 'inet ' | grep -v '127.0.0.1' | awk '{print $2}' | head -n 1"); // macOS
} else {
$localIP = gethostbyname(gethostname()); // Fallback method
}

self::$_cachedLocalIP = trim($localIP ?? '');
}

return self::$_cachedLocalIP;
}

/**
* Used for testing only. Do not use in your code.
*/
public static function setCachedLocalIP(string $ip): void
{
self::$_cachedLocalIP = $ip;
}
}
16 changes: 16 additions & 0 deletions tests/Unit/LogFileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use Opcodes\LogViewer\LogFile;
use Opcodes\LogViewer\Logs\LogType;
use Opcodes\LogViewer\Utils\Utils;

test('log file can be instantiated with just a path to the file', function () {
$path = storage_path('logs/laravel.log');
Expand All @@ -23,3 +24,18 @@
->and($type->value)->toBe(LogType::DEFAULT)
->and($type->name())->toBe('Unknown');
});

test('log file identifier is based on server address', function () {
$path = storage_path('logs/laravel.log');
file_put_contents($path, str_repeat('0', 10)); // 10 bytes
// Set the cached local IP to a known value:
Utils::setCachedLocalIP($serverIp = '123.123.123.123');

$logFile = new LogFile($path);

expect($logFile->identifier)->toBe(
Utils::shortMd5($serverIp . ':' . $path) . '-laravel.log'
)->and($logFile->subFolderIdentifier())->toBe(
Utils::shortMd5($serverIp . ':' . $logFile->subFolder)
);
});
12 changes: 12 additions & 0 deletions tests/Unit/LogFolderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use Opcodes\LogViewer\LogFile;
use Opcodes\LogViewer\LogFolder;
use Opcodes\LogViewer\Utils\Utils;

test('LogFolder can get the earliest timestamp of the files it contains', function () {
$firstFile = Mockery::mock(new LogFile('folder/test.log'))
Expand All @@ -22,3 +23,14 @@

expect($folder->latestTimestamp())->toBe($firstFile->latestTimestamp());
});

test('log folder identifier is based on server address', function () {
// Set the cached local IP to a known value:
Utils::setCachedLocalIP($serverIp = '123.123.123.123');

$folder = new LogFolder('folder', []);

expect($folder->identifier)->toBe(
Utils::shortMd5($serverIp . ':' . $folder->path)
);
});

0 comments on commit 101eed9

Please sign in to comment.