/// <summary> /// Deletes given custom avatar. Does nothing when given avatar is not custom. /// </summary> /// <param name="avatar">Avatar to delete.</param> private static void DeleteCustomAvatar(AvatarInfo avatar) { // Delete avatar if some exists and is custom if (avatar != null) { AvatarInfoProvider.DeleteAvatarFile(avatar.AvatarGUID.ToString(), avatar.AvatarFileExtension, false, false); AvatarInfo.Provider.Delete(avatar); } }
/// <summary> /// Deletes user picture. /// </summary> /// <param name="ui">UserInfo</param> public static void DeleteOldUserPicture(UserInfo ui) { // Delete old picture if needed if (ui.UserAvatarID != 0) { // Delete avatar info provider if needed AvatarInfo ai = AvatarInfoProvider.GetAvatarInfoWithoutBinary(ui.UserAvatarID); if (ai != null) { if (ai.AvatarIsCustom) { AvatarInfoProvider.DeleteAvatarFile(ai.AvatarGUID.ToString(), ai.AvatarFileExtension, false, false); AvatarInfoProvider.DeleteAvatarInfo(ai); } ui.UserAvatarID = 0; UserInfoProvider.SetUserInfo(ui); CMSContext.CurrentUser.UserAvatarID = 0; } } // Backward compatibility else if (ui.UserPicture != "") { try { // Remove from HDD string jDirectory = HttpContext.Current.Server.MapPath("~/CMSGlobalFiles/UserPictures/"); string filename = ui.UserPicture.Remove(ui.UserPicture.IndexOf('/')); if (File.Exists(jDirectory + filename)) { File.Delete(jDirectory + filename); } } catch { } ui.UserPicture = ""; UserInfoProvider.SetUserInfo(ui); CMSContext.CurrentUser.UserPicture = ""; } }
/// <summary> /// Deletes group picture. /// </summary> /// <param name="gi">GroupInfo</param> public static void DeleteOldGroupPicture(GroupInfo gi) { // Delete old picture if needed if (gi.GroupAvatarID != 0) { // Delete avatar info provider if needed AvatarInfo ai = AvatarInfoProvider.GetAvatarInfoWithoutBinary(gi.GroupAvatarID); if (ai != null) { if (ai.AvatarIsCustom) { AvatarInfoProvider.DeleteAvatarInfo(ai); AvatarInfoProvider.DeleteAvatarFile(ai.AvatarGUID.ToString(), ai.AvatarFileExtension, false, false); } gi.GroupAvatarID = 0; GroupInfoProvider.SetGroupInfo(gi); } } }
/// <summary> /// OK click event handler. /// </summary> protected void btnOK_Click(object sender, EventArgs e) { if ((uploadAvatar.PostedFile == null) && (ai == null)) { ShowError(GetString("avat.fileinputerror")); } else { SiteInfo site = SiteContext.CurrentSite; int width = 0; int height = 0; int sidesize = 0; // Get resize values if (drpAvatarType.SelectedValue != "all") { // Get right settings key string siteName = ((site != null) ? (site.SiteName + ".") : ""); string prefix = "CMSAvatar"; if (drpAvatarType.SelectedValue == "group") { prefix = "CMSGroupAvatar"; } width = SettingsKeyInfoProvider.GetIntValue(siteName + prefix + "Width"); height = SettingsKeyInfoProvider.GetIntValue(siteName + prefix + "Height"); sidesize = SettingsKeyInfoProvider.GetIntValue(siteName + prefix + "MaxSideSize"); } // Check if avatar name is unique string newAvatarName = txtAvatarName.Text.Trim(); AvatarInfo avatarWithSameName = AvatarInfoProvider.GetAvatarInfoWithoutBinary(newAvatarName); if (avatarWithSameName != null) { if (ai != null) { // Check unique avatar name of existing avatar if (avatarWithSameName.AvatarID != ai.AvatarID) { ShowError(GetString("avat.uniqueavatarname")); return; } } // Check unique avatar name of new avatar else { ShowError(GetString("avat.uniqueavatarname")); return; } } // Process form in these cases: // 1 - creating new avatar and uploaded file is not empty and it is image file // 2 - updating existing avatar and not uploading new image file // 3 - updating existing avatar and uploading image file if (((ai == null) && (uploadAvatar.PostedFile != null) && (uploadAvatar.PostedFile.ContentLength > 0) && (ImageHelper.IsImage(Path.GetExtension(uploadAvatar.PostedFile.FileName)))) || ((ai != null) && ((uploadAvatar.PostedFile == null) || (uploadAvatar.PostedFile.ContentLength == 0))) || ((ai != null) && (uploadAvatar.PostedFile != null) && (uploadAvatar.PostedFile.ContentLength > 0) && (ImageHelper.IsImage(Path.GetExtension(uploadAvatar.PostedFile.FileName))))) { if (ai == null) { switch (drpAvatarType.SelectedValue) { case "user": ai = new AvatarInfo(uploadAvatar.PostedFile, width, height, sidesize); break; case "group": ai = new AvatarInfo(uploadAvatar.PostedFile, width, height, sidesize); break; case "all": ai = new AvatarInfo(uploadAvatar.PostedFile, 0, 0, 0); break; default: ai = new AvatarInfo(uploadAvatar.PostedFile, 0, 0, 0); break; } ai.AvatarIsCustom = false; ai.AvatarGUID = Guid.NewGuid(); } else if ((uploadAvatar.PostedFile != null) && (uploadAvatar.PostedFile.ContentLength > 0) && (ImageHelper.IsMimeImage(uploadAvatar.PostedFile.ContentType))) { AvatarInfoProvider.DeleteAvatarFile(ai.AvatarGUID.ToString(), ai.AvatarFileExtension, false, false); AvatarInfoProvider.UploadAvatar(ai, uploadAvatar.PostedFile, width, height, sidesize); } // Set new avatar name ai.AvatarName = newAvatarName; imgAvatar.Visible = true; imgAvatar.ImageUrl = ResolveUrl("~/CMSModules/Avatars/CMSPages/GetAvatar.aspx?maxsidesize=250&avatarguid=" + ai.AvatarGUID); // Set new type ai.AvatarType = drpAvatarType.SelectedValue; // If avatar is not global, can't be default any default avatar if (ai.AvatarIsCustom) { // Set all default avatar options to false ai.DefaultUserAvatar = ai.DefaultMaleUserAvatar = ai.DefaultFemaleUserAvatar = ai.DefaultGroupAvatar = false; } else { // If user default avatar is changing if (ai.DefaultUserAvatar ^ chkDefaultUserAvatar.Checked) { AvatarInfoProvider.ClearDefaultAvatar(DefaultAvatarTypeEnum.User); } // If male default avatar is changing if (ai.DefaultMaleUserAvatar ^ chkDefaultMaleUserAvatar.Checked) { AvatarInfoProvider.ClearDefaultAvatar(DefaultAvatarTypeEnum.Male); } // If female default avatar is changing if (ai.DefaultFemaleUserAvatar ^ chkDefaultFemaleUserAvatar.Checked) { AvatarInfoProvider.ClearDefaultAvatar(DefaultAvatarTypeEnum.Female); } // If group default avatar is changing if (ai.DefaultGroupAvatar ^ chkDefaultGroupAvatar.Checked) { AvatarInfoProvider.ClearDefaultAvatar(DefaultAvatarTypeEnum.Group); } // Set new default avatar settings ai.DefaultUserAvatar = chkDefaultUserAvatar.Checked; ai.DefaultMaleUserAvatar = chkDefaultMaleUserAvatar.Checked; ai.DefaultFemaleUserAvatar = chkDefaultFemaleUserAvatar.Checked; ai.DefaultGroupAvatar = chkDefaultGroupAvatar.Checked; } AvatarInfoProvider.SetAvatarInfo(ai); avatarId = ai.AvatarID; URLHelper.Redirect("Avatar_Edit.aspx?saved=1&avatarid=" + avatarId); } else { // If given file is not valid if ((uploadAvatar.PostedFile != null) && (uploadAvatar.PostedFile.ContentLength > 0) && !ImageHelper.IsImage(Path.GetExtension(uploadAvatar.PostedFile.FileName))) { ShowError(GetString("avat.filenotvalid")); } else { // If posted file is not given ShowError(GetString("avat.fileinputerror")); } } } }
/// <summary> /// Updates picture of current group. /// </summary> /// <param name="gi">Group info object</param> public void UpdateGroupPicture(GroupInfo gi) { AvatarInfo ai = null; if (gi != null) { // Delete avatar if needed if (ValidationHelper.GetBoolean(hiddenDeleteAvatar.Value, false)) { DeleteOldGroupPicture(gi); } // If some file was uploaded if ((uplFilePicture.PostedFile != null) && (uplFilePicture.PostedFile.ContentLength > 0) && ImageHelper.IsImage(Path.GetExtension(uplFilePicture.PostedFile.FileName))) { // Check if this group has some avatar and if so check if is custom ai = AvatarInfoProvider.GetAvatarInfoWithoutBinary(gi.GroupAvatarID); if ((ai != null) && ai.AvatarIsCustom) { ReplaceExistingAvatar(ai); } else { // Delete old picture DeleteOldGroupPicture(gi); ai = CreateNewAvatar(); } AvatarInfoProvider.SetAvatarInfo(ai); // Update group info gi.GroupAvatarID = ai.AvatarID; GroupInfoProvider.SetGroupInfo(gi); plcImageActions.Visible = true; } // If predefined was chosen else if (!string.IsNullOrEmpty(hiddenAvatarGuid.Value)) { // Delete old picture DeleteOldGroupPicture(gi); Guid guid = ValidationHelper.GetGuid(hiddenAvatarGuid.Value, Guid.NewGuid()); ai = AvatarInfoProvider.GetAvatarInfoWithoutBinary(guid); // Update group info if (ai != null) { gi.GroupAvatarID = ai.AvatarID; GroupInfoProvider.SetGroupInfo(gi); } plcImageActions.Visible = true; } else { plcImageActions.Visible = false; } } else { bool pseudoDelete = ValidationHelper.GetBoolean(hiddenDeleteAvatar.Value, false); // Try to get avatar info if (avatarID != 0) { ai = AvatarInfoProvider.GetAvatarInfoWithoutBinary(avatarID); } // If some new picture was uploaded if ((uplFilePicture.PostedFile != null) && (uplFilePicture.PostedFile.ContentLength > 0) && ImageHelper.IsImage(Path.GetExtension(uplFilePicture.PostedFile.FileName))) { // Change delete to false because file will be replaced pseudoDelete = false; // If some avatar exists and is custom if ((ai != null) && (ai.AvatarIsCustom)) { // Delete file and upload new AvatarInfoProvider.DeleteAvatarFile(ai.AvatarGUID.ToString(), ai.AvatarFileExtension, false, false); ReplaceExistingAvatar(ai); } else { ai = CreateNewAvatar(); } // Update database AvatarInfoProvider.SetAvatarInfo(ai); } // If some predefined avatar was selected else if (!string.IsNullOrEmpty(hiddenAvatarGuid.Value)) { // Change delete to false because file will be replaced pseudoDelete = false; // If some avatar exists and is custom if ((ai != null) && (ai.AvatarIsCustom)) { AvatarInfoProvider.DeleteAvatarFile(ai.AvatarGUID.ToString(), ai.AvatarFileExtension, false, false); AvatarInfoProvider.DeleteAvatarInfo(ai); } Guid guid = ValidationHelper.GetGuid(hiddenAvatarGuid.Value, Guid.NewGuid()); ai = AvatarInfoProvider.GetAvatarInfoWithoutBinary(guid); } // If file was deleted - not replaced if (pseudoDelete) { // Delete it ai = AvatarInfoProvider.GetAvatarInfoWithoutBinary(avatarID); if (ai != null) { if (ai.AvatarIsCustom) { AvatarInfoProvider.DeleteAvatarInfo(ai); } } ai = null; avatarID = 0; plcImageActions.Visible = false; GroupPicture.AvatarID = 0; } // Update avatar id if (ai != null) { GroupPicture.AvatarID = avatarID = ai.AvatarID; plcImageActions.Visible = true; } } }