Skip to content

Commit 542acfa

Browse files
authored
Merge pull request #40 from WyriHaximus-labs/copy-fix
Reworked file copying to use pipe
2 parents 00409a5 + 1264b22 commit 542acfa

File tree

2 files changed

+14
-33
lines changed

2 files changed

+14
-33
lines changed

src/Node/File.php

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -206,32 +206,21 @@ protected function copyToFile(FileInterface $node)
206206
{
207207
$stream = new ObjectStream();
208208

209-
$this->open('r')->then(function (ReadableStreamInterface $readStream) use ($node, $stream) {
209+
$this->open('r')->then(function (ReadableStreamInterface $readStream) use ($node) {
210210
$readStream->pause();
211-
return $node->open('ctw')->then(function (WritableStreamInterface $writeStream) use ($readStream, $node, $stream) {
212-
$deferred = new Deferred();
213-
$writePromises = [];
214-
$readStream->on('end', function () use ($deferred, $writeStream, &$writePromises) {
215-
\React\Promise\all($writePromises)->then(function ()use ($deferred, $writeStream) {
216-
$writeStream->end();
217-
$deferred->resolve();
218-
});
219-
});
220-
$readStream->on('data', function ($data) use ($writeStream, &$writePromises) {
221-
$writePromises[] = $writeStream->write($data);
222-
});
223-
$readStream->resume();
224-
return $deferred->promise();
225-
})->always(function () use ($node) {
226-
$node->close();
211+
212+
return \React\Promise\all([
213+
'read' => $readStream,
214+
'write' => $node->open('ctw'),
215+
]);
216+
})->then(function (array $streams) use ($stream, $node) {
217+
$streams['read']->pipe($streams['write']);
218+
$streams['read']->on('close', function () use ($streams, $stream, $node) {
219+
$streams['write']->close();
220+
$stream->end($node);
227221
});
228-
})->then(function () {
229-
return $this->close();
230-
}, function () {
231-
return $this->close();
232-
})->then(function () use ($stream, $node) {
233-
$stream->end($node);
234-
});
222+
$streams['read']->resume();
223+
})->done();
235224

236225
return $stream;
237226
}

tests/Adapters/FileTest.php

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -187,15 +187,7 @@ public function testCopyToDirectory(LoopInterface $loop, FilesystemInterface $fi
187187
} while (!file_exists($tempFileSource) && !file_exists($tempFileDestination));
188188
$this->assertFileExists($tempFileSource);
189189
$this->assertSame($contents, file_get_contents($tempFileSource));
190-
$promise = $filesystem->file($tempFileSource)->copy($filesystem->dir($tempFileDestination));
191-
$timer = $loop->addTimer(self::TIMEOUT, function () use ($loop) {
192-
$loop->stop();
193-
$this->fail('Event loop timeout');
194-
});
195-
$promise->always(function () use ($loop, $timer) {
196-
$loop->cancelTimer($timer);
197-
});
198-
$this->await($promise, $loop);
190+
$this->await($filesystem->file($tempFileSource)->copy($filesystem->dir($tempFileDestination)), $loop);
199191
do {
200192
usleep(500);
201193
$this->checkIfTimedOut();

0 commit comments

Comments
 (0)