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

Server issue  Avatar Upload - Cloud

#1
We discovered an interesting bug when trying to upload low KB files on cloud servers. Anything smaller than 32kb would throw a rename error. Apparently on a cloud server, the rename throws an exception faster than the cloud to check if the file already exists. In Avatar.php, we added this small try/catch and now any size avatar will work.

PHP:
protected function _writeAvatar($userId, $size, $tempFile)
    {
        if (!in_array($size, array_keys(self::$_sizes)))
        {
            throw new XenForo_Exception('Invalid avatar size.');
        }

        $filePath = $this->getAvatarFilePath($userId, $size);
        $directory = dirname($filePath);

        if (XenForo_Helper_File::createDirectory($directory, true) && is_writable($directory))
        {
            if (file_exists($filePath))
            {
                unlink($filePath);
            }

            try {
                $success = rename($tempFile, $filePath);
            } catch (Exception $e) {
                $success = file_exists($filePath);
            }

            if ($success)
            {
                XenForo_Helper_File::makeWritableByFtpUser($filePath);
            }

            return $success;
        }
        else
        {
            return false;
        }
    }
 

Mike

XenForo developer
Staff member
#3
That seems like a bug with the cloud filesystem them - it's very misleading to error on rename if it actually works.
 
#4
It looks like it's related to a small amount of latency and the rename function errors out on small files because it can't see them immediately after return
 

Mike

XenForo developer
Staff member
#5
If it's a latency thing, then a synchronous action (FS modification) is acting asynchronously and that's just going to introduce all sorts of errors. This really looks like a server issue; it just seems to violate the expected behavior.

What wass the exception anyway?