Commit c3c93c05 authored by Guillaume Perréal's avatar Guillaume Perréal
Browse files

UploadedFile: correction des méthodes copyFrom et copyTo.

Showing with 15 additions and 6 deletions
+15 -6
...@@ -384,20 +384,24 @@ class UploadedFile ...@@ -384,20 +384,24 @@ class UploadedFile
* @param int $writeOffset * @param int $writeOffset
* @return int * @return int
*/ */
public function copyFrom($source, $maxlen = PHP_INT_MAX, $writeOffset = 0) public function copyFrom($source, $maxlen = -1, $writeOffset = 0)
{ {
if($maxlen === 0) { if($maxlen === 0) {
return 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); $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 // 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 { } else {
// Sinon fait une copie par blocs (moins performant) // Sinon fait une copie par blocs (moins performant)
if($maxlen === -1) {
$maxlen = PHP_INT_MAX;
}
$copied = 0; $copied = 0;
while(!$this->feof($source) && $copied <= $maxlen) { while(!$this->feof($source) && $copied <= $maxlen) {
$copied += $stream->write($this->fread($source, min(static::$copyBlockSize, $maxlen - $copied))); $copied += $stream->write($this->fread($source, min(static::$copyBlockSize, $maxlen - $copied)));
...@@ -417,13 +421,18 @@ class UploadedFile ...@@ -417,13 +421,18 @@ class UploadedFile
*/ */
public function copyTo($dest, $maxlen = PHP_INT_MAX, $readOffset = 0) 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) { if (0 <= $actualLength) {
return 0; 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); $stream->seek($readOffset);
if(false !== $fileHandle = $stream->cast(STREAM_CAST_AS_STREAM)) { if(false !== $fileHandle = $stream->cast(STREAM_CAST_AS_STREAM)) {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment