service/components/behaviors/StoreBehavior.php
/**
     * Writes content chunk to file
     *
     * @param  service\components\contents\ContentStream $stream
     * @param  string                                    $contentChunk
     * @return void
     */
Writes content chunk to file
@param  service\components\contents\ContentStream $stream
@param  string                                    $contentChunk
@return void
Writes content chunk to file @param service \ components \ contents \ ContentStream $stream @param string $contentChunk @return void
public function storeContentChunk(\service\components\contents\ContentStream $stream, $contentChunk)
    {
        $rangeStart = $stream->getParameter('rangeStart') != null ? (int)$stream->getParameter('rangeStart') : 0;
        $rangeEnd = $stream->getParameter('rangeEnd') != null ? (int)$stream->getParameter('rangeEnd') : $this->owner->length - 1;
        if ($this->hasStoringError) {
            return;
        }
        if ($this->storedContent == null) {
            $this->storedContentChunk = null;
            $this->storingStream = null;
            $this->hasStoringError = false;
            $this->storedContent = $this->openStoredContent();
            if ($this->storedContent != null && !$this->storedContent->isRangeStored($rangeStart, $rangeEnd)) {
                try {
                    $this->storedContentChunk = $this->storedContent->createChunk(true);
                } catch (\Exception $e) {
                    Yii::error($e);
                }
                if ($this->storedContentChunk != null) {
                    $this->storedContentChunk->start = $rangeStart;
                    $storage = Yii::$app->storage;
                    try {
                        $content = $storage->getFileContent($this->storedContentChunk->file);
                    } catch (\Exception $e) {
                        Yii::error($e);
                    }
                    if (isset($content)) {
                        $context = $content->createStreamContext();
                        $this->storingStream = $content->createStream($context, 'w');
                        $this->storingStream->truncate();
                    }
                }
            }
        }
        $needNewChunk = ($this->storedContentChunk != null && $this->storedContentChunk->length > self::MAX_STORED_CONTENT_CHUNK_SIZE);
        if ($needNewChunk) {
            $newChunkStart = $this->storedContentChunk->start + $this->storedContentChunk->length;
            if ($this->storingStream != null) {
                $this->storingStream->close();
                $this->storingStream = null;
            }
            $this->storedContentChunk->setScenario(StoredContentChunk::SCENARIO_UPDATE);
            $this->storedContentChunk->end = $this->storedContentChunk->start + $this->storedContentChunk->length - 1;
            $this->storedContentChunk->unlock();
            $this->storedContentChunk->save();
            $this->storedContentChunk = null;
            try {
                $this->storedContentChunk = $this->storedContent->createChunk(true);
            } catch (\Exception $e) {
                Yii::error($e);
            }
            if ($this->storedContentChunk != null) {
                $this->storedContentChunk->start = $newChunkStart;
                $storage = Yii::$app->storage;
                try {
                    $content = $storage->getFileContent($this->storedContentChunk->file);
                } catch (\Exception $e) {
                    Yii::error($e);
                }
                if (isset($content)) {
                    $context = $content->createStreamContext();
                    $this->storingStream = $content->createStream($context, 'w');
                    $this->storingStream->truncate();
                }
            }
        }
        if ($this->storingStream != null) {
            try {
                $bytesWritten = $this->storingStream->write($contentChunk);
                $this->hasStoringError = ($bytesWritten === false);
            } catch (\Exception $e) {
                $this->hasStoringError = true;
                Yii::error($e);
            }
            if (!$this->hasStoringError) {
                $this->storedContentChunk->length += $bytesWritten;
            }
        }
    }
public function storeContentChunk ( \ service \ components \ contents \ ContentStream $ stream , $ contentChunk ) { $ rangeStart = $ stream -> getParameter ( 'rangeStart' ) != null ? ( int ) $ stream -> getParameter ( 'rangeStart' ) : 0 ; $ rangeEnd = $ stream -> getParameter ( 'rangeEnd' ) != null ? ( int ) $ stream -> getParameter ( 'rangeEnd' ) : $ this -> owner -> length - 1 ; if ( $ this -> hasStoringError ) { return ; } if ( $ this -> storedContent == null ) { $ this -> storedContentChunk = null ; $ this -> storingStream = null ; $ this -> hasStoringError = false ; $ this -> storedContent = $ this -> openStoredContent ( ) ; if ( $ this -> storedContent != null && ! $ this -> storedContent -> isRangeStored ( $ rangeStart , $ rangeEnd ) ) { try { $ this -> storedContentChunk = $ this -> storedContent -> createChunk ( true ) ; } catch ( \ Exception $ e ) { Yii :: error ( $ e ) ; } if ( $ this -> storedContentChunk != null ) { $ this -> storedContentChunk -> start = $ rangeStart ; $ storage = Yii :: $ app -> storage ; try { $ content = $ storage -> getFileContent ( $ this -> storedContentChunk -> file ) ; } catch ( \ Exception $ e ) { Yii :: error ( $ e ) ; } if ( isset ( $ content ) ) { $ context = $ content -> createStreamContext ( ) ; $ this -> storingStream = $ content -> createStream ( $ context , 'w' ) ; $ this -> storingStream -> truncate ( ) ; } } } } $ needNewChunk = ( $ this -> storedContentChunk != null && $ this -> storedContentChunk -> length > self :: MAX_STORED_CONTENT_CHUNK_SIZE ) ; if ( $ needNewChunk ) { $ newChunkStart = $ this -> storedContentChunk -> start + $ this -> storedContentChunk -> length ; if ( $ this -> storingStream != null ) { $ this -> storingStream -> close ( ) ; $ this -> storingStream = null ; } $ this -> storedContentChunk -> setScenario ( StoredContentChunk :: SCENARIO_UPDATE ) ; $ this -> storedContentChunk -> end = $ this -> storedContentChunk -> start + $ this -> storedContentChunk -> length - 1 ; $ this -> storedContentChunk -> unlock ( ) ; $ this -> storedContentChunk -> save ( ) ; $ this -> storedContentChunk = null ; try { $ this -> storedContentChunk = $ this -> storedContent -> createChunk ( true ) ; } catch ( \ Exception $ e ) { Yii :: error ( $ e ) ; } if ( $ this -> storedContentChunk != null ) { $ this -> storedContentChunk -> start = $ newChunkStart ; $ storage = Yii :: $ app -> storage ; try { $ content = $ storage -> getFileContent ( $ this -> storedContentChunk -> file ) ; } catch ( \ Exception $ e ) { Yii :: error ( $ e ) ; } if ( isset ( $ content ) ) { $ context = $ content -> createStreamContext ( ) ; $ this -> storingStream = $ content -> createStream ( $ context , 'w' ) ; $ this -> storingStream -> truncate ( ) ; } } } if ( $ this -> storingStream != null ) { try { $ bytesWritten = $ this -> storingStream -> write ( $ contentChunk ) ; $ this -> hasStoringError = ( $ bytesWritten === false ) ; } catch ( \ Exception $ e ) { $ this -> hasStoringError = true ; Yii :: error ( $ e ) ; } if ( ! $ this -> hasStoringError ) { $ this -> storedContentChunk -> length += $ bytesWritten ; } } }
Writes content chunk to file
 @param  service\components\contents\ContentStream $stream
 @param  string                                    $contentChunk
 @return void
Writes content chunk to file @param service \ components \ contents \ ContentStream $stream @param string $contentChunk @return void
//Don't store content if there were errors //Reset storing attributes //Store content only if stored content created successfully and chunk is not already stored //Calculate new chunk start //Close stored content chunk stream //Save content chunk