From 6f4e2cb7991bb434a167f6eff4f98092a823f0fd Mon Sep 17 00:00:00 2001 From: Mihail Geshoski Date: Tue, 14 Jul 2020 15:11:10 +0800 Subject: [PATCH] MDL-65115 core_files: Add get_total_content_size() in stored_file class New function get_total_content_size() is introduced in the stored_file class. The puprose of this function is to calculate and return the total size (in bytes) of the content of an archive file. --- lib/filestorage/stored_file.php | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/lib/filestorage/stored_file.php b/lib/filestorage/stored_file.php index fc01b1ecae7..d04b236b9b0 100644 --- a/lib/filestorage/stored_file.php +++ b/lib/filestorage/stored_file.php @@ -496,6 +496,27 @@ class stored_file { return $this->filesystem->list_files($this, $packer); } + /** + * Returns the total size (in bytes) of the contents of an archive. + * + * @param file_packer $packer file packer instance + * @return int|null total size in bytes + */ + public function get_total_content_size(file_packer $packer): ?int { + // Fetch the contents of the archive. + $files = $this->list_files($packer); + + // Early return if the value of $files is not of type array. + // This can happen when the utility class is unable to open or read the contents of the archive. + if (!is_array($files)) { + return null; + } + + return array_reduce($files, function ($contentsize, $file) { + return $contentsize + $file->size; + }, 0); + } + /** * Extract file to given file path (real OS filesystem), existing files are overwritten. *