1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Shopware\Storefront\Controller; |
4
|
|
|
|
5
|
|
|
use Shopware\Core\Checkout\Document\Exception\InvalidDocumentException; |
6
|
|
|
use Shopware\Core\Framework\Routing\Annotation\RouteScope; |
7
|
|
|
use Shopware\Core\System\SalesChannel\SalesChannelContext; |
8
|
|
|
use Shopware\Storefront\Page\Account\Document\DocumentPageLoader; |
9
|
|
|
use Symfony\Component\HttpFoundation\HeaderUtils; |
10
|
|
|
use Symfony\Component\HttpFoundation\Request; |
11
|
|
|
use Symfony\Component\HttpFoundation\Response; |
12
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @RouteScope(scopes={"storefront"}) |
16
|
|
|
*/ |
17
|
|
|
class DocumentController extends StorefrontController |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var DocumentPageLoader |
21
|
|
|
*/ |
22
|
|
|
protected $documentPageLoader; |
23
|
|
|
|
24
|
|
|
public function __construct(DocumentPageLoader $documentPageLoader) |
25
|
|
|
{ |
26
|
|
|
$this->documentPageLoader = $documentPageLoader; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @Route("/account/order/document/{documentId}/{deepLinkCode}", name="frontend.account.order.single.document", methods={"GET"}) |
31
|
|
|
* |
32
|
|
|
* @internal (flag:FEATURE_NEXT_10537) |
33
|
|
|
* |
34
|
|
|
* @throws InvalidDocumentException |
35
|
|
|
*/ |
36
|
|
|
public function downloadDocument(Request $request, SalesChannelContext $context): Response |
37
|
|
|
{ |
38
|
|
|
$download = $request->query->getBoolean('download', false); |
39
|
|
|
|
40
|
|
|
$documentPage = $this->documentPageLoader->load($request, $context); |
41
|
|
|
|
42
|
|
|
$generatedDocument = $documentPage->getDocument(); |
43
|
|
|
|
44
|
|
|
return $this->createResponse( |
45
|
|
|
$generatedDocument->getFilename(), |
46
|
|
|
$generatedDocument->getFileBlob(), |
47
|
|
|
$download, |
48
|
|
|
$generatedDocument->getContentType() |
49
|
|
|
); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
private function createResponse(string $filename, string $content, bool $forceDownload, string $contentType): Response |
53
|
|
|
{ |
54
|
|
|
$response = new Response($content); |
55
|
|
|
|
56
|
|
|
$disposition = HeaderUtils::makeDisposition( |
57
|
|
|
$forceDownload ? HeaderUtils::DISPOSITION_ATTACHMENT : HeaderUtils::DISPOSITION_INLINE, |
58
|
|
|
$filename, |
59
|
|
|
// only printable ascii |
60
|
|
|
preg_replace('/[\x00-\x1F\x7F-\xFF]/', '_', $filename) ?? '' |
61
|
|
|
); |
62
|
|
|
|
63
|
|
|
$response->headers->set('Content-Type', $contentType); |
64
|
|
|
$response->headers->set('Content-Disposition', $disposition); |
65
|
|
|
|
66
|
|
|
return $response; |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|