Commit a9db1096 authored by Joe Hoyle's avatar Joe Hoyle
Browse files

Reapply hack to sdk for seeking uncached cached data

We have to modify the cachingentity, as we often want to seek ahead of
cached data, which the aws sdk does not support
parent dddd001b
Loading
Loading
Loading
Loading
+38 −15
Original line number Diff line number Diff line
@@ -63,14 +63,21 @@ class CachingEntityBody extends AbstractEntityBodyDecorator
        } elseif ($whence == SEEK_CUR) {
            $byte = $offset + $this->ftell();
        } else {
            throw new RuntimeException(__CLASS__ . ' supports only SEEK_SET and SEEK_CUR seek operations');
            /**
             * Hack workaround by Joe Hoyle, we need to fake the SEEK_END byte so exif functions work
             */
            $byte = $this->body->getSize();
            //throw new RuntimeException(__CLASS__ . ' supports only SEEK_SET and SEEK_CUR seek operations');
        }

        // You cannot skip ahead past where you've read from the remote stream
        if ($byte > $this->body->getSize()) {
            throw new RuntimeException(
                "Cannot seek to byte {$byte} when the buffered stream only contains {$this->body->getSize()} bytes"
            );
            // Modification by Joe Hoyle, this hack is needed fot getimagesize()
            // to work on the stream
            $this->read( $byte );
            //throw new RuntimeException(
            //    "Cannot seek to byte {$byte} when the buffered stream only contains {$this->body->getSize()} bytes"
            //);
        }

        return $this->body->seek($byte);
@@ -99,6 +106,13 @@ class CachingEntityBody extends AbstractEntityBodyDecorator

        // More data was requested so read from the remote stream
        if ($remaining) {

            /**
             * Modification by Joe Hoyle, we do a while loop to geet reading data, 
             * as the ramainging amount of data to fetch from the remote stream could
             * be more than the chunk size.
             */
            while( $remaining > 0 ) {
                // If data was written to the buffer in a position that would have been filled from the remote stream,
                // then we must skip bytes on the remote stream to emulate overwriting bytes from that position. This
                // mimics the behavior of other PHP stream wrappers.
@@ -110,8 +124,17 @@ class CachingEntityBody extends AbstractEntityBodyDecorator
                    $this->skipReadBytes = max(0, $this->skipReadBytes - $len);
                }

                $remaining -= strlen( $remoteData );

                $data .= $remoteData;
                $this->body->write($remoteData);

                if ( ! $remoteData ) {
                    break;
                }
            }
            

        }

        return $data;