|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Puzzle\AMQP\Messages\Bodies; |
|
4
|
|
|
|
|
5
|
|
|
use Puzzle\AMQP\Messages\Body; |
|
6
|
|
|
use Puzzle\AMQP\Messages\Chunks\Chunk; |
|
7
|
|
|
use Puzzle\AMQP\Messages\Chunks\ChunkedMessageMetadata; |
|
8
|
|
|
use Puzzle\ValueObjects\Uuid; |
|
9
|
|
|
use Puzzle\AMQP\Messages\ContentType; |
|
10
|
|
|
use Puzzle\AMQP\Messages\Chunks\ChunkSize; |
|
11
|
|
|
|
|
12
|
|
|
class StreamedFile implements Body |
|
13
|
|
|
{ |
|
14
|
|
|
private |
|
15
|
|
|
$filepath, |
|
16
|
|
|
$chunkSize, |
|
17
|
|
|
$metadata; |
|
18
|
|
|
|
|
19
|
|
|
public function __construct($filepath, ChunkSize $chunkSize = null) |
|
20
|
|
|
{ |
|
21
|
|
|
$this->ensureFilepathIsValid($filepath); |
|
22
|
|
|
|
|
23
|
|
|
$this->filepath = $filepath; |
|
24
|
|
|
$this->chunkSize = $chunkSize; |
|
25
|
|
|
|
|
26
|
|
|
$size = filesize($filepath); |
|
27
|
|
|
$nbChunks = (int) ceil($size / $chunkSize->toBytes()); |
|
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
$this->metadata = new ChunkedMessageMetadata(new Uuid(), $size, $nbChunks, sha1_file($filepath)); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
private function ensureFilepathIsValid($filepath) |
|
33
|
|
|
{ |
|
34
|
|
|
if(is_file($filepath) === false || is_readable($filepath) === false) |
|
35
|
|
|
{ |
|
36
|
|
|
throw new \InvalidArgumentException("Cannot read $filepath"); |
|
37
|
|
|
} |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function inOriginalFormat() |
|
41
|
|
|
{ |
|
42
|
|
|
return file_get_contents($this->filepath); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @return \Generator|string |
|
|
|
|
|
|
47
|
|
|
*/ |
|
48
|
|
|
public function asTransported() |
|
49
|
|
|
{ |
|
50
|
|
|
if($this->isChunked() === false) |
|
51
|
|
|
{ |
|
52
|
|
|
return $this->inOriginalFormat(); |
|
|
|
|
|
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
$offset = 0; |
|
56
|
|
|
$playhead = 0; |
|
57
|
|
|
|
|
58
|
|
|
$stream = fopen($this->filepath, 'r'); |
|
59
|
|
|
|
|
60
|
|
View Code Duplication |
while(! feof($stream)) |
|
|
|
|
|
|
61
|
|
|
{ |
|
62
|
|
|
$content = fread($stream, $this->chunkSize->toBytes()); |
|
63
|
|
|
$playhead++; |
|
64
|
|
|
|
|
65
|
|
|
$chunk = new Chunk($playhead, $offset, $content, $this->metadata); |
|
66
|
|
|
yield $chunk; |
|
67
|
|
|
|
|
68
|
|
|
$offset += $chunk->size(); |
|
69
|
|
|
unset($chunk, $content); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
fclose($stream); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public function getContentType() |
|
76
|
|
|
{ |
|
77
|
|
|
return ContentType::BINARY; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function __toString() |
|
81
|
|
|
{ |
|
82
|
|
|
return sprintf( |
|
83
|
|
|
'<binary stream of %d bytes>', |
|
84
|
|
|
filesize($this->filepath) |
|
85
|
|
|
); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function isChunked() |
|
89
|
|
|
{ |
|
90
|
|
|
return $this->chunkSize instanceof ChunkSize; |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: