1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Spiral Framework, SpiralScout LLC. |
4
|
|
|
* |
5
|
|
|
* @package spiralFramework |
6
|
|
|
* @author Anton Titov (Wolfy-J) |
7
|
|
|
* @copyright ©2009-2011 |
8
|
|
|
*/ |
9
|
|
|
namespace Spiral\Storage; |
10
|
|
|
|
11
|
|
|
use Psr\Http\Message\StreamInterface; |
12
|
|
|
use Psr\Http\Message\UploadedFileInterface; |
13
|
|
|
use Spiral\Core\Component; |
14
|
|
|
use Spiral\Files\FilesInterface; |
15
|
|
|
use Spiral\Files\Streams\StreamableInterface; |
16
|
|
|
use Spiral\Files\Streams\StreamWrapper; |
17
|
|
|
use Spiral\Storage\Exceptions\ServerException; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* AbstractServer implementation with different naming. |
21
|
|
|
*/ |
22
|
|
|
abstract class StorageServer extends Component implements ServerInterface |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* Default mimetype to be used when nothing else can be applied. |
26
|
|
|
*/ |
27
|
|
|
const DEFAULT_MIMETYPE = 'application/octet-stream'; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
protected $options = []; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var FilesInterface |
36
|
|
|
*/ |
37
|
|
|
protected $files = null; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param FilesInterface $files Required for local filesystem operations. |
41
|
|
|
* @param array $options Server specific options. |
42
|
|
|
*/ |
43
|
|
|
public function __construct(FilesInterface $files, array $options) |
44
|
|
|
{ |
45
|
|
|
$this->options = $options + $this->options; |
46
|
|
|
$this->files = $files; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* {@inheritdoc} |
51
|
|
|
*/ |
52
|
|
|
public function allocateFilename(BucketInterface $bucket, $name) |
53
|
|
|
{ |
54
|
|
|
if (empty($stream = $this->allocateStream($bucket, $name))) { |
55
|
|
|
throw new ServerException("Unable to allocate local filename for '{$name}'."); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
//Default implementation will use stream to create temporary filename, such filename |
59
|
|
|
//can't be used outside php scope |
60
|
|
|
return StreamWrapper::getUri($stream); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* {@inheritdoc} |
65
|
|
|
*/ |
66
|
|
|
public function copy(BucketInterface $bucket, BucketInterface $destination, $name) |
67
|
|
|
{ |
68
|
|
|
return $this->put($destination, $name, $this->allocateStream($bucket, $name)); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* {@inheritdoc} |
73
|
|
|
*/ |
74
|
|
|
public function replace(BucketInterface $bucket, BucketInterface $destination, $name) |
75
|
|
|
{ |
76
|
|
|
if ($this->copy($bucket, $destination, $name)) { |
77
|
|
|
$this->delete($bucket, $name); |
78
|
|
|
|
79
|
|
|
return true; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
throw new ServerException("Unable to copy '{$name}' to new bucket."); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Cast local filename to be used in file based methods and etc. |
87
|
|
|
* |
88
|
|
|
* @param string|StreamInterface $source |
89
|
|
|
* @return string |
90
|
|
|
*/ |
91
|
|
|
protected function castFilename($source) |
92
|
|
|
{ |
93
|
|
|
if (empty($source)) { |
94
|
|
|
return StreamWrapper::getUri(\GuzzleHttp\Psr7\stream_for('')); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
if (is_string($source)) { |
98
|
|
|
if (!$this->files->exists($source)) { |
99
|
|
|
//This code is going to use additional abstraction layer to connect storage and guzzle |
100
|
|
|
return StreamWrapper::getUri(\GuzzleHttp\Psr7\stream_for($source)); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return $source; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
if ($source instanceof UploadedFileInterface || $source instanceof StreamableInterface) { |
107
|
|
|
$source = $source->getStream(); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
if ($source instanceof StreamInterface) { |
111
|
|
|
return StreamWrapper::getUri($source); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
throw new ServerException("Unable to get filename for non Stream instance."); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Cast stream associated with origin data. |
119
|
|
|
* |
120
|
|
|
* @param string|StreamInterface $source |
121
|
|
|
* @return StreamInterface |
122
|
|
|
*/ |
123
|
|
|
protected function castStream($source) |
124
|
|
|
{ |
125
|
|
|
if ($source instanceof UploadedFileInterface || $source instanceof StreamableInterface) { |
126
|
|
|
$source = $source->getStream(); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
if ($source instanceof StreamInterface) { |
130
|
|
|
return $source; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
if (empty($source)) { |
134
|
|
|
//This code is going to use additional abstraction layer to connect storage and guzzle |
135
|
|
|
return \GuzzleHttp\Psr7\stream_for(''); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
return \GuzzleHttp\Psr7\stream_for($source); |
139
|
|
|
} |
140
|
|
|
} |