1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Some fatal errors

Discussion in 'XenForo Development Discussions' started by Kirk, Aug 2, 2015.

  1. Kirk

    Kirk Well-Known Member

    Hey all so recently I got some bugs reported on one of my add-ons

    So here are some of the errors some of the users have been reporting to me


    And here's my DataWriter code:
    PHP:
    class GFNRegAvatar_Extend_XenForo_DataWriter_User extends XFCP_GFNRegAvatar_Extend_XenForo_DataWriter_User
    {
        protected 
    $_avatarFile false;

        protected function 
    _preSave()
        {
            
    parent::_preSave();

            if (!
    XenForo_Application::isRegistered('regAvatarProxy'))
            {
                return;
            }

            
    $avatar XenForo_Application::get('regAvatarProxy');
            if (empty(
    $avatar['file']))
            {
                if (
    $avatar['required'] && $avatar['supported'])
                {
                    
    $this->error(new XenForo_Phrase('gfnregavatar_please_select_valid_avatar'), 'avatar');
                }

                return;
            }

            
    /** @var XenForo_Upload $file */
            
    $file $avatar['file'];

            if (!
    $file->isValid())
            {
                foreach (
    $file->getErrors() as $error)
                {
                    
    $this->error($error'avatar');
                }
            }

            if (!
    $file->isImage())
            {
                
    $this->error(new XenForo_Phrase('uploaded_file_is_not_valid_image'), 'avatar');
            }

            
    $imageType $file->getImageInfoField('type');
            if (!
    in_array($imageType, array(IMAGETYPE_GIFIMAGETYPE_JPEGIMAGETYPE_PNG)))
            {
                
    $this->error(new XenForo_Phrase('uploaded_file_is_not_valid_image'), 'avatar');
            }

            
    $this->_avatarFile $file->getTempFile();
        }

        protected function 
    _postSaveAfterTransaction()
        {
            
    parent::_postSaveAfterTransaction();

            if (
    $this->_avatarFile)
            {
                
    /** @var XenForo_Model_Avatar $avatarModel */
                
    $avatarModel $this->getModelFromCache('XenForo_Model_Avatar');

                try
                {
                    
    $avatarModel->applyAvatar($this->get('user_id'), $this->_avatarFile);
                }
                catch (
    Exception $e)
                {
                    
    XenForo_Error::logException($e);
                }
            }
        }
    }
     
  2. Kirk

    Kirk Well-Known Member

    Oh my i should of been thorough detailed in my original post.

    Anyways So when people upload a large avatar like 500px by 500px or 1000px by 1000px this is when the error occurs. I feel like it's somewhere in my DW code that's causing this problem.
     
  3. Kirk

    Kirk Well-Known Member

    bump....
     
  4. Daniel Hood

    Daniel Hood Well-Known Member

    Does uploading the same image as an avatar with your add-on disabled trigger the error?
     
  5. Kirk

    Kirk Well-Known Member

    I tried uploading a 1000px by 1000px as an avatar and no errors occur when I do this when i have my add-on disabled.
     
  6. Marcus

    Marcus Well-Known Member

    You could disable either the pre or the post save function and see which takes so long.
     
  7. Kirk

    Kirk Well-Known Member

    So I've commented out the post save function and it seems that part is taking the longest.
     
    Marcus likes this.
  8. Marcus

    Marcus Well-Known Member

    So you would look for $avatarModel->applyAvatar($this->get('user_id'), $this->_avatarFile);
     
  9. Kirk

    Kirk Well-Known Member

    Right which I have in my protected function _postSaveAfterTransaction

    PHP:

    class GFNRegAvatar_Extend_XenForo_DataWriter_User extends XFCP_GFNRegAvatar_Extend_XenForo_DataWriter_User
    {
    protected $_avatarFile = false;

    protected function _preSave()
    {
    parent::_preSave();

    if (!XenForo_Application::isRegistered('regAvatarProxy'))
    {
    return;
    }

    $avatar = XenForo_Application::get('regAvatarProxy');
    if (empty($avatar['file']))
    {
    if ($avatar['required'] && $avatar['supported'])
    {
    $this->error(new XenForo_Phrase('gfnregavatar_please_select_valid_avatar'), 'avatar');
    }

    return;
    }

    /** @var XenForo_Upload $file */
    $file = $avatar['file'];

    if (!$file->isValid())
    {
    foreach ($file->getErrors() as $error)
    {
    $this->error($error, 'avatar');
    }
    }

    if (!$file->isImage())
    {
    $this->error(new XenForo_Phrase('uploaded_file_is_not_valid_image'), 'avatar');
    }

    $imageType = $file->getImageInfoField('type');
    if (!in_array($imageType, array(IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG)))
    {
    $this->error(new XenForo_Phrase('uploaded_file_is_not_valid_image'), 'avatar');
    }

    $this->_avatarFile = $file->getTempFile();
    }

    protected function _postSaveAfterTransaction()
    {
    parent::_postSaveAfterTransaction();

    if ($this->_avatarFile)
    {
    /** @var XenForo_Model_Avatar $avatarModel */
    $avatarModel = $this->getModelFromCache('XenForo_Model_Avatar');

    try
    {
    $avatarModel->applyAvatar($this->get('user_id'), $this->_avatarFile);
    }
    catch (Exception $e)
    {
    XenForo_Error::logException($e);
    }
    }
    }
    }
    (I've marked in red). So would I need to move the $avatarModel->applyAvatar($this->get('user_id'), $this->_avatarFile); into _preSave function?
     
    Last edited: Aug 11, 2015
  10. Marcus

    Marcus Well-Known Member

    I think it does not matter where you call this function, this function takes a long time anywhere where you execute it.

    It's possible that your server has some problems uploading a file, or there are other issues. I would first take a look if really this function is slowing down anything. If yes, I would take a good look at this function, how it works, what it does.
     
  11. Kirk

    Kirk Well-Known Member

    So I tried commentating out that line and it went very fast but the avatar didn't show after registration so I guess I need to dig into that line of code more to understand it
     
    Marcus likes this.
  12. Kirk

    Kirk Well-Known Member

    From what I see the applyAvatar() grabs the user id, the image width and height and the time stamp. If I'm wrong on this could someone give a better explanation of what the applyAvatar() does?
     
  13. Marcus

    Marcus Well-Known Member

    I have actually no idea how this function works, I just would look at the red part: $avatarModel->applyAvatar($this->get('user_id'), $this->_avatarFile);It's possible that the file is very large and the function has some problems working with this large data.
     

Share This Page