public static bool AddMemberPhotoForGame(Guid gameId, Stream fileStream, string nameOfFile, Guid gameMemberId, MemberTypeEnum memberType) { try { var dc = new ManagementContext(); string name = String.Empty; Guid id = new Guid(); GameMember member = null; GameOfficial official = null; if (memberType == MemberTypeEnum.Skater) { member = dc.GameMembers.Where(x => x.GameMemberId == gameMemberId && x.Team.Game.GameId == gameId).FirstOrDefault(); if (member == null) return false; name = member.MemberName; id = member.MemberLinkId; } else if (memberType == MemberTypeEnum.Referee) { official = dc.GameOfficials.Where(x => x.GameOfficialId == gameMemberId && x.Game.GameId == gameId).FirstOrDefault(); if (official == null) return false; name = official.MemberName; id = official.MemberLinkId; } //time stamp for the save location DateTime timeOfSave = DateTime.UtcNow; FileInfo info = new FileInfo(nameOfFile); //the file name when we save it string url = "http://images.rdnation.com/games/" + timeOfSave.Year + "/" + timeOfSave.Month + "/" + timeOfSave.Day + "/"; string imageLocationToSave = @"C:\WebSites\images.rdnation.com\games\" + timeOfSave.Year + @"\" + timeOfSave.Month + @"\" + timeOfSave.Day + @"\"; //creates the directory for the image if (!Directory.Exists(imageLocationToSave)) Directory.CreateDirectory(imageLocationToSave); string fileName = RDN.Utilities.Strings.StringExt.ToSearchEngineFriendly(name + " roller derby-") + timeOfSave.ToFileTimeUtc() + info.Extension; string fileNameThumb = RDN.Utilities.Strings.StringExt.ToSearchEngineFriendly(name + " roller derby-thumb-") + timeOfSave.ToFileTimeUtc() + info.Extension; string urlMain = url + fileName; string urlThumb = url + fileNameThumb; string imageLocationToSaveMain = imageLocationToSave + fileName; string imageLocationToSaveThumb = imageLocationToSave + fileNameThumb; bool isLinkedToMainMember = false; if (id != new Guid()) { var memMain = dc.Members.Where(x => x.MemberId == id).FirstOrDefault(); if (memMain != null) { fileName = RDN.Utilities.Strings.StringExt.ToSearchEngineFriendly(memMain.DerbyName + " roller derby-") + timeOfSave.ToFileTimeUtc() + info.Extension; fileNameThumb = RDN.Utilities.Strings.StringExt.ToSearchEngineFriendly(memMain.DerbyName + " roller derby-thumb-") + timeOfSave.ToFileTimeUtc() + info.Extension; urlMain = url + fileName; urlThumb = url + fileNameThumb; imageLocationToSaveMain = imageLocationToSave + fileName; imageLocationToSaveThumb = imageLocationToSave + fileNameThumb; isLinkedToMainMember = true; RDN.Library.DataModels.Member.MemberPhoto image = new RDN.Library.DataModels.Member.MemberPhoto(); image.ImageUrl = urlMain; image.SaveLocation = imageLocationToSaveMain; image.SaveLocationThumb = imageLocationToSaveThumb; image.ImageUrlThumb = urlThumb; image.IsPrimaryPhoto = false; image.IsVisibleToPublic = true; image.Member = memMain; memMain.Photos.Add(image); } } //if it is, we won't actually save the picture in the game member table.. if (isLinkedToMainMember == false && member != null) { url += fileName; imageLocationToSave += fileName; GameMemberPhoto image = new GameMemberPhoto(); image.ImageUrl = urlMain; image.SaveLocation = imageLocationToSaveMain; image.SaveLocationThumb = imageLocationToSaveThumb; image.ImageUrlThumb = urlThumb; image.IsPrimaryPhoto = true; image.IsVisibleToPublic = true; image.Member = member; member.Photos.Add(image); } if (isLinkedToMainMember == false && official != null) { url += fileName; imageLocationToSave += fileName; GameOfficialPhoto image = new GameOfficialPhoto(); image.ImageUrl = urlMain; image.SaveLocation = imageLocationToSaveMain; image.SaveLocationThumb = imageLocationToSaveThumb; image.ImageUrlThumb = urlThumb; image.IsPrimaryPhoto = true; image.IsVisibleToPublic = true; image.Official = official; official.Photos.Add(image); } using (var newfileStream = new FileStream(imageLocationToSaveMain, FileMode.OpenOrCreate, FileAccess.Write)) { fileStream.CopyTo(newfileStream); } Image thumbImg = Image.FromStream(fileStream); Image thumb = RDN.Utilities.Drawing.Images.ScaleDownImage(thumbImg, 300, 300); thumb.Save(imageLocationToSaveThumb); //saves the photo to the DB. dc.SaveChanges(); return true; } catch (Exception exception) { ErrorDatabaseManager.AddException(exception, exception.GetType()); } return false; }
/// <summary> /// Adds the primary member photo and returns the url of the photo. /// </summary> /// <param name="mem"></param> /// <param name="fileStream"></param> /// <param name="nameOfFile"></param> /// <returns></returns> private static string AddPrimaryMemberPhoto(MemberDisplay mem, Stream fileStream, string nameOfFile) { StringBuilder sb = new StringBuilder(); try { var dc = new ManagementContext(); var memDb = dc.Members.Where(x => x.MemberId == mem.MemberId).FirstOrDefault(); //time stamp for the save location DateTime timeOfSave = DateTime.UtcNow; FileInfo info = new FileInfo(nameOfFile); //the file name when we save it string fileName = RDN.Utilities.Strings.StringExt.ToSearchEngineFriendly(mem.DerbyName + " roller derby-") + timeOfSave.ToFileTimeUtc() + info.Extension; string fileNameThumb = RDN.Utilities.Strings.StringExt.ToSearchEngineFriendly(mem.DerbyName + " roller derby-") + timeOfSave.ToFileTimeUtc() + "_thumb" + info.Extension; string url = "http://images.rdnation.com/members/" + timeOfSave.Year + "/" + timeOfSave.Month + "/" + timeOfSave.Day + "/"; string imageLocationToSave = @"C:\WebSites\images.rdnation.com\members\" + timeOfSave.Year + @"\" + timeOfSave.Month + @"\" + timeOfSave.Day + @"\"; //creates the directory for the image if (!Directory.Exists(imageLocationToSave)) Directory.CreateDirectory(imageLocationToSave); string urlMain = url + fileName; string urlThumb = url + fileNameThumb; string imageLocationToSaveMain = imageLocationToSave + fileName; string imageLocationToSaveThumb = imageLocationToSave + fileNameThumb; RDN.Library.DataModels.Member.MemberPhoto image = new RDN.Library.DataModels.Member.MemberPhoto(); image.ImageUrl = urlMain; image.SaveLocation = imageLocationToSaveMain; image.SaveLocationThumb = imageLocationToSaveThumb; image.ImageUrlThumb = urlThumb; image.IsPrimaryPhoto = true; image.IsVisibleToPublic = true; image.Member = memDb; memDb.Photos.Add(image); sb.Append(imageLocationToSaveMain); using (var newfileStream = new FileStream(imageLocationToSaveMain, FileMode.OpenOrCreate, FileAccess.Write)) { fileStream.CopyTo(newfileStream); } Image thumbImg = Image.FromStream(fileStream); Image thumb = RDN.Utilities.Drawing.Images.ScaleDownImage(thumbImg, 300, 300); sb.Append(nameOfFile); sb.Append(imageLocationToSaveThumb); thumb.Save(imageLocationToSaveThumb); //saves the photo to the DB. int c = dc.SaveChanges(); mem.Photos.Add(new PhotoItem(url, true, "Profile")); return urlMain; } catch (Exception exception) { ErrorDatabaseManager.AddException(exception, exception.GetType(), additionalInformation: sb.ToString()); } return string.Empty; }