Asgaroth / xupload

XUpload extension for Yii Framework

Home Page:http://www.yiiframework.com/extension/xupload/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Session state overwriting

xloading opened this issue · comments

Hi Asgaroth,

First of all thanks a lot for the great extension - it helped me a lot! ;)

I would like to clarify with you one problem that I'm facing while using this extension.
I need users to have opportunity to upload 3 images. And Xupload solves this easily BUT when I submit the form I quite often find only 1 or 2 images actually uploaded to target directory even though 3 was uploaded with Xupload.

After some research I found that this happens because only those 1 or 2 files appear in 'xuploadFiles' session state. Therefore I came to conclusion that this happens because there is no locking of session information (I store my sessions in DB) in this piece of code:

protected function beforeReturn() {
        $path = $this->getPath();

        // Now we need to save our file info to the user's session
        $userFiles = Yii::app( )->user->getState( $this->stateVariable, array());

        $userFiles[$this->formModel->{$this->fileNameAttribute}] = array(
            "path" => $path.$this->formModel->{$this->fileNameAttribute},
            //the same file or a thumb version that you generated
            "thumb" => $path.$this->formModel->{$this->fileNameAttribute},
            "filename" => $this->formModel->{$this->fileNameAttribute},
            'size' => $this->formModel->{$this->sizeAttribute},
            'mime' => $this->formModel->{$this->mimeTypeAttribute},
            'name' => $this->formModel->{$this->displayNameAttribute},
        );
        Yii::app( )->user->setState( $this->stateVariable, $userFiles );

        return true;
    }

Because of this absence of locking 3 quick AJAX upload requests (e.g. with small files to upload) can simply overwrite 'xuploadFiles' state array set by another concurrent request.

This problem seems to be partially related to the following issue that won't be fixed in Yii and needs to be resolved in each certain case:
http://code.google.com/p/yii/issues/detail?id=1403

Can you suggest some kind of general workaround for this problem that can be introduced within the extension?
I think I can do the one for my particular case (MySQL) but I have no idea how this can be done universally.

Thank you a lot for any help in advance!

I've actually managed to solve this problem by setting 'sequentialUploads' option of jQuery plugin to True - this makes AJAX requests sequential and therefore guarantees proper handling of session state information in DB.

Don't know if the bug should be present in this extension or not.

This behaviour is only happening when session is stored in the database, and its the frameowork handling that, not the extension.