ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
DownloadMultipleConsumer.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
30
36{
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');
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
94 {
95 $this->use_revision_titles = $use_revision_titles;
96 return $this;
97 }
98
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}
Mime type determination.
Definition: MimeType.php:30
Stream factory which enables the user to create streams without the knowledge of the concrete class.
Definition: Streams.php:32
static ofResource($resource)
Wraps an already created resource with the stream abstraction.
Definition: Streams.php:64
__construct(protected array $resources, private StreamAccess $stream_access, protected FileNamePolicy $file_name_policy, protected string $zip_file_name)
const CLIENT_DATA_DIR
Definition: constants.php:46
Interface ResponseHeader.
$resources
Definition: ltiservices.php:68
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
global $DIC
Definition: shib_login.php:26
$response
Definition: xapitoken.php:93