ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
class.ilZipBackgroundTaskHandler.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2013 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 include_once "Services/BackgroundTask/interfaces/interface.ilBackgroundTaskHandler.php";
5 
13 {
14  protected $task; // [ilBackgroundTask]
15  protected $filename; // [string]
16 
20  private $log;
21 
27  public function __construct()
28  {
29  $this->log = ilLoggerFactory::getLogger('btsk');
30  $this->settings = new ilSetting("fold");
31  }
32 
33  //
34  // setter/getter
35  //
36 
42  public function setDeliveryFilename($a_value)
43  {
44  $this->filename = $a_value;
45  }
46 
52  public function getDeliveryFilename()
53  {
54  return $this->filename;
55  }
56 
62  protected function setTask(ilBackgroundTask $a_task)
63  {
64  $this->task = $a_task;
65  }
66 
67 
68  //
69  // handler interface
70  //
71 
72  public function getTask()
73  {
74  return $this->task;
75  }
76 
77  public function process()
78  {
79  $this->log->debug("start");
80  // create temporary file to download
81  $tmpdir = $this->getTempFolderPath();
82  ilUtil::makeDirParents($tmpdir);
83 
84  // gather all files
85  $current_step = $this->gatherFiles();
86 
87  // has been cancelled?
88  if($this->task->isToBeCancelled())
89  {
90  return $this->cancel();
91  }
92 
93  // :TODO: create zip in several steps
94 
95  $this->task->setCurrentStep(++$current_step);
96  $this->task->save();
97 
98  // create archive to download
99  $tmpzipfile = $this->getTempZipFilePath();
100  ilUtil::zip($tmpdir, $tmpzipfile, true);
101  ilUtil::delDir($tmpdir);
102 
103  // has been cancelled?
104  if($this->task->isToBeCancelled())
105  {
106  return $this->cancel();
107  }
108 
109  $this->task->setStatus(ilBackgroundTask::STATUS_FINISHED);
110  $this->task->save();
111  $this->log->debug("end");
112  }
113 
119  public function cancel()
120  {
121  $this->log->debug("");
122  $this->deleteTempFiles();
123 
124  $this->task->setStatus(ilBackgroundTask::STATUS_CANCELLED);
125  $this->task->save();
126 
127  return true;
128  }
129 
133  public function finish()
134  {
135  global $ilCtrl;
136 
137  $this->deleteTempFiles(false);
138 
139  $ilCtrl->setParameterByClass("ilbackgroundtaskhub", "tid", $this->task->getId());
140  $url = $ilCtrl->getLinkTargetByClass("ilbackgroundtaskhub", "deliver", "", false, false);
141 
142  return array("redirect", $url);
143  }
144 
148  public function deliver()
149  {
150  $tmpzipfile = $this->getTempZipFilePath();
151  $deliverFilename = ilUtil::getAsciiFilename($this->getDeliveryFilename()) . ".zip";
152  ilUtil::deliverFile($tmpzipfile, $deliverFilename, '', false, true, false);
153  }
154 
155  public function deleteTaskAndFiles()
156  {
157  if(!$this->task)
158  {
159  return;
160  }
161 
162  $this->deleteTempFiles();
163 
164  $this->task->delete();
165  unset($this->task);
166  }
167 
168 
169  //
170  // zip handling
171  //
172 
178  abstract protected function gatherFiles();
179 
185  protected function deleteTempFiles($a_delete_zip = true)
186  {
187  $successful = true;
188 
189  // delete temp directory
190  $tmp_folder = $this->getTempFolderPath();
191  if(is_dir($tmp_folder))
192  {
193  ilUtil::delDir($tmp_folder);
194  $successful = !file_exists($tmp_folder);
195  }
196 
197  if($a_delete_zip)
198  {
199  // delete temp zip file
200  $tmp_file = $this->getTempZipFilePath();
201  if(file_exists($tmp_file))
202  {
203  $successful = @unlink($tmp_file);
204  }
205  }
206 
207  return $successful;
208  }
209 
210 
211  //
212  // temp directories
213  //
214 
220  protected function getTempFolderPath()
221  {
222  return $this->getTempBasePath() . ".tmp";
223  }
224 
230  protected function getTempZipFilePath()
231  {
232  return $this->getTempBasePath() . ".zip";
233  }
234 
240  protected function getTempBasePath()
241  {
242  return ilUtil::getDataDir() . "/temp/dl_" . $this->task->getId();
243  }
244 }
static makeDirParents($a_dir)
Create a new directory and all parent directories.
Background task handler interface.
ILIAS Setting Class.
getTempZipFilePath()
Gets the full path of the temporary zip file that gets created.
deleteTempFiles($a_delete_zip=true)
Deletes the temporary files and folders belonging to this download.
Background task handler for zip creation.
$url
Definition: shib_logout.php:72
deleteTaskAndFiles()
Remove task and its files.
setDeliveryFilename($a_value)
Sets the delivery file name.
global $ilCtrl
Definition: ilias.php:18
getDeliveryFilename()
Gets the delivery file name.
static deliverFile($a_file, $a_filename, $a_mime='', $isInline=false, $removeAfterDelivery=false, $a_exit_after=true)
deliver file for download via browser.
static zip($a_dir, $a_file, $compress_content=false)
zips given directory/file into given zip.file
gatherFiles()
Copy files to target directory.
Create styles array
The data for the language used.
getTempFolderPath()
Gets the temporary folder path to copy the files and folders to.
settings()
Definition: settings.php:2
static getDataDir()
get data directory (outside webspace)
setTask(ilBackgroundTask $a_task)
Set current task instance.
static getLogger($a_component_id)
Get component logger.
static delDir($a_dir, $a_clean_only=false)
removes a dir and all its content (subdirs and files) recursively
getTempBasePath()
Gets the temporary base path for all files and folders related to this download.