ILIAS  release_7 Revision v7.30-3-g800a261c036
class.ilCopyFilesToTempDirectoryJob.php
Go to the documentation of this file.
1<?php
2
8
9/* Copyright (c) 1998-2010 ILIAS open source, Extended GPL, see docs/LICENSE */
10
18{
19
23 private $logger = null;
28
29
33 public function __construct()
34 {
35 $this->logger = $GLOBALS['DIC']->logger()->cal();
36 }
37
38
41 public function getInputTypes()
42 {
43 return
44 [
45 new SingleType(ilCopyDefinition::class),
46 ];
47 }
48
49
54 public function getOutputType()
55 {
56 return new SingleType(StringValue::class);
57 }
58
59
60 public function isStateless()
61 {
62 return true;
63 }
64
65
72 public function run(array $input, Observer $observer)
73 {
74 $definition = $input[0];
75
76 $this->logger->info('Called copy files job');
77
78 $this->target_directory = $definition->getTempDir();
79
80 // create temp directory
81 $tmpdir = $this->createUniqueTempDirectory();
82 $targetdir = $this->createTargetDirectory($tmpdir);
83
84 // copy files from source to temp directory
85 //$this->copyFiles($targetdir, $input[0]);
86 $this->copyFiles($targetdir, $definition);
87
88 // zip
89
90 // return zip file name
91 $this->logger->debug('Returning new tempdirectory: ' . $targetdir);
92
93 $out = new StringValue();
94 $out->setValue($targetdir);
95
96 return $out;
97 }
98
99
105 protected function createUniqueTempDirectory()
106 {
107 $tmpdir = ilUtil::ilTempnam();
108 ilUtil::makeDirParents($tmpdir);
109 $this->logger->info('New temp directory: ' . $tmpdir);
110
111 return $tmpdir;
112 }
113
114
115 protected function createTargetDirectory($a_tmpdir)
116 {
117 $final_dir = $a_tmpdir . "/" . $this->target_directory;
118 ilUtil::makeDirParents($final_dir);
119 $this->logger->info('New final directory: ' . $final_dir);
120
121 return $final_dir;
122 }
123
124
131 protected function copyFiles($tmpdir, ilCopyDefinition $definition)
132 {
133 foreach ($definition->getCopyDefinitions() as $copy_task) {
134 $source_dir_or_file = $copy_task[ilCopyDefinition::COPY_SOURCE_DIR];
135 $target_dir_or_file = $copy_task[ilCopyDefinition::COPY_TARGET_DIR];
136 $absolute_path_of_target_dir_or_file = $tmpdir . '/' . $target_dir_or_file;
137 $absolute_directory_of_target_dir_or_file = $tmpdir . '/' . dirname($target_dir_or_file);
138
139 $this->logger->debug('Creating directory: ' . $tmpdir . '/' . dirname($target_dir_or_file));
140
142 $absolute_directory_of_target_dir_or_file
143 );
144
145 if (!file_exists($source_dir_or_file)) {
146 // 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
147 $is_empty_folder = preg_match_all("/\/$/", $target_dir_or_file);
148 if ($is_empty_folder && !file_exists($absolute_path_of_target_dir_or_file)) {
149 mkdir($absolute_path_of_target_dir_or_file);
150 $this->logger->notice('Empty folder has been created: ' . $tmpdir . '/' . $source_dir_or_file);
151 } else {
152 $this->logger->notice('Cannot find file: ' . $source_dir_or_file);
153 }
154 continue;
155 }
156
157 $this->logger->debug(
158 'Copying from: ' .
159 $source_dir_or_file .
160 ' to ' .
161 $absolute_path_of_target_dir_or_file
162 );
163 if (
164 !is_dir($source_dir_or_file) && is_file($source_dir_or_file)
165 && !is_dir($absolute_path_of_target_dir_or_file)
166 ) {
167 copy(
168 $source_dir_or_file,
169 $absolute_path_of_target_dir_or_file
170 );
171 }
172 }
173
174 return;
175 }
176
177
182 {
183 return 30;
184 }
185}
if(!defined('PATH_SEPARATOR')) $GLOBALS['_PEAR_default_error_mode']
Definition: PEAR.php:64
An exception for terminatinating execution or to throw for unit testing.
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($tmpdir, ilCopyDefinition $definition)
Copy files.
static ilTempnam($a_temp_path=null)
Returns a unique and non existing Path for e temporary file or directory.
static makeDirParents($a_dir)
Create a new directory and all parent directories.