Skip to content

Commit

Permalink
MDL-65115 core_files: Add get_total_content_size() in stored_file class
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Mihail Geshoski authored and Jenkins committed Sep 3, 2020
1 parent 4b20cc2 commit 7e2020b
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions lib/filestorage/stored_file.php
Expand Up @@ -496,6 +496,27 @@ public function list_files(file_packer $packer) {
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.
*
Expand Down

0 comments on commit 7e2020b

Please sign in to comment.