ILIAS  release_8 Revision v8.23
class.ilCheckSumOfFileSizesJob.php
Go to the documentation of this file.
1 <?php
23 
31 {
32  private ?ilLogger $logger;
33  protected \ilSetting $settings;
34 
35 
39  public function __construct()
40  {
41  global $DIC;
42  $this->logger = $DIC->logger()->cal();
43  $this->settings = new ilSetting("fold");
44  }
45 
46 
50  public function getInputTypes(): array
51  {
52  return
53  [
54  new SingleType(ilCopyDefinition::class),
55  ];
56  }
57 
58 
62  public function getOutputType(): Type
63  {
64  return new SingleType(ilCopyDefinition::class);
65  }
66 
67 
71  public function isStateless(): bool
72  {
73  return true;
74  }
75 
76 
81  public function run(array $input, \ILIAS\BackgroundTasks\Observer $observer): Value
82  {
83  $this->logger->debug('Start checking adherence to maxsize!');
84  $this->logger->dump($input);
85  $definition = $input[0];
86  $object_ref_ids = $definition->getObjectRefIds();
87 
88  // get global limit (max sum of individual file-sizes) from file settings
89  $size_limit = (int) $this->settings->get("bgtask_download_limit", '0');
90  $size_limit_bytes = $size_limit * 1024 * 1024;
91  $this->logger->debug('Global limit (max sum of all file-sizes) in file-settings: ' . $size_limit_bytes . ' bytes');
92  // get sum of individual file-sizes
93  $total_bytes = 0;
94  $this->calculateRecursive($object_ref_ids, $total_bytes);
95  $this->logger->debug('Calculated sum of all file-sizes: ' . $total_bytes . 'MB');
96  // check if calculated total size adheres top global limit
97  $adheres_to_limit = new BooleanValue();
98  $adheres_to_limit->setValue(true);
99  if ($total_bytes > $size_limit_bytes) {
100  $adheres_to_limit->setValue(false);
101  }
102 
103  $definition->setSumFileSizes($total_bytes);
104  $definition->setAdheresToLimit($adheres_to_limit);
105 
106  return $definition;
107  }
108 
109 
114  protected function calculateRecursive(array $a_ref_ids, int &$a_file_size): void
115  {
116  global $DIC;
117  $tree = $DIC['tree'];
118 
119  // parse folders
120  foreach ($a_ref_ids as $ref_id) {
121  if (!$this->validateAccess($ref_id)) {
122  continue;
123  }
124 
125  // we are only interested in folders and files
126  switch (ilObject::_lookupType($ref_id, true)) {
127  case "fold":
128  // get child objects
129  $subtree = $tree->getChildsByTypeFilter($ref_id, array("fold", "file"));
130  if (count($subtree) > 0) {
131  $child_ref_ids = array();
132  foreach ($subtree as $child) {
133  $child_ref_ids[] = $child["ref_id"];
134  }
135  $this->calculateRecursive($child_ref_ids, $a_file_size);
136  }
137  break;
138 
139  case "file":
140  $a_file_size += ilObjFileAccess::_lookupFileSize($ref_id);
141  break;
142  }
143  }
144  }
145 
146 
152  protected function validateAccess(int $ref_id): bool
153  {
154  global $DIC;
155  $ilAccess = $DIC['ilAccess'];
156 
157  if (!$ilAccess->checkAccess("read", "", $ref_id)) {
158  return false;
159  }
160  return !ilObject::_isInTrash($ref_id);
161  }
162 
163 
168  {
169  return 30;
170  }
171 }
Class ChatMainBarProvider .
run(array $input, \ILIAS\BackgroundTasks\Observer $observer)
static _lookupFileSize(int $a_id, bool $by_reference=true)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
global $DIC
Definition: feed.php:28
$ref_id
Definition: ltiauth.php:67
static _isInTrash(int $ref_id)
getExpectedTimeOfTaskInSeconds()
int the amount of seconds this task usually taskes. If your task-duration scales with the the amount ...
calculateRecursive(array $a_ref_ids, int &$a_file_size)
Calculates the number and size of the files being downloaded recursively.
static _lookupType(int $id, bool $reference=false)
validateAccess(int $ref_id)
Check file access.