-
-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathFile.php
More file actions
228 lines (199 loc) · 5.56 KB
/
File.php
File metadata and controls
228 lines (199 loc) · 5.56 KB
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
<?php
namespace React\Filesystem\Node;
use Exception;
use React\Filesystem\AdapterInterface;
use React\Filesystem\FilesystemInterface;
use React\Filesystem\ObjectStream;
use React\Filesystem\ObjectStreamSink;
use React\Filesystem\Stream\StreamFactory;
use React\Promise\Stream;
use React\Stream\ReadableStreamInterface;
use React\Stream\WritableStreamInterface;
class File implements FileInterface
{
use GenericOperationTrait;
/**
* @var bool
*/
protected $open = false;
protected $fileDescriptor;
/**
* @var FilesystemInterface
*/
protected $filesystem;
/**
* @var AdapterInterface
*/
protected $adapter;
/**
* @param string $filename
* @param FilesystemInterface $filesystem
*/
public function __construct($filename, FilesystemInterface $filesystem)
{
$this->filesystem = $filesystem;
$this->adapter = $filesystem->getAdapter();
$this->createNameNParentFromFilename($filename);
}
/**
* {@inheritDoc}
*/
public function exists()
{
return $this->stat()->then(function () {
return null;
}, function () {
throw new Exception('Not found');
});
}
/**
* {@inheritDoc}
*/
public function size()
{
return $this->adapter->stat($this->path)->then(function ($result) {
return $result['size'];
});
}
/**
* {@inheritDoc}
*/
public function time()
{
return $this->adapter->stat($this->path)->then(function ($result) {
return [
'atime' => $result['atime'],
'ctime' => $result['ctime'],
'mtime' => $result['mtime'],
];
});
}
/**
* {@inheritDoc}
*/
public function rename($toFilename)
{
return $this->adapter->rename($this->path, $toFilename)->then(function () use ($toFilename) {
return $this->filesystem->file($toFilename);
});
}
/**
* {@inheritDoc}
*/
public function create($mode = AdapterInterface::CREATION_MODE, $time = null)
{
return $this->stat()->then(function () {
throw new \Exception('File exists already');
}, function () use ($mode, $time) {
return $this->adapter->touch($this->path, $mode, $time);
});
}
/**
* {@inheritDoc}
*/
public function touch($mode = AdapterInterface::CREATION_MODE, $time = null)
{
return $this->adapter->touch($this->path, $mode, $time);
}
/**
* {@inheritDoc}
*/
public function open($flags, $mode = AdapterInterface::CREATION_MODE)
{
if ($this->open === true) {
return \React\Promise\reject(new Exception('File is already open'));
}
return $this->adapter->open($this->path, $flags, $mode)->then(function ($fd) use ($flags) {
$this->open = true;
$this->fileDescriptor = $fd;
return StreamFactory::create($this->path, $fd, $flags, $this->adapter);
});
}
/**
* {@inheritDoc}
*/
public function close()
{
if ($this->open === false) {
return \React\Promise\reject(new Exception('File is already closed'));
}
return $this->adapter->close($this->fileDescriptor)->then(function () {
$this->open = false;
$this->fileDescriptor = null;
});
}
/**
* {@inheritDoc}
*/
public function getContents()
{
return $this->adapter->getContents($this->path);
}
/**
* {@inheritDoc}
*/
public function putContents($contents)
{
return $this->adapter->putContents($this->path, $contents);
}
/**
* {@inheritDoc}
*/
public function remove()
{
return $this->adapter->unlink($this->path);
}
/**
* @param NodeInterface $node
* @return \React\Promise\PromiseInterface
*/
public function copy(NodeInterface $node)
{
return ObjectStreamSink::promise($this->copyStreaming($node));
}
/**
* @param NodeInterface $node
* @return ObjectStream
*/
public function copyStreaming(NodeInterface $node)
{
if ($node instanceof FileInterface) {
return $this->copyToFile($node);
}
if ($node instanceof DirectoryInterface) {
return $this->copyToDirectory($node);
}
throw new \UnexpectedValueException('Unsupported node type');
}
/**
* @param FileInterface $node
* @return ObjectStream
*/
protected function copyToFile(FileInterface $node)
{
$stream = new ObjectStream();
$this->open('r')->then(function (ReadableStreamInterface $readStream) use ($node) {
$readStream->pause();
return \React\Promise\all([
'read' => $readStream,
'write' => $node->open('ctw'),
]);
})->then(function (array $streams) use ($stream, $node) {
$streams['read']->pipe($streams['write']);
$streams['read']->on('close', function () use ($streams, $stream, $node) {
$streams['write']->close();
$stream->end($node);
});
$streams['read']->resume();
})->done();
return $stream;
}
/**
* @param DirectoryInterface $node
* @return ObjectStream
*/
protected function copyToDirectory(DirectoryInterface $node)
{
return $this->copyToFile($node->getFilesystem()->file($node->getPath() . $this->getName()));
}
}