ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilCopyFilesToTempDirectoryJob.php
Go to the documentation of this file.
1<?php
2
25
33{
34 private ?ilLogger $logger;
35 protected string $target_directory = '';
36
37
41 public function __construct()
42 {
43 global $DIC;
44 $this->logger = $DIC->logger()->cal();
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(StringValue::class);
66 }
67
68
69 public function isStateless(): bool
70 {
71 return true;
72 }
73
74
79 public function run(array $input, Observer $observer): Value
80 {
81 $definition = $input[0];
82
83 $this->logger->info('Called copy files job');
84
85 $this->target_directory = $definition->getTempDir();
86
87 // create temp directory
88 $tmpdir = $this->createUniqueTempDirectory();
89 $targetdir = $this->createTargetDirectory($tmpdir);
90
91 // copy files from source to temp directory
92 //$this->copyFiles($targetdir, $input[0]);
93 $this->copyFiles($targetdir, $definition);
94
95 // zip
96
97 // return zip file name
98 $this->logger->debug('Returning new tempdirectory: ' . $targetdir);
99
100 $out = new StringValue();
101 $out->setValue($targetdir);
102
103 return $out;
104 }
105
106
112 protected function createUniqueTempDirectory(): string
113 {
114 $tmpdir = ilFileUtils::ilTempnam();
116 $this->logger->info('New temp directory: ' . $tmpdir);
117
118 return $tmpdir;
119 }
120
121
122 protected function createTargetDirectory(string $a_tmpdir): string
123 {
124 $final_dir = $a_tmpdir . "/" . $this->target_directory;
125 ilFileUtils::makeDirParents($final_dir);
126 $this->logger->info('New final directory: ' . $final_dir);
127
128 return $final_dir;
129 }
130
131
135 protected function copyFiles(string $tmpdir, ilCopyDefinition $definition): void
136 {
137 foreach ($definition->getCopyDefinitions() as $copy_task) {
138 $source_dir_or_file = $copy_task[ilCopyDefinition::COPY_SOURCE_DIR];
139 $target_dir_or_file = $copy_task[ilCopyDefinition::COPY_TARGET_DIR];
140 $absolute_path_of_target_dir_or_file = $tmpdir . '/' . $target_dir_or_file;
141 $absolute_directory_of_target_dir_or_file = $tmpdir . '/' . dirname($target_dir_or_file);
142
143 $this->logger->debug('Creating directory: ' . $tmpdir . '/' . dirname($target_dir_or_file));
144
146 $absolute_directory_of_target_dir_or_file
147 );
148
149 if (!file_exists($source_dir_or_file)) {
150 // if the "file" to be copied is an empty folder the directory has to be created so it will be contained in the download zip
151 $is_empty_folder = preg_match_all("/\/$/", $target_dir_or_file);
152 if ($is_empty_folder && !file_exists($absolute_path_of_target_dir_or_file)) {
153 mkdir($absolute_path_of_target_dir_or_file);
154 $this->logger->notice('Empty folder has been created: ' . $tmpdir . '/' . $source_dir_or_file);
155 } else {
156 $this->logger->notice('Cannot find file: ' . $source_dir_or_file);
157 }
158 continue;
159 }
160
161 $this->logger->debug(
162 'Copying from: ' .
163 $source_dir_or_file .
164 ' to ' .
165 $absolute_path_of_target_dir_or_file
166 );
167 if (
168 !is_dir($source_dir_or_file) && is_file($source_dir_or_file)
169 && !is_dir($absolute_path_of_target_dir_or_file)
170 ) {
171 copy(
172 $source_dir_or_file,
173 $absolute_path_of_target_dir_or_file
174 );
175 }
176 }
177 }
178
179
184 {
185 return 30;
186 }
187}
$out
Definition: buildRTE.php:24
Description of class class.
getCopyDefinitions()
Get copy definitions.
run(array $input, Observer $observer)
run the job
getExpectedTimeOfTaskInSeconds()
int the amount of seconds this task usually taskes. If your task-duration scales with the the amount ...
copyFiles(string $tmpdir, ilCopyDefinition $definition)
Copy files.
static makeDirParents(string $a_dir)
Create a new directory and all parent directories.
static ilTempnam(?string $a_temp_path=null)
Returns a unique and non existing Path for e temporary file or directory.
Component logger with individual log levels by component id.
global $DIC
Definition: shib_login.php:26