From 92a0afc15ef1f9e43159d273ac332adca6ada99c Mon Sep 17 00:00:00 2001 From: Matthew Barbour Date: Thu, 10 Oct 2019 10:25:37 -0500 Subject: [PATCH] Supporting function for cron uploader changes --- core/util.php | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/core/util.php b/core/util.php index 70b5f2ea..b79a2238 100644 --- a/core/util.php +++ b/core/util.php @@ -350,6 +350,51 @@ function join_url(string $base, string ...$paths) return $output; } +function get_dir_contents(string $dir): array +{ + if(empty($dir)) { + throw new Exception("dir required"); + } + if(!is_dir($dir)) { + return []; + } + $results = array_diff( + scandir( + $dir), + ['..', '.']); + + return $results; +} + +/** + * Returns amount of files & total size of dir. + */ +function scan_dir(string $path): array +{ + $bytestotal = 0; + $nbfiles = 0; + + $ite = new RecursiveDirectoryIterator( + $path, + FilesystemIterator::KEY_AS_PATHNAME | + FilesystemIterator::CURRENT_AS_FILEINFO | + FilesystemIterator::SKIP_DOTS); + foreach (new RecursiveIteratorIterator($ite) as $filename => $cur) { + try { + $filesize = $cur->getSize(); + $bytestotal += $filesize; + $nbfiles++; + } catch (RuntimeException $e) { + // This usually just means that the file got eaten by the import + continue; + } + } + + $size_mb = $bytestotal / 1048576; // to mb + $size_mb = number_format($size_mb, 2, '.', ''); + return ['path' => $path, 'total_files' => $nbfiles, 'total_mb' => $size_mb]; +} + /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\ * Debugging functions *