ILIAS  trunk Revision v11.0_alpha-1713-gd8962da2f67
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
DownloadMultipleConsumer.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
30 
36 {
37  protected ResourceCollection $collection;
38  protected ?int $revision_number = null;
39  protected bool $use_revision_titles = false;
40 
44  public function __construct(protected array $resources, private StreamAccess $stream_access, protected FileNamePolicy $file_name_policy, protected string $zip_file_name)
45  {
46  }
47 
48 
49  public function run(): void
50  {
51  global $DIC;
52 
53  $directory = CLIENT_DATA_DIR . '/temp';
54  $temp = tempnam($directory, 'zip');
55  $zip = new \ZipArchive();
56  $zip->open($temp, \ZipArchive::CREATE | \ZipArchive::OVERWRITE);
57 
58  foreach ($this->resources as $resource) {
59  $revision = $this->stream_access->populateRevision($resource->getCurrentRevision());
60  if ($this->use_revision_titles) {
61  $file_name = $this->file_name_policy->prepareFileNameForConsumer($revision->getTitle());
62  } else {
63  $file_name = $this->file_name_policy->prepareFileNameForConsumer(
64  $revision->getInformation()->getTitle()
65  );
66  }
67 
68  $zip->addFile($revision->maybeStreamResolver()->getStream()->getMetadata('uri'), $file_name);
69  }
70 
71  $zip->close();
72  $response = $DIC->http()->response();
74  $response = $response->withHeader(ResponseHeader::CONNECTION, 'close');
75  $response = $response->withHeader(ResponseHeader::ACCEPT_RANGES, 'bytes');
76  $response = $response->withHeader(
78  'attachment'
79  . '; filename="'
80  . $this->zip_file_name
81  . '"'
82  );
83  $fopen = fopen($temp, 'rb');
84  $response = $response->withBody(Streams::ofResource($fopen));
85  $DIC->http()->saveResponse($response);
86  $DIC->http()->sendResponse();
87  fclose($fopen);
88  unlink($temp);
89  $DIC->http()->close();
90  }
91 
92 
93  public function useRevisionTitlesForFileNames(bool $use_revision_titles): self
94  {
95  $this->use_revision_titles = $use_revision_titles;
96  return $this;
97  }
98 
99  public function setRevisionNumber(int $revision_number): DeliveryConsumer
100  {
101  $this->revision_number = $revision_number;
102  return $this;
103  }
104 
105  public function overrideFileName(string $file_name): DeliveryConsumer
106  {
107  $this->zip_file_name = $file_name;
108  return $this;
109  }
110 }
$resources
Definition: ltiservices.php:65
$response
Definition: xapitoken.php:93
__construct(protected array $resources, private StreamAccess $stream_access, protected FileNamePolicy $file_name_policy, protected string $zip_file_name)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
const CLIENT_DATA_DIR
Definition: constants.php:46
static ofResource($resource)
Wraps an already created resource with the stream abstraction.
Definition: Streams.php:64
global $DIC
Definition: shib_login.php:22