private void DeleteEmoticonFiles(List <string> fileUrls) { foreach (string s in fileUrls) { if (s.Contains("/") || s.Contains("\\")) { IOUtil.DeleteFile(s); } else { string str = IOUtil.JoinPath(Globals.GetPath(SystemDirecotry.Upload_Emoticons), s.Substring(0, 1), s.Substring(1, 1), s); IOUtil.DeleteFile(str); } IOUtil.DeleteFile(GetThumbFilePath(s, false)); } }
private void DeleteUserAvatarFile(User user) { if (!string.IsNullOrEmpty(user.AvatarSrc)) { try { IOUtil.DeleteFile(user.SmallAvatarPath); IOUtil.DeleteFile(user.AvatarPath); IOUtil.DeleteFile(user.BigAvatarPath); } catch { return; } } }
public static bool ImageUnite(string destPath, string imagePath, ImageFormat imageFormat, Color backgroundColor, int width, int height) { try { using (Bitmap bitmap = new Bitmap(width, height)) { using (Graphics g = Graphics.FromImage(bitmap)) { g.Clear(backgroundColor); g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; g.CompositingQuality = CompositingQuality.HighQuality; Bitmap tempImage; string tempfileName; try { tempImage = new Bitmap(imagePath); //b1 = Image.FromFile(fileName); tempfileName = imagePath; //string tempFile = null; } catch { tempImage = new Bitmap(Globals.ApplicationPath + "/notImage.png"); tempfileName = Globals.ApplicationPath + "/notImage.png"; //tempFile = ""; } if (tempImage.Width > width || tempImage.Height > height) { tempImage = GetThunmbnailImage(tempfileName, width, height); tempImage.Save(destPath, imageFormat); return(true); } int x = 0, y = 0; //调整缩略图的位置 if (tempImage.Width < width)//当图片宽度小于指定宽度时 使其位于中间 { x = (width - tempImage.Width) / 2; } if (tempImage.Height < height) { y = (height - tempImage.Height) / 2; } g.DrawImage(tempImage, x, y, tempImage.Width, tempImage.Height); tempImage.Dispose(); } if (destPath == imagePath) { IOUtil.DeleteFile(imagePath); } bitmap.Save(destPath, imageFormat); } return(true); } catch { return(false); } }
//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); }
/// <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); //删除用户制作头像时上传的临时文件 } }