public static function createFromUploadedFile(UploadedFile $upload, $uploadPath, $filename = null)
    {
        if (!$upload->isValid()) {
            throw new UploadException(sprintf('An error occurred during file upload. Error code: %s', $upload->getError()));
        } elseif (($uploadPath = realpath($uploadPath . '/')) === false) {
            throw new UploadException('An error occurred during file upload. The specified upload path is invalid.');
        }

        if (!$filename) {
            $fullPath = sprintf(
                '%s%s%s.%s',
                rtrim($uploadPath, DIRECTORY_SEPARATOR),
                DIRECTORY_SEPARATOR,
                uniqid(),
                ($upload->getExtension() ?: ($upload->guessExtension() ?: 'file'))
            );
        } else {
            $fullPath = rtrim($uploadPath, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $filename;
        }

        $file = new self($fullPath, $upload->getClientOriginalName(), $upload->getMimeType(), $upload->getClientSize(), md5_file($upload->getRealPath()));
        $file->_uploadedFile = $upload;

        return $file;
    }