ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilCheckSumOfFileSizesJob.php
Go to the documentation of this file.
1 <?php
24 
32 {
33  private ?ilLogger $logger;
34  protected \ilSetting $settings;
35 
36 
40  public function __construct()
41  {
42  global $DIC;
43  $this->logger = $DIC->logger()->cal();
44  $this->settings = new ilSetting("fold");
45  }
46 
47 
51  public function getInputTypes(): array
52  {
53  return
54  [
55  new SingleType(ilCopyDefinition::class),
56  ];
57  }
58 
59 
63  public function getOutputType(): Type
64  {
65  return new SingleType(ilCopyDefinition::class);
66  }
67 
68 
72  public function isStateless(): bool
73  {
74  return true;
75  }
76 
77 
82  public function run(array $input, \ILIAS\BackgroundTasks\Observer $observer): Value
83  {
84  $this->logger->debug('Start checking adherence to maxsize!');
85  $this->logger->dump($input);
86  $definition = $input[0];
87  $object_ref_ids = $definition->getObjectRefIds();
88 
89  // get global limit (max sum of individual file-sizes) from file settings
90  $general = new General();
91  $size_limit = $general->getDownloadLimitinMB();
92  $size_limit_bytes = $size_limit * 1024 * 1024;
93  $this->logger->debug('Global limit (max sum of all file-sizes) in file-settings: ' . $size_limit_bytes . ' bytes');
94  // get sum of individual file-sizes
95  $total_bytes = 0;
96  $this->calculateRecursive($object_ref_ids, $total_bytes);
97  $this->logger->debug('Calculated sum of all file-sizes: ' . $total_bytes . 'MB');
98  // check if calculated total size adheres top global limit
99  $adheres_to_limit = new BooleanValue();
100  $adheres_to_limit->setValue(true);
101  if ($total_bytes > $size_limit_bytes) {
102  $adheres_to_limit->setValue(false);
103  }
104 
105  $definition->setSumFileSizes($total_bytes);
106  $definition->setAdheresToLimit($adheres_to_limit);
107 
108  return $definition;
109  }
110 
111 
116  protected function calculateRecursive(array $a_ref_ids, int &$a_file_size): void
117  {
118  global $DIC;
119  $tree = $DIC['tree'];
120 
121  // parse folders
122  foreach ($a_ref_ids as $ref_id) {
123  if (!$this->validateAccess($ref_id)) {
124  continue;
125  }
126 
127  // we are only interested in folders and files
128  switch (ilObject::_lookupType($ref_id, true)) {
129  case "fold":
130  // get child objects
131  $subtree = $tree->getChildsByTypeFilter($ref_id, array("fold", "file"));
132  if (count($subtree) > 0) {
133  $child_ref_ids = array();
134  foreach ($subtree as $child) {
135  $child_ref_ids[] = $child["ref_id"];
136  }
137  $this->calculateRecursive($child_ref_ids, $a_file_size);
138  }
139  break;
140 
141  case "file":
142  $a_file_size += ilObjFileAccess::_lookupFileSize($ref_id);
143  break;
144  }
145  }
146  }
147 
148 
154  protected function validateAccess(int $ref_id): bool
155  {
156  global $DIC;
157  $ilAccess = $DIC['ilAccess'];
158 
159  if (!$ilAccess->checkAccess("read", "", $ref_id)) {
160  return false;
161  }
162  return !ilObject::_isInTrash($ref_id);
163  }
164 
165 
170  {
171  return 30;
172  }
173 }
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
$general
SECTIONS.
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.