Passed
Pull Request — master (#52)
by Daniel
06:09
created

Base64EncodedFile::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
cc 1
nc 1
nop 3
crap 2
1
<?php
2
3
/*
4
 * This file is part of the Silverback API Component Bundle Project
5
 *
6
 * (c) Daniel West <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Silverback\ApiComponentBundle\Model\Uploadable;
15
16
use Symfony\Component\HttpFoundation\File\Exception\FileException;
17
use Symfony\Component\HttpFoundation\File\File;
18
use Symfony\Component\Mime\MimeTypes;
19
20
/**
21
 * Source: https://github.com/hshn/base64-encoded-file/blob/master/src/HttpFoundation/File/Base64EncodedFile.php.
22
 *
23
 * @author Shota Hoshino <[email protected]>
24
 */
25
class Base64EncodedFile extends File
26
{
27
    /**
28
     * @param string $encoded
29
     * @param bool   $strict
30
     * @param bool   $checkPath
31
     */
32
    public function __construct($encoded, $strict = true, $checkPath = true)
33
    {
34
        parent::__construct($this->restoreToTemporary($encoded, $strict), $checkPath);
35
    }
36
37
    /**
38
     * @param string $encoded
39
     * @param bool   $strict
40
     *
41
     * @throws FileException
42
     */
43
    private function restoreToTemporary($encoded, $strict = true): string
44
    {
45
        if (0 === strpos($encoded, 'data:')) {
46
            if (0 !== strpos($encoded, 'data://')) {
47
                $encoded = substr_replace($encoded, 'data://', 0, 5);
48
            }
49
50
            $source = @fopen($encoded, 'r');
51
            if (false === $source) {
52
                throw new FileException('Unable to decode strings as base64');
53
            }
54
55
            $meta = stream_get_meta_data($source);
56
57
            if ($strict) {
58
                if (!isset($meta['base64']) || true !== $meta['base64']) {
59
                    throw new FileException('Unable to decode strings as base64');
60
                }
61
            }
62
63
            if (false === $path = tempnam($directory = sys_get_temp_dir(), 'Base64EncodedFile')) {
64
                throw new FileException(sprintf('Unable to create a file into the "%s" directory', $path));
65
            }
66
67
            if (null !== $extension = (MimeTypes::getDefault()->getExtensions($meta['mediatype'])[0] ?? null)) {
68
                $path .= '.' . $extension;
69
            }
70
71
            if (false === $target = @fopen($path, 'wb+')) {
72
                throw new FileException(sprintf('Unable to write the file "%s"', $path));
73
            }
74
75
            if (false === @stream_copy_to_stream($source, $target)) {
76
                throw new FileException(sprintf('Unable to write the file "%s"', $path));
77
            }
78
79
            if (false === @fclose($target)) {
80
                throw new FileException(sprintf('Unable to write the file "%s"', $path));
81
            }
82
83
            if (false === @fclose($source)) {
84
                throw new FileException(sprintf('Unable to close data stream'));
85
            }
86
87
            return $path;
88
        }
89
90
        if (false === $decoded = base64_decode($encoded, $strict)) {
91
            throw new FileException('Unable to decode strings as base64');
92
        }
93
94
        if (false === $path = tempnam($directory = sys_get_temp_dir(), 'Base64EncodedFile')) {
95
            throw new FileException(sprintf('Unable to create a file into the "%s" directory', $directory));
96
        }
97
98
        if (false === file_put_contents($path, $decoded, FILE_BINARY)) {
99
            throw new FileException(sprintf('Unable to write the file "%s"', $path));
100
        }
101
102
        return $path;
103
    }
104
}
105