public int DetectAuthenticInfo(AuthUser operatorUser, int userID, out List <string> photos) { photos = null; if (operatorUser.UserID <= 0) { ThrowError(new NotLoginError()); return(4); } if (!CanRealnameCheck(operatorUser)) { ThrowError(new NoPermissionRealnameCheckError()); return(4); } AuthenticUser userInfo = GetAuthenticUserInfo(operatorUser, userID); if (userInfo == null) { ThrowError(new CustomError("没有该用户提交的实名认证材料")); return(4); } List <byte[]> photoData; int state = DetectAuthenticInfo(userInfo.Realname, userInfo.IDNumber, out photoData); if (state == 0) { photos = new List <string>(); if (photoData != null) { string photoString = ""; string temp; string photoDirName = "Photos"; string photoPath = Globals.GetPath(SystemDirecotry.Upload_IDCard, photoDirName); string virtualPath = Globals.GetVirtualPath(SystemDirecotry.Upload_IDCard, photoDirName); if (!Directory.Exists(photoPath)) { Directory.CreateDirectory(photoPath); } for (int i = 0; i < photoData.Count; i++) { string fileName = string.Format("{0}_{1}.jpg", userInfo.IDNumber, i); if (photoString.Length > 0) { photoString += "|"; } temp = UrlUtil.JoinUrl(virtualPath, fileName); photoString += temp; photos.Add(temp); fileName = IOUtil.JoinPath(photoPath, fileName); if (!File.Exists(fileName)) { File.WriteAllBytes(fileName, photoData[i]); } if (photos.Count > 1) //多余的照片不要, 只要最多两张 { break; } } UserDao.Instance.UpdateAuthenticUserPhoto(userID, photoString, state); } } return(state); }
private string BuildThumb(PhysicalFileFromTemp file) { return(string.Empty); string thumbPathRoot = IOUtil.ResolvePath(ThumbRoot); string extendName = Path.GetExtension(file.TempUploadFileName); string level1 = file.MD5.Substring(0, 1); string level2 = file.MD5.Substring(1, 1); string level3 = file.MD5.Substring(2, 1); extendName = extendName.ToLower(); string thumbFilename = string.Format("{0}_{1}.png", file.MD5, file.FileSize); string dir = IOUtil.JoinPath(thumbPathRoot, level1, level2, level3); try { if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); } } catch { return(string.Empty); } string thumbFilePath = IOUtil.JoinPath(thumbPathRoot, level1, level2, level3, thumbFilename); string thumbUrl = UrlUtil.JoinUrl(ThumbRoot, level1, level2, level3, thumbFilename); switch (extendName) { case ".jpg": case ".jpge": case ".bmp": case ".png": case ".gif": Image img = null, imgThumb = null; try { img = Bitmap.FromFile(file.PhysicalFilePath); } catch // (Exception ex) { return(string.Empty); } using (imgThumb = new Bitmap(ThumbSize, ThumbSize)) { int x, y, w, h; float scale = (float)img.Width / (float)img.Height; if (img.Width > img.Height) { x = 0; w = ThumbSize; h = (int)((float)w / scale); y = (ThumbSize - h) / 2; } else if (img.Width == img.Height) { x = 0; y = 0; w = ThumbSize; h = ThumbSize; } else { y = 0; h = ThumbSize; w = (int)((float)ThumbSize * scale); x = (ThumbSize - w) / 2; } using (Graphics g = Graphics.FromImage(imgThumb)) { g.Clear(Color.White); g.DrawImage(img, new Rectangle(x, y, w, h), new Rectangle(0, 0, img.Width, img.Height), GraphicsUnit.Pixel); g.Save(); } try { img.Save(thumbFilePath, System.Drawing.Imaging.ImageFormat.Png); } catch { return(string.Empty); } } img.Dispose(); return(thumbUrl); default: break; } return(string.Empty); }
//public string GetAvatarUrl(UserAvatarType type, int userID, string fileProfix, bool isChcech) //{ // if (StringUtil.StartsWith(fileProfix, '~')) // { // //int index = fileProfix.LastIndexOf("/"); // return fileProfix.Replace("{size}", type.ToString()); // } // string url; // GetAvatarPathAndUrl(type, userID, fileProfix, isChcech, false, false, out url); // return url; //} //public string GetAvatarPhysicalPath(UserAvatarType type, int userID, string fileProfix, bool isChcech) //{ // string url; // return GetAvatarPathAndUrl(type, userID, fileProfix, isChcech, true, true, out url); //} //public string GetAvatarPath(UserAvatarType type, int userID, string fileProfix, bool isCheched, out string virtualPath) //{ // return GetAvatarPathAndUrl(type, userID, fileProfix, isCheched, false, true, out virtualPath); //} //private string GetAvatarPathAndUrl(UserAvatarType type, int userID, string fileProfix, bool isCheched, bool createDirectory, bool getPhysicsPath, out string virtualPath) //{ // if (fileProfix.Length > 4) // { // virtualPath = string.Empty; // return string.Empty; // } // if (string.IsNullOrEmpty(fileProfix)) // { // fileProfix = ".jpg"; // } // else // { // if (!StringUtil.StartsWith(fileProfix, '.')) // { // fileProfix = "." + fileProfix; // } // } // string fileName = userID + fileProfix; // string pathLevel = GetUserAvatarPathLevel(userID); // string temp1 = IOUtil.JoinPath(isCheched ? string.Empty : Consts.User_UncheckAvatarSuffix, type.ToString(), pathLevel); // virtualPath = UrlUtil.JoinUrl(Globals.GetRelativeUrl(SystemDirecotry.Upload_Avatar), temp1, fileName); // if (getPhysicsPath || createDirectory) // { // string dir = IOUtil.JoinPath(Globals.GetPath(SystemDirecotry.Upload_Avatar), temp1); // if (createDirectory) IOUtil.CreateDirectory(dir); // if (getPhysicsPath) return IOUtil.JoinPath(dir, fileName); // } // return string.Empty; //} /// <summary> /// 验证用户临时头像 /// </summary> /// <param name="operatorUserID"></param> /// <param name="targetUserIds"></param> /// <param name="isChecked"></param> public void CheckUserAvatar(AuthUser operatorUser, IEnumerable <int> targetUserIds, bool isChecked) { if (!CanAvatarCheck(operatorUser)) { ThrowError(new NoPermissionEditUserProfileError()); return; } UserTempAvatarCollection tempAvatarDatas = UserTempAvatarDao.Instance.GetUserTempAvatars(targetUserIds); string defaultDir = GetAvatarSizeDirectoryName(UserAvatarSize.Default); string bigDir = GetAvatarSizeDirectoryName(UserAvatarSize.Big); string smallDir = GetAvatarSizeDirectoryName(UserAvatarSize.Small); foreach (UserTempAvatar ta in tempAvatarDatas) { #if !DEBUG try { #endif if (isChecked)//审核通过 { if (!string.IsNullOrEmpty(ta.CurrentAvatar)) { IOUtil.DeleteFile(GetAvatarPhysicalPath(ta.UserID, UserAvatarSize.Small, ta.CurrentAvatar)); IOUtil.DeleteFile(GetAvatarPhysicalPath(ta.UserID, UserAvatarSize.Default, ta.CurrentAvatar)); IOUtil.DeleteFile(GetAvatarPhysicalPath(ta.UserID, UserAvatarSize.Big, ta.CurrentAvatar)); } string newFileType = Path.GetExtension(ta.TempAvatar); string newAvatarSrc = GetAvatarLevel(ta.UserID, "/", newFileType); string newFilePath = Globals.GetPath(SystemDirecotry.Upload_Avatar, "{0}", newAvatarSrc); IOUtil.MoveFile(IOUtil.MapPath(string.Format(ta.TempAvatar, smallDir)) , string.Format(newFilePath, smallDir)); IOUtil.MoveFile(IOUtil.MapPath(string.Format(ta.TempAvatar, defaultDir)) , string.Format(newFilePath, defaultDir)); IOUtil.MoveFile(IOUtil.MapPath(string.Format(ta.TempAvatar, bigDir)) , string.Format(newFilePath, bigDir)); User u = UserBO.Instance.GetUser(ta.UserID); u.AvatarSrc = newAvatarSrc; if (OnUserAvatarChanged != null) { OnUserAvatarChanged(u.UserID, u.AvatarSrc, u.SmallAvatarPath, u.AvatarPath, u.BigAvatarPath); } UserDao.Instance.UpdateAvatar(u.UserID, u.AvatarPropFlag.GetStringForSave(), true); } else //未审核通过 { IOUtil.DeleteFile(IOUtil.MapPath(string.Format(ta.TempAvatar, defaultDir))); IOUtil.DeleteFile(IOUtil.MapPath(string.Format(ta.TempAvatar, bigDir))); IOUtil.DeleteFile(IOUtil.MapPath(string.Format(ta.TempAvatar, smallDir))); } #if !DEBUG } catch { } #endif } UserTempDataBO.Instance.Delete(targetUserIds, UserTempDataType.Avatar); }
public EmoticonSaveStatus SaveEmoticonFile(int userID, byte[] fileData, string md5, string fileName, out string relativeUrl) { Size thumbSize = new Size(AllSettings.Current.EmoticonSettings.ThumbImageWidth, AllSettings.Current.EmoticonSettings.ThumbImageHeight); relativeUrl = ""; if (fileData != null && fileData.Length > 0) { //判断文件大小是否超出限制 if (fileData.Length > MaxEmticonFileSize(userID)) { return(EmoticonSaveStatus.FileSizeOverflow); } if (!IsAllowedFileType(fileName)) { return(EmoticonSaveStatus.Failed); } string dirLevel1, dirLevel2; string filePostfix = ".gif"; //Path.GetExtension(fileName); fileName = string.Format("{0}_{1}{2}", md5, fileData.Length, filePostfix); string directory = Globals.GetPath(SystemDirecotry.Upload_Emoticons); dirLevel1 = md5.Substring(0, 1); dirLevel2 = md5.Substring(1, 1); directory = IOUtil.JoinPath(directory, dirLevel1, dirLevel2); if (!Directory.Exists(directory)) { Directory.CreateDirectory(directory); } string filePath = IOUtil.JoinPath(directory, fileName); if (!File.Exists(filePath)) { using (FileStream fileStream = File.Create(filePath, fileData.Length)) { fileStream.Write(fileData, 0, fileData.Length); fileStream.Seek(0, SeekOrigin.Begin); #region 生成缩略图 try { using (Bitmap bmpSource = new Bitmap(fileStream)) { using (Bitmap bmp = new Bitmap(thumbSize.Width, thumbSize.Height)) { using (Graphics g = Graphics.FromImage(bmp)) { g.Clear(Color.White); g.DrawImage(bmpSource, new Rectangle(0, 0, thumbSize.Width, thumbSize.Height)); g.Save(); } bmp.Save(GetThumbFilePath(filePath, false), ImageFormat.Png); } } } catch { } #endregion fileStream.Close(); } } relativeUrl = fileName;//直接返回文件名 return(EmoticonSaveStatus.Success); } return(EmoticonSaveStatus.Failed); }
/// <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); //删除用户制作头像时上传的临时文件 } }