Skip to content

Commit

Permalink
Fix left hand file name sorting (#268)
Browse files Browse the repository at this point in the history
* Sort files before returning them

* Fix indentation

* Reset array keys before returning it
  • Loading branch information
sdapkus authored Apr 6, 2022
1 parent 199b4f2 commit 52e7c70
Showing 1 changed file with 95 additions and 95 deletions.
190 changes: 95 additions & 95 deletions src/Rap2hpoutre/LaravelLogViewer/LaravelLogViewer.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,27 +55,26 @@ public function __construct()
public function setFolder($folder)
{
if (app('files')->exists($folder)) {

$this->folder = $folder;
}
else if(is_array($this->storage_path)) {

} else if (is_array($this->storage_path)) {

foreach ($this->storage_path as $value) {

$logsPath = $value . '/' . $folder;

if (app('files')->exists($logsPath)) {
$this->folder = $folder;
break;
}
}
} else {
$logsPath = $this->storage_path . '/' . $folder;
if (app('files')->exists($logsPath)) {
$this->folder = $folder;
}

$logsPath = $this->storage_path . '/' . $folder;
if (app('files')->exists($logsPath)) {
$this->folder = $folder;
}

}
}

Expand All @@ -101,11 +100,11 @@ public function pathToLogFile($file)
{

if (app('files')->exists($file)) { // try the absolute path

return $file;
}
if (is_array($this->storage_path)) {

foreach ($this->storage_path as $folder) {
if (app('files')->exists($folder . '/' . $file)) { // try the absolute path
$file = $folder . '/' . $file;
Expand All @@ -120,9 +119,9 @@ public function pathToLogFile($file)
$file = $logsPath . '/' . $file;
// check if requested file is really in the logs directory
if (dirname($file) !== $logsPath) {
throw new \Exception('No such log file: '.$file);
throw new \Exception('No such log file: ' . $file);
}

return $file;
}

Expand Down Expand Up @@ -236,47 +235,47 @@ public function all()
}

/**Creates a multidimensional array
* of subdirectories and files
*
* @param null $path
*
* @return array
*/
* of subdirectories and files
*
* @param null $path
*
* @return array
*/
public function foldersAndFiles($path = null)
{
$contents = array();
$dir = $path ? $path : $this->storage_path;
foreach (scandir($dir) as $node) {
if ($node == '.' || $node == '..') continue;
$path = $dir . '\\' . $node;
if (is_dir($path)) {
$contents[$path] = $this->foldersAndFiles($path);
} else {
$contents[] = $path;
}
}

return $contents;
$contents = array();
$dir = $path ? $path : $this->storage_path;
foreach (scandir($dir) as $node) {
if ($node == '.' || $node == '..') continue;
$path = $dir . '\\' . $node;
if (is_dir($path)) {
$contents[$path] = $this->foldersAndFiles($path);
} else {
$contents[] = $path;
}
}

return $contents;
}

/**Returns an array of
* all subdirectories of specified directory
*
* @param string $folder
*
* @return array
*/
/**Returns an array of
* all subdirectories of specified directory
*
* @param string $folder
*
* @return array
*/
public function getFolders($folder = '')
{
$folders = [];
$listObject = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator($this->storage_path.'/'.$folder, \RecursiveDirectoryIterator::SKIP_DOTS),
\RecursiveIteratorIterator::CHILD_FIRST
);
foreach ($listObject as $fileinfo) {
if($fileinfo->isDir()) $folders[] = $fileinfo->getRealPath();
}
return $folders;
$folders = [];
$listObject = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator($this->storage_path . '/' . $folder, \RecursiveDirectoryIterator::SKIP_DOTS),
\RecursiveIteratorIterator::CHILD_FIRST
);
foreach ($listObject as $fileinfo) {
if ($fileinfo->isDir()) $folders[] = $fileinfo->getRealPath();
}
return $folders;
}


Expand All @@ -297,77 +296,78 @@ public function getFolderFiles($basename = false)
public function getFiles($basename = false, $folder = '')
{
$files = [];
$pattern = function_exists('config') ? config('logviewer.pattern', '*.log') : '*.log';
$fullPath = $this->storage_path.'/'.$folder;
$pattern = function_exists('config') ? config('logviewer.pattern', '*.log') : '*.log';
$fullPath = $this->storage_path . '/' . $folder;

$listObject = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator($fullPath, \RecursiveDirectoryIterator::SKIP_DOTS),
\RecursiveIteratorIterator::CHILD_FIRST
);

$listObject = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator($fullPath, \RecursiveDirectoryIterator::SKIP_DOTS),
\RecursiveIteratorIterator::CHILD_FIRST
);
foreach ($listObject as $fileinfo) {
if (!$fileinfo->isDir() && strtolower(pathinfo($fileinfo->getRealPath(), PATHINFO_EXTENSION)) == explode('.', $pattern)[1])
$files[] = $basename ? basename($fileinfo->getRealPath()) : $fileinfo->getRealPath();
}

foreach ($listObject as $fileinfo) {
if(!$fileinfo->isDir() && strtolower(pathinfo($fileinfo->getRealPath(), PATHINFO_EXTENSION)) == explode('.', $pattern)[1])
$files[] = $basename ? basename($fileinfo->getRealPath()) : $fileinfo->getRealPath();
}
return $files;
arsort($files);

return array_values($files);
}

/**
* @return string
*/
* @return string
*/
public function getStoragePath()
{
return $this->storage_path;
return $this->storage_path;
}

/**
* @param $path
*
* @return void
*/
public function setStoragePath($path)
{
$this->storage_path = $path;
}
/**
* @param $path
*
* @return void
*/
public function setStoragePath($path)
{
$this->storage_path = $path;
}

public static function directoryTreeStructure($storage_path, array $array)
{
foreach ($array as $k => $v) {
if(is_dir( $k )) {
foreach ($array as $k => $v) {
if (is_dir($k)) {

$exploded = explode( "\\", $k );
$show = last( $exploded );
$exploded = explode("\\", $k);
$show = last($exploded);

echo '<div class="list-group folder">
<a href="?f='. \Illuminate\Support\Facades\Crypt::encrypt($k).'">
echo '<div class="list-group folder">
<a href="?f=' . \Illuminate\Support\Facades\Crypt::encrypt($k) . '">
<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span
class="fa fa-folder"></span> '.$show.'
class="fa fa-folder"></span> ' . $show . '
</a>
</div>';

if ( is_array( $v ) ) {
self::directoryTreeStructure( $storage_path, $v );
}
if (is_array($v)) {
self::directoryTreeStructure($storage_path, $v);
}

}
else {
} else {

$exploded = explode( "\\", $v );
$show2 = last( $exploded );
$folder = str_replace( $storage_path, "", rtrim( str_replace( $show2, "", $v ), "\\" ) );
$file = $v;
$exploded = explode("\\", $v);
$show2 = last($exploded);
$folder = str_replace($storage_path, "", rtrim(str_replace($show2, "", $v), "\\"));
$file = $v;


echo '<div class="list-group">
<a href="?l='.\Illuminate\Support\Facades\Crypt::encrypt($file).'&f='.\Illuminate\Support\Facades\Crypt::encrypt($folder).'">
echo '<div class="list-group">
<a href="?l=' . \Illuminate\Support\Facades\Crypt::encrypt($file) . '&f=' . \Illuminate\Support\Facades\Crypt::encrypt($folder) . '">
<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span> <span
class="fa fa-file"></span> '.$show2.'
class="fa fa-file"></span> ' . $show2 . '
</a>
</div>';

}
}
}
}

return;
}
Expand Down

0 comments on commit 52e7c70

Please # to comment.