예제 #1
0
        /// <summary>
        /// 处理由头像Flash上传的已经截取好的图片(三个尺寸)
        /// </summary>
        /// <param name="Request"></param>
        public void SaveAvatar(AuthUser operatorUser, int targetUserID, HttpRequest request)
        {
            if (operatorUser == User.Guest)
            {
                ThrowError(new NotLoginError());
                return;
            }

            if (operatorUser.UserID != targetUserID)
            {
                if (!CanEditUserAvatar(operatorUser, targetUserID))
                {
                    return;
                }
            }

            string tempFilename = request.QueryString["file"];

            string extendName = Path.GetExtension(tempFilename).ToLower();

            if (extendName != ".png" &&
                extendName != ".gif" &&
                extendName != ".jpg" &&
                extendName != ".jpeg" &&
                extendName != ".bmp")
            {
                throw new Exception("头像的文件扩展名不能是" + extendName);
            }

            uint length    = 0;
            int  lastIndex = 0;

            byte[] data = request.BinaryRead(request.TotalBytes);
            byte[] avatarData, dataLength, sizeArray;

            int            sizeIndex = 0;
            UserAvatarSize avatarSize;

            dataLength = new byte[4];
            sizeArray  = StringUtil.Split <byte>(request.Headers["size"]);

            bool isUnappreved;

            //如果开启了头像审查,且操作者没有审查头像的权限,那么头像就应该是未审核状态
            if (AllSettings.Current.AvatarSettings.EnableAvatarCheck
                &&
                CanAvatarCheck(operatorUser) == false)
            {
                isUnappreved = true;
            }
            else
            {
                isUnappreved = false;
            }

            //同时上传3个尺寸的头像。 分割数据
            while (lastIndex < data.Length)
            {
                dataLength[0] = data[lastIndex];
                dataLength[1] = data[lastIndex + 1];
                dataLength[2] = data[lastIndex + 2];
                dataLength[3] = data[lastIndex + 3];

                Array.Reverse(dataLength);
                length     = BitConverter.ToUInt32(dataLength, 0);
                lastIndex += 4;
                avatarData = new byte[length];
                for (int i = 0; i < length; i++)
                {
                    avatarData[i] = data[lastIndex + i];
                }
                lastIndex += (int)length;

                if (sizeArray[sizeIndex] == 24)
                {
                    avatarSize = UserAvatarSize.Small;
                }
                else if (sizeArray[sizeIndex] == 120)
                {
                    avatarSize = UserAvatarSize.Big;
                }
                else
                {
                    avatarSize = UserAvatarSize.Default;
                }


                string savePath = BuildAvatarPath(targetUserID, isUnappreved, avatarSize, extendName);

                IOUtil.CreateDirectoryIfNotExists(Path.GetDirectoryName(savePath));

                using (FileStream file = new FileStream(savePath, FileMode.Create))
                {
                    file.Write(avatarData, 0, avatarData.Length);
                }

                //UploadAvatar(operatorUser, targetUserID, temp, avatarType, extendName);

                sizeIndex++;
            }

            #region 对用户进行积分操作

            if (isUnappreved)
            {
                string savePath = UrlUtil.JoinUrl(Globals.GetVirtualPath(SystemDirecotry.Upload_Avatar), Consts.User_UncheckAvatarSuffix, "{0}", GetAvatarLevel(targetUserID, "\\", extendName));

                UserTempDataBO.Instance.SaveData(targetUserID, UserTempDataType.Avatar, savePath, true);
            }
            else
            {
                AuthUser user = GetAuthUser(targetUserID, true);

                string savePath = GetAvatarLevel(targetUserID, "/", extendName);
                user.AvatarPropFlag.OriginalData = savePath;
                UserDao.Instance.UpdateAvatar(targetUserID, user.AvatarPropFlag.GetStringForSave(), true);
                //RemoveUserCache(targetUserID);

                user.AvatarSrc = savePath;
                user.ClearAvatarCache();

                if (OnUserAvatarChanged != null)
                {
                    string smallAvatarPath   = UrlUtil.JoinUrl(Globals.SiteRoot, user.SmallAvatarPath);
                    string defaultAvatarPath = UrlUtil.JoinUrl(Globals.SiteRoot, user.AvatarPath);
                    string bigAvatarPath     = UrlUtil.JoinUrl(Globals.SiteRoot, user.BigAvatarPath);
                    OnUserAvatarChanged(targetUserID, savePath, smallAvatarPath, defaultAvatarPath, bigAvatarPath);
                }
            }

            #endregion


            if (tempFilename.StartsWith(Globals.GetVirtualPath(SystemDirecotry.Temp_Avatar), StringComparison.OrdinalIgnoreCase))
            {
                IOUtil.DeleteFile(tempFilename); //删除用户制作头像时上传的临时文件
            }
        }