Passed
Push — master ( ec0d02...c0f595 )
by Jan
06:31 queued 10s
created

LabelResponse::setAutoEtag()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 5
rs 10
1
<?php
2
/**
3
 * This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
4
 *
5
 * Copyright (C) 2019 - 2020 Jan Böhmer (https://github.com/jbtronics)
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Affero General Public License as published
9
 * by the Free Software Foundation, either version 3 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License
18
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19
 */
20
21
namespace App\Helpers;
22
23
24
use Symfony\Component\HttpFoundation\BinaryFileResponse;
25
use Symfony\Component\HttpFoundation\File\File;
26
use Symfony\Component\HttpFoundation\Request;
27
use Symfony\Component\HttpFoundation\Response;
28
29
class LabelResponse extends Response
30
{
31
    public function __construct($content = '', int $status = 200, array $headers = [])
32
    {
33
        parent::__construct($content, $status, $headers);
34
    }
35
36
    public function setContent($content)
37
    {
38
        parent::setContent($content);
39
40
        $this->setAutoEtag();
41
        $this->setAutoLastModified();
42
        return $this;
43
    }
44
45
    public function prepare(Request $request)
46
    {
47
        parent::prepare($request);
48
49
        $this->headers->set('Content-Type','application/pdf');
50
51
52
        if ('HTTP/1.0' !== $request->server->get('SERVER_PROTOCOL')) {
53
            $this->setProtocolVersion('1.1');
54
        }
55
56
        $this->ensureIEOverSSLCompatibility($request);
57
58
        return $this;
59
    }
60
61
    /**
62
     * Automatically sets the Last-Modified header according the file modification date.
63
     */
64
    public function setAutoLastModified()
65
    {
66
        $this->setLastModified(new \DateTime());
67
68
        return $this;
69
    }
70
71
    /**
72
     * Automatically sets the ETag header according to the checksum of the file.
73
     */
74
    public function setAutoEtag()
75
    {
76
        $this->setEtag(base64_encode(hash('sha256', $this->content, true)));
77
78
        return $this;
79
    }
80
81
    /**
82
     * Sets the Content-Disposition header with the given filename.
83
     *
84
     * @param string $disposition      ResponseHeaderBag::DISPOSITION_INLINE or ResponseHeaderBag::DISPOSITION_ATTACHMENT
85
     * @param string $filename         Optionally use this UTF-8 encoded filename instead of the real name of the file
86
     * @param string $filenameFallback A fallback filename, containing only ASCII characters. Defaults to an automatically encoded filename
87
     *
88
     * @return $this
89
     */
90
    public function setContentDisposition($disposition, $filename, $filenameFallback = '')
91
    {
92
        if ('' === $filenameFallback && (!preg_match('/^[\x20-\x7e]*$/', $filename) || false !== strpos($filename, '%'))) {
93
            $encoding = mb_detect_encoding($filename, null, true) ?: '8bit';
94
95
            for ($i = 0, $filenameLength = mb_strlen($filename, $encoding); $i < $filenameLength; ++$i) {
96
                $char = mb_substr($filename, $i, 1, $encoding);
97
98
                if ('%' === $char || \ord($char) < 32 || \ord($char) > 126) {
99
                    $filenameFallback .= '_';
100
                } else {
101
                    $filenameFallback .= $char;
102
                }
103
            }
104
        }
105
106
        $dispositionHeader = $this->headers->makeDisposition($disposition, $filename, $filenameFallback);
107
        $this->headers->set('Content-Disposition', $dispositionHeader);
108
109
        return $this;
110
    }
111
112
}