-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathDownloadService.php
More file actions
207 lines (176 loc) · 7.22 KB
/
DownloadService.php
File metadata and controls
207 lines (176 loc) · 7.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<?php
/**
* webtrees: online genealogy
* Copyright (C) 2025 webtrees development team
* <http://webtrees.net>
*
* RepositoryHierarchy (webtrees custom module):
* Copyright (C) 2025 Markus Hemprich
* <http://www.familienforschung-hemprich.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace Jefferson49\Webtrees\Module\RepositoryHierarchy;
use DOMDocument;
use Fisharebest\Webtrees\Encodings\UTF8;
use Fisharebest\Webtrees\I18N;
use Fisharebest\Webtrees\Report\PdfRenderer;
use Nyholm\Psr7\Factory\Psr17Factory;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface;
use RuntimeException;
/**
* Download Service
*/
class DownloadService
{
//Download and EAD XML types
public const DOWNLOAD_OPTION_EAD_XML = 'download_option_ead_xml';
public const DOWNLOAD_OPTION_APE_EAD = 'download_option_ape_ead';
public const DOWNLOAD_OPTION_DDB_EAD = 'download_option_ddb_ead';
public const DOWNLOAD_OPTION_ATOM = 'download_option_atom';
public const DOWNLOAD_OPTION_HTML = 'download_option_html';
public const DOWNLOAD_OPTION_PDF = 'download_option_pdf';
public const DOWNLOAD_OPTION_TEXT = 'download_option_text';
public const DOWNLOAD_OPTION_XML = 'download_option_xml';
public const DOWNLOAD_OPTION_ALL = 'download_option_all';
/**
* Options for downloads
*
* @param string $selection
*
* @return array<string>
*/
public static function getDownloadOptions(string $selection = self::DOWNLOAD_OPTION_ALL): array
{
$xml_options = [
self::DOWNLOAD_OPTION_EAD_XML => I18N::translate('EAD XML'),
//self::DOWNLOAD_OPTION_APE_EAD => I18N::translate('apeEAD XML'),
//self::DOWNLOAD_OPTION_DDB_EAD => I18N::translate('DDB EAD XML'),
//self::DOWNLOAD_OPTION_ATOM => I18N::translate('AtoM EAD XML'),
];
$text_options = [
self::DOWNLOAD_OPTION_HTML => I18N::translate('Finding aid as HTML'),
self::DOWNLOAD_OPTION_PDF => I18N::translate('Finding aid as PDF'),
];
switch($selection) {
case self::DOWNLOAD_OPTION_XML:
$options = $xml_options;
break;
case self::DOWNLOAD_OPTION_TEXT:
$options = $text_options;
break;
case self::DOWNLOAD_OPTION_ALL:
$options = $text_options + $xml_options;
break;
default:
$options = $text_options + $xml_options;
}
return $options;
}
/**
* Is XML download command
*
* @param string $command
*
* @return bool
*/
public static function isXmlDownloadCommand(string $command): bool
{
$xml_options = self::getDownloadOptions(self::DOWNLOAD_OPTION_XML);
return array_key_exists($command, $xml_options);
}
/**
* Return response to download a file from a DOM document
*
* @param DOMDocument $dom DOM object
* @param string $filename Name of download file without extension
*
* @return ResponseInterface
*/
public static function responseForDOMDownload(DOMDocument $dom, string $filename): ResponseInterface
{
$resource = Functions::export($dom);
$stream_factory = new Psr17Factory();
$response_factory = Functions::getFromContainer(ResponseFactoryInterface::class);
$stream = $stream_factory->createStreamFromResource($resource);
return $response_factory->createResponse()
->withBody($stream)
->withHeader('content-type', 'text/xml; charset=' . UTF8::NAME)
->withHeader('content-disposition', 'attachment; filename="' . addcslashes($filename, '"') . '.xml"');
}
/**
* Return response to download a HTML file
*
* @param string $html HTML text
* @param string $filename Name of download file without extension
*
* @return ResponseInterface
*/
public function responseForHtmlDownload(string $html, string $filename): ResponseInterface
{
$stream = fopen('php://memory', 'wb+');
if ($stream === false) {
throw new RuntimeException('Failed to create temporary stream');
}
//Write html to stream
$bytes_written = fwrite($stream, $html);
if ($bytes_written !== strlen($html)) {
throw new RuntimeException('Unable to write to stream. Perhaps the disk is full?');
}
if (rewind($stream) === false) {
throw new RuntimeException('Cannot rewind temporary stream');
}
//Prepare and return the response
$resource = $stream;
$stream_factory = new Psr17Factory();
$response_factory = Functions::getFromContainer(ResponseFactoryInterface::class);
$stream = $stream_factory->createStreamFromResource($resource);
return $response_factory->createResponse()
->withBody($stream)
->withHeader('content-type', 'text/html; charset=' . UTF8::NAME)
->withHeader('content-disposition', 'attachment; filename="' . addcslashes($filename, '"') . '.html"');
}
/**
* Return response to download a PDF file
*
* @param PdfRenderer $pdf
* @param string $filename Name of download file without extension
*
* @return ResponseInterface
*/
public function responseForPdfDownload(PdfRenderer $pdf, string $filename): ResponseInterface
{
$stream = fopen('php://memory', 'wb+');
if ($stream === false) {
throw new RuntimeException('Failed to create temporary stream');
}
//Write pdf to stream
$bytes_written = fwrite($stream, $pdf->tcpdf->Output('doc.pdf', 'S'));
if ($bytes_written !== strlen($pdf->tcpdf->Output('doc.pdf', 'S'))) {
throw new RuntimeException('Unable to write to stream. Perhaps the disk is full?');
}
if (rewind($stream) === false) {
throw new RuntimeException('Cannot rewind temporary stream');
}
//Prepare and return the response
$resource = $stream;
$stream_factory = new Psr17Factory();
$response_factory = Functions::getFromContainer(ResponseFactoryInterface::class);
$stream = $stream_factory->createStreamFromResource($resource);
return $response_factory->createResponse()
->withBody($stream)
->withHeader('content-type', 'application/pdf; charset=' . UTF8::NAME)
->withHeader('content-disposition', 'attachment; filename="' . addcslashes($filename, '"') . '.pdf"');
}
}