From c3c93c05fe5ddf105bccc098082e8841dc8d52f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Guillaume=20Perr=C3=A9al?= <guillaume.perreal@irstea.fr> Date: Fri, 23 Jan 2015 14:50:16 +0100 Subject: [PATCH] =?UTF-8?q?UploadedFile:=20correction=20des=20m=C3=A9thode?= =?UTF-8?q?s=20copyFrom=20et=20copyTo.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Entity/UploadedFile.php | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/Entity/UploadedFile.php b/Entity/UploadedFile.php index c17d8393..e8300cfc 100644 --- a/Entity/UploadedFile.php +++ b/Entity/UploadedFile.php @@ -384,20 +384,24 @@ class UploadedFile * @param int $writeOffset * @return int */ - public function copyFrom($source, $maxlen = PHP_INT_MAX, $writeOffset = 0) + public function copyFrom($source, $maxlen = -1, $writeOffset = 0) { if($maxlen === 0) { return 0; } - $stream = $this->filesystem->createStream($this->getPath(), new StreamMode('cb')); + $stream = $this->filesystem->createStream($this->getPath()); + $stream->open(new StreamMode('cb')); $stream->seek($writeOffset); - if(false !== $fileHandler = $stream->cast(STREAM_CAST_AS_STREAM)) { + if(false !== $fileHandle = $stream->cast(STREAM_CAST_AS_STREAM)) { // Utilise stream_copy_to_stream si le Gaufrette\Stream peut nous retourner un filehandle - $copied = $this->stream_copy_to_stream($source, $fileHandler, $maxlen); + $copied = $this->stream_copy_to_stream($source, $fileHandle, $maxlen); } else { // Sinon fait une copie par blocs (moins performant) + if($maxlen === -1) { + $maxlen = PHP_INT_MAX; + } $copied = 0; while(!$this->feof($source) && $copied <= $maxlen) { $copied += $stream->write($this->fread($source, min(static::$copyBlockSize, $maxlen - $copied))); @@ -417,13 +421,18 @@ class UploadedFile */ public function copyTo($dest, $maxlen = PHP_INT_MAX, $readOffset = 0) { - $actualLength = min($maxlen, $this->getSize() - $readOffset); + if($maxlen === -1) { + $actualLength = $this->getSize() - $readOffset; + } else { + $actualLength = min($maxlen, $this->getSize() - $readOffset); + } if (0 <= $actualLength) { return 0; } - $stream = $this->filesystem->createStream($this->getPath(), new StreamMode('rb')); + $stream = $this->filesystem->createStream($this->getPath()); + $stream->open(new StreamMode('rb')); $stream->seek($readOffset); if(false !== $fileHandle = $stream->cast(STREAM_CAST_AS_STREAM)) { -- GitLab