From afc6b7bd686a72eb64005e194945ee3db063acc1 Mon Sep 17 00:00:00 2001 From: EV-soft Date: Sat, 12 Jun 2021 11:19:53 +0200 Subject: [PATCH] Improve sorting by size function Fix sort order of "Size" column, View size and other information about folders Move repetitive test for posix outside foreach loop --- tinyfilemanager.php | 80 ++++++++++++++++++++++++++------------------- 1 file changed, 47 insertions(+), 33 deletions(-) diff --git a/tinyfilemanager.php b/tinyfilemanager.php index 08bb05c..de729da 100644 --- a/tinyfilemanager.php +++ b/tinyfilemanager.php @@ -1,6 +1,6 @@ ".lang('Root path')." \"{$root_path}\" ".lang('not found!')." "; + echo "

".lng('Root path')." \"{$root_path}\" ".lng('not found!')."

"; exit; } @@ -1892,10 +1892,6 @@ fm_show_message(); $num_files = count($files); $num_folders = count($folders); $all_files_size = 0; -$posix = (function_exists('posix_getpwuid') && function_exists('posix_getgrgid')); -$owner = array('name' => '?'); -$group = array('name' => '?'); - $tableTheme = (FM_THEME == "dark") ? "text-white bg-dark table-dark" : "bg-white"; ?>
@@ -1917,9 +1913,7 @@ $tableTheme = (FM_THEME == "dark") ? "text-white bg-dark table-dark" : "bg-white - - @@ -1940,6 +1934,13 @@ $tableTheme = (FM_THEME == "dark") ? "text-white bg-dark table-dark" : "bg-white '?'); + $group = array('name' => '?'); + } $ii = 3399; foreach ($folders as $f) { $is_link = is_link($path . '/' . $f); @@ -1947,15 +1948,23 @@ $tableTheme = (FM_THEME == "dark") ? "text-white bg-dark table-dark" : "bg-white $modif_raw = filemtime($path . '/' . $f); $modif = date(FM_DATETIME_FORMAT, $modif_raw); if ($calc_folder) { - $filesize_raw = fm_get_directorysize($path . '/' . $f); + $dir_data = fm_get_directoryInfo($path . '/' . $f); + $filesize_raw = $dir_data[0]; + $fileCount = $dir_data[1]; + $dir_Count = $dir_data[2]; $filesize = fm_get_filesize($filesize_raw); + $dir_info = lng('Folder content').': + '. $dir_Count. ' '. lng('folders').' + '. $fileCount. ' '. lng('files').' + '. $filesize_raw. ' '. lng('bytes'); } else { $filesize_raw = ""; $filesize = lng('Folder'); } + $sizeSort= ''; $perms = substr(decoct(fileperms($path . '/' . $f)), -4); - if ($posix) { + if ($posix==true) { $owner = posix_getpwuid(fileowner($path . '/' . $f)); $group = posix_getgrgid(filegroup($path . '/' . $f)); } @@ -1972,16 +1981,14 @@ $tableTheme = (FM_THEME == "dark") ? "text-white bg-dark table-dark" : "bg-white
' . readlink($path . '/' . $f) . '' : '') ?>
- "> - + + - + - - @@ -2005,8 +2012,9 @@ $tableTheme = (FM_THEME == "dark") ? "text-white bg-dark table-dark" : "bg-white $filesize = fm_get_filesize($filesize_raw); $filelink = '?p=' . urlencode(FM_PATH) . '&view=' . urlencode($f); $all_files_size += $filesize_raw; + $sizeSort= ''; $perms = substr(decoct(fileperms($path . '/' . $f)), -4); - if ($posix) { + if ($posix==true) { $owner = posix_getpwuid(fileowner($path . '/' . $f)); $group = posix_getgrgid(filegroup($path . '/' . $f)); } @@ -2033,16 +2041,14 @@ $tableTheme = (FM_THEME == "dark") ? "text-white bg-dark table-dark" : "bg-white ' . readlink($path . '/' . $f) . '' : '') ?> - "> - - - + + + + - - @@ -2504,30 +2510,29 @@ function fm_get_size($file) function fm_get_filesize($size) { $size = (float) $size; - $units = array('B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'); + $units = array(' B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'); $power = $size > 0 ? floor(log($size, 1024)) : 0; - return sprintf('%s %s', round($size / pow(1024, $power), 2), $units[$power]); + return sprintf('%s %s', number_format(round($size / pow(1024, $power), 2), $size > 1024 ? 2 : 0), $units[$power]); } /** - * Get director total size + * Get director total size and info * @param string $directory - * @return int + * @return array or string */ -function fm_get_directorysize($directory) { +function fm_get_directoryInfo($directory) { global $calc_folder; if ($calc_folder==true) { // Slower output - $size = 0; $count= 0; $dirCount= 0; + $size = 0; $count= 0; $dirCount= -2; foreach(new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory)) as $file) if ($file->isFile()) { $size+=$file->getSize(); $count++; } else if ($file->isDir()) { $dirCount++; } - // return [$size, $count, $dirCount]; - return $size; + return [$size, $count, $dirCount]; } - else return 'Folder'; // Quick output + else return lng('Folder'); // Quick output } /** @@ -2687,6 +2692,7 @@ function fm_get_file_icon_class($path) case 'cpp': case 'cs': case 'py': + case 'rs': case 'map': case 'lock': case 'dtd': @@ -2705,11 +2711,13 @@ function fm_get_file_icon_class($path) case 'scss': $img = 'fa fa-css3'; break; + case 'bz2': case 'zip': case 'rar': case 'gz': case 'tar': case '7z': + case 'xz': $img = 'fa fa-file-archive-o'; break; case 'php': @@ -2747,6 +2755,7 @@ function fm_get_file_icon_class($path) case 'm3u8': case 'pls': case 'cue': + case 'xspf': $img = 'fa fa-headphones'; break; case 'avi': @@ -2778,6 +2787,7 @@ function fm_get_file_icon_class($path) $img = 'fa fa-file-text-o'; break; case 'bak': + case 'swp': $img = 'fa fa-clipboard'; break; case 'doc': @@ -2944,6 +2954,10 @@ function fm_get_file_mimes($extension) $fileTypes['php'] = ['application/x-php']; $fileTypes['html'] = ['text/html']; $fileTypes['txt'] = ['text/plain']; + //Unknown mime-types should be 'application/octet-stream' + if(empty($fileTypes[$extension])) { + $fileTypes[$extension] = ['application/octet-stream']; + } return $fileTypes[$extension]; } @@ -3551,7 +3565,8 @@ $isStickyNavBar = $sticky_navbar ? 'navbar-fixed' : 'navbar-normal'; .fa.fa-home { font-size:1.3em;vertical-align:bottom } .path { margin-bottom:10px } form.dropzone { min-height:200px;border:2px dashed #007bff;line-height:6rem; } - .right { text-align:right } + .right { text-align:right } + .bold { font-weight:600 } .center, .close, .login-form { text-align:center } .message { padding:4px 7px;border:1px solid #ddd;background-color:#fff } .message.ok { border-color:green;color:green } @@ -3755,7 +3770,6 @@ $isStickyNavBar = $sticky_navbar ? 'navbar-fixed' : 'navbar-normal'; -