ILIAS  release_8 Revision v8.23
DownloadMultipleConsumer.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
29 
35 {
37  protected ?int $revision_number = null;
38  protected bool $use_revision_titles = false;
42  protected array $resources;
45  protected string $zip_file_name;
46 
50  public function __construct(
51  array $resources,
52  StreamAccess $stream_access,
53  FileNamePolicy $file_name_policy,
54  string $zip_file_name
55  ) {
56  $this->resources = $resources;
57  $this->stream_access = $stream_access;
58  $this->file_name_policy = $file_name_policy;
59  $this->zip_file_name = $zip_file_name;
60  }
61 
62 
63  public function run(): void
64  {
65  global $DIC;
66 
67  $directory = CLIENT_DATA_DIR . '/temp';
68  $temp = tempnam($directory, 'zip');
69  $zip = new \ZipArchive();
70  $zip->open($temp, \ZipArchive::CREATE | \ZipArchive::OVERWRITE);
71 
72  foreach ($this->resources as $resource) {
73  $revision = $this->stream_access->populateRevision($resource->getCurrentRevision());
74  if ($this->use_revision_titles) {
75  $file_name = $this->file_name_policy->prepareFileNameForConsumer($revision->getTitle());
76  } else {
77  $file_name = $this->file_name_policy->prepareFileNameForConsumer(
78  $revision->getInformation()->getTitle()
79  );
80  }
81 
82  $zip->addFile($revision->maybeGetToken()->resolveStream()->getMetadata('uri'), $file_name);
83  }
84 
85  $zip->close();
86  $response = $DIC->http()->response();
88  $response = $response->withHeader(ResponseHeader::CONNECTION, 'close');
89  $response = $response->withHeader(ResponseHeader::ACCEPT_RANGES, 'bytes');
90  $response = $response->withHeader(
92  'attachment'
93  . '; filename="'
94  . $this->zip_file_name
95  . '"'
96  );
97  $fopen = fopen($temp, 'rb');
98  $response = $response->withBody(Streams::ofResource($fopen));
99  $DIC->http()->saveResponse($response);
100  $DIC->http()->sendResponse();
101  fclose($fopen);
102  unlink($temp);
103  $DIC->http()->close();
104  }
105 
106 
107  public function useRevisionTitlesForFileNames(bool $use_revision_titles): self
108  {
109  $this->use_revision_titles = $use_revision_titles;
110  return $this;
111  }
112 
113  public function setRevisionNumber(int $revision_number): DeliveryConsumer
114  {
115  $this->revision_number = $revision_number;
116  return $this;
117  }
118 
119  public function overrideFileName(string $file_name): DeliveryConsumer
120  {
121  $this->zip_file_name = $file_name;
122  return $this;
123  }
124 }
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
global $DIC
Definition: feed.php:28
__construct(array $resources, StreamAccess $stream_access, FileNamePolicy $file_name_policy, string $zip_file_name)
const CLIENT_DATA_DIR
Definition: constants.php:46
static ofResource($resource)
Wraps an already created resource with the stream abstraction.
Definition: Streams.php:65
$response