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.
This commit is contained in:
Mihail Geshoski 2020-07-14 15:11:10 +08:00 committed by Jenkins
parent 303119ad5b
commit 09a55f952e

View File

@ -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.
*