-
Notifications
You must be signed in to change notification settings - Fork 7
/
Base64FileTrait.php
167 lines (136 loc) · 3.78 KB
/
Base64FileTrait.php
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
<?php
/*
* This file is part of the Ivory Base64 File package.
*
* (c) Eric GELOEN <geloen.eric@gmail.com>
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code.
*/
namespace Ivory\Base64FileBundle\Model;
/**
* @author GeLo <geloen.eric@gmail.com>
*/
trait Base64FileTrait
{
/**
* @var string
*/
private $path;
/**
* @var resource
*/
private $resource;
/**
* {@inheritdoc}
*/
public function __destruct()
{
if (is_resource($this->resource)) {
fclose($this->resource);
}
if (file_exists($this->path)) {
unlink($this->path);
}
}
/**
* @param bool $encoded
* @param bool $asResource
*
* @return resource|string
*/
public function getData($encoded = true, $asResource = true)
{
$resource = fopen('php://temp', 'rb+');
if ($encoded) {
$filter = stream_filter_append($resource, 'convert.base64-encode', STREAM_FILTER_WRITE);
}
$this->copyStreamToStream($this->resource, $resource);
if (isset($filter)) {
stream_filter_remove($filter);
}
if ($asResource) {
return $resource;
}
$content = stream_get_contents($resource);
fclose($resource);
return $content;
}
/**
* @param string|resource $value
* @param bool $encoded
*
* @return string
*/
private function load($value, $encoded = true)
{
$this->path = tempnam(sys_get_temp_dir(), 'ivory_base64');
$this->resource = fopen($this->path, 'w+');
if ($encoded) {
$filter = stream_filter_append($this->resource, 'convert.base64-decode', STREAM_FILTER_WRITE);
}
try {
if (is_string($value)) {
$this->copyStringToStream($value, $this->resource);
} elseif (is_resource($value)) {
$this->copyStreamToStream($value, $this->resource);
} else {
throw new \InvalidArgumentException(sprintf(
'The base64 file value must be a string or a resource, got "%s".',
gettype($value)
));
}
} catch (\Exception $e) {
fclose($this->resource);
throw $e;
}
if (isset($filter)) {
stream_filter_remove($filter);
}
fflush($this->resource);
return $this->path;
}
/**
* @param string $from
* @param resource $to
*/
private function copyStringToStream($from, $to)
{
$toPosition = ftell($to);
$success = @fwrite($to, $from);
fseek($to, $toPosition);
if (!$success) {
$error = error_get_last();
throw new \RuntimeException(sprintf(
'An error occurred while copying the value (%s).',
$error['message']
));
}
}
/**
* @param resource $from
* @param resource $to
*/
private function copyStreamToStream($from, $to)
{
$metadata = stream_get_meta_data($from);
$seekable = $metadata['seekable'];
$toPosition = ftell($to);
if ($seekable) {
$fromPosition = ftell($from);
rewind($from);
}
$success = @stream_copy_to_stream($from, $to);
if (isset($fromPosition)) {
fseek($from, $fromPosition);
}
fseek($to, $toPosition);
if (!$success) {
$error = error_get_last();
throw new \RuntimeException(sprintf(
'An error occurred while copying the value (%s).',
$error['message']
));
}
}
}