ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
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  return $this->cancel();
90  }
91 
92  // :TODO: create zip in several steps
93 
94  $this->task->setCurrentStep(++$current_step);
95  $this->task->save();
96 
97  // create archive to download
98  $tmpzipfile = $this->getTempZipFilePath();
99  ilUtil::zip($tmpdir, $tmpzipfile, true);
100  ilUtil::delDir($tmpdir);
101 
102  // has been cancelled?
103  if ($this->task->isToBeCancelled()) {
104  return $this->cancel();
105  }
106 
107  $this->task->setStatus(ilBackgroundTask::STATUS_FINISHED);
108  $this->task->save();
109  $this->log->debug("end");
110  }
111 
117  public function cancel()
118  {
119  $this->log->debug("");
120  $this->deleteTempFiles();
121 
122  $this->task->setStatus(ilBackgroundTask::STATUS_CANCELLED);
123  $this->task->save();
124 
125  return true;
126  }
127 
131  public function finish()
132  {
133  global $DIC;
134  $ilCtrl = $DIC['ilCtrl'];
135 
136  $this->deleteTempFiles(false);
137 
138  $ilCtrl->setParameterByClass("ilbackgroundtaskhub", "tid", $this->task->getId());
139  $url = $ilCtrl->getLinkTargetByClass("ilbackgroundtaskhub", "deliver", "", false, false);
140 
141  return array("redirect", $url);
142  }
143 
147  public function deliver()
148  {
149  $tmpzipfile = $this->getTempZipFilePath();
150  $deliverFilename = ilUtil::getAsciiFilename($this->getDeliveryFilename()) . ".zip";
151  ilUtil::deliverFile($tmpzipfile, $deliverFilename, '', false, true, false);
152  }
153 
154  public function deleteTaskAndFiles()
155  {
156  if (!$this->task) {
157  return;
158  }
159 
160  $this->deleteTempFiles();
161 
162  $this->task->delete();
163  unset($this->task);
164  }
165 
166 
167  //
168  // zip handling
169  //
170 
176  abstract protected function gatherFiles();
177 
183  protected function deleteTempFiles($a_delete_zip = true)
184  {
185  $successful = true;
186 
187  // delete temp directory
188  $tmp_folder = $this->getTempFolderPath();
189  if (is_dir($tmp_folder)) {
190  ilUtil::delDir($tmp_folder);
191  $successful = !file_exists($tmp_folder);
192  }
193 
194  if ($a_delete_zip) {
195  // delete temp zip file
196  $tmp_file = $this->getTempZipFilePath();
197  if (file_exists($tmp_file)) {
198  $successful = @unlink($tmp_file);
199  }
200  }
201 
202  return $successful;
203  }
204 
205 
206  //
207  // temp directories
208  //
209 
215  protected function getTempFolderPath()
216  {
217  return $this->getTempBasePath() . ".tmp";
218  }
219 
225  protected function getTempZipFilePath()
226  {
227  return $this->getTempBasePath() . ".zip";
228  }
229 
235  protected function getTempBasePath()
236  {
237  return ilUtil::getDataDir() . "/temp/dl_" . $this->task->getId();
238  }
239 }
static makeDirParents($a_dir)
Create a new directory and all parent directories.
Background task handler interface.
settings()
Definition: settings.php:2
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.
global $DIC
Definition: saml.php:7
Background task handler for zip creation.
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 zip($a_dir, $a_file, $compress_content=false)
zips given directory/file into given zip.file
gatherFiles()
Copy files to target directory.
getTempFolderPath()
Gets the temporary folder path to copy the files and folders to.
static getDataDir()
get data directory (outside webspace)
setTask(ilBackgroundTask $a_task)
Set current task instance.
static getLogger($a_component_id)
Get component logger.
$url
static delDir($a_dir, $a_clean_only=false)
removes a dir and all its content (subdirs and files) recursively
static deliverFile( $a_file, $a_filename, $a_mime='', $isInline=false, $removeAfterDelivery=false, $a_exit_after=true)
deliver file for download via browser.
getTempBasePath()
Gets the temporary base path for all files and folders related to this download.