ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilCalendarCopyFilesToTempDirectoryJob.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
29 
30 /* Copyright (c) 1998-2010 ILIAS open source, Extended GPL, see docs/LICENSE */
31 
37 {
38  private ilLogger $logger;
40 
41  protected string $target_directory;
42 
46  public function __construct()
47  {
48  global $DIC;
49 
50  $this->irss = $DIC->resourceStorage();
51  $this->logger = $DIC->logger()->cal();
52  }
53 
57  public function getInputTypes(): array
58  {
59  return
60  [
61  new SingleType(ilCalendarRessourceStorageCopyDefinition::class)
62  ];
63  }
64 
69  public function getOutputType(): Type
70  {
71  return new SingleType(StringValue::class);
72  }
73 
77  public function isStateless(): bool
78  {
79  return true;
80  }
81 
85  public function run(array $input, Observer $observer): Value
86  {
87  $cal_copy_def = $input[0];
88 
89  $this->logger->info('Called copy files job');
90 
91  $this->target_directory = $cal_copy_def->getTempDir();
92  //$this->target_directory = $input[1]->getValue();
93 
94  // create temp directory
95  $tmpdir = $this->createUniqueTempDirectory();
96  $targetdir = $this->createTargetDirectory($tmpdir);
97 
98  // copy files from source to temp directory
99  //$this->copyFiles($targetdir, $input[0]);
100  $this->copyFiles($targetdir, $cal_copy_def);
101 
102  // zip
103 
104  // return zip file name
105  $this->logger->debug('Returning new tempdirectory: ' . $targetdir);
106 
107  $out = new StringValue();
108  $out->setValue($targetdir);
109  return $out;
110  }
111 
117  protected function createUniqueTempDirectory(): string
118  {
119  $tmpdir = ilFileUtils::ilTempnam();
121  $this->logger->info('New temp directory: ' . $tmpdir);
122  return $tmpdir;
123  }
124 
125  protected function createTargetDirectory(string $a_tmpdir): string
126  {
127  $final_dir = $a_tmpdir . "/" . $this->target_directory;
128  ilFileUtils::makeDirParents($final_dir);
129  $this->logger->info('New final directory: ' . $final_dir);
130  return $final_dir;
131  }
132 
136  protected function copyFiles(string $tmpdir, ilCalendarRessourceStorageCopyDefinition $definition): void
137  {
138  foreach ($definition->getCopyDefinitions() as $copy_task) {
140  $this->copyWithRId($tmpdir, $copy_task);
141  } else {
142  $this->copyWithAbsolutePath($tmpdir, $copy_task);
143  }
144  }
145  }
146 
147  protected function copyWithRId(string $tmpdir, array $copy_task)
148  {
151  $resource_identification = $this->irss->manage()->find($rid);
152 
153  if(is_null($resource_identification)) {
154  $this->logger->notice('Cannot ressource identification of rid: ' . $rid);
155  return;
156  }
157 
158  $this->logger->debug('Creating directory: ' . $tmpdir . '/' . dirname($target_dir));
159  ilFileUtils::makeDirParents($tmpdir . '/' . dirname($target_dir));
160  $this->logger->debug('Copying ressource with id: ' . $rid . ' to ' . $tmpdir . '/' . $target_dir);
161 
162  file_put_contents(
164  $this->irss->consume()->stream($resource_identification)->getStream()
165  );
166  }
167 
168  protected function copyWithAbsolutePath(string $tmpdir, array $copy_task)
169  {
172 
173  if (!file_exists($absolute_path)) {
174  $this->logger->notice('Cannot find file: ' . $absolute_path);
175  return;
176  }
177 
178  $this->logger->debug('Creating directory: ' . $tmpdir . '/' . dirname($target_dir));
179  ilFileUtils::makeDirParents($tmpdir . '/' . dirname($target_dir));
180  $this->logger->debug('Copying from: ' . $absolute_path . ' to ' . $tmpdir . '/' . $target_dir);
181 
182  copy($absolute_path, $tmpdir . '/' . $target_dir);
183  }
184 
189  {
190  return 30;
191  }
192 }
getExpectedTimeOfTaskInSeconds()
int the amount of seconds this task usually taskes. If your task-duration scales with the the amount ...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static makeDirParents(string $a_dir)
Create a new directory and all parent directories.
global $DIC
Definition: feed.php:28
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$out
Definition: buildRTE.php:24
static ilTempnam(?string $a_temp_path=null)
Returns a unique and non existing Path for e temporary file or directory.
copyFiles(string $tmpdir, ilCalendarRessourceStorageCopyDefinition $definition)
Copy files.