public Arena.Services.Contracts.ModifyResult UpdateSmallGroupPhoto(System.IO.Stream stream, int id) { try { Arena.SmallGroup.Group group = new Arena.SmallGroup.Group(id); Boolean accessDenied = false; // If this person isn't the outright leader and they don't have edit access if (group.Leader.PersonID != ArenaContext.Current.Person.PersonID && RestApi.GroupClusterOperationAllowed(ArenaContext.Current.Person.PersonID, group.GroupClusterID, OperationType.Edit) == false) { accessDenied = true; // Do a deeper dive into each member of the group foreach (GroupMember gm in group.Members) { if (gm.Active && gm.Role.Value == "Leader") { accessDenied = false; break; } } } if (accessDenied) { throw new Exception("Access denied."); } MultipartParser parser = new MultipartParser(stream); if (parser.Success) { // Make sure this is a real image MemoryStream ms = new MemoryStream(parser.FileContents); String myStream = Encoding.ASCII.GetString(ms.ToArray()); Image image = Image.FromStream(ms); ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders(); ImageCodecInfo codec = codecs.First(c => c.FormatID == image.RawFormat.Guid); // If we have an existing image, delete it if (group.ImageBlob.BlobID > 0) { group.ImageBlob.Delete(); } // Create a new file Utility.ArenaImage arenaImage = new Utility.ArenaImage(); // Update everything arenaImage.ByteArray = parser.FileContents; arenaImage.MimeType = codec.MimeType; arenaImage.OriginalFileName = Path.GetFileNameWithoutExtension(parser.Filename) + "." + codec.FormatDescription.ToLower().Replace("jpeg", "jpg"); arenaImage.FileExtension = codec.FormatDescription.ToLower().Replace("jpeg", "jpg"); // Save the file group.ImageBlob = arenaImage; group.ImageBlob.Save(ArenaContext.Current.User.Identity.Name); group.Save(ArenaContext.Current.User.Identity.Name); } return new Services.Contracts.ModifyResult() { Link = Contracts.SmallGroupMapper.getImageThumbnailUrl(group.ImageBlob), Successful = true.ToString() }; } catch (Exception ex) { return new Services.Contracts.ModifyResult() { Successful = false.ToString(), ErrorMessage = string.Format("{0} - {1}", ex.GetType().ToString(), ex.Message) }; } }
public Arena.Services.Contracts.ModifyResult UpdateSmallGroupPhoto(System.IO.Stream stream, int id) { try { Arena.SmallGroup.Group group = new Arena.SmallGroup.Group(id); Boolean accessDenied = false; // If this person isn't the outright leader and they don't have edit access if (group.Leader.PersonID != ArenaContext.Current.Person.PersonID && RestApi.GroupClusterOperationAllowed(ArenaContext.Current.Person.PersonID, group.GroupClusterID, OperationType.Edit) == false) { accessDenied = true; // Do a deeper dive into each member of the group foreach (GroupMember gm in group.Members) { if (gm.Active && gm.Role.Value == "Leader") { accessDenied = false; break; } } } if (accessDenied) { throw new Exception("Access denied."); } MultipartParser parser = new MultipartParser(stream); if (parser.Success) { // Make sure this is a real image MemoryStream ms = new MemoryStream(parser.FileContents); String myStream = Encoding.ASCII.GetString(ms.ToArray()); Image image = Image.FromStream(ms); ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders(); ImageCodecInfo codec = codecs.First(c => c.FormatID == image.RawFormat.Guid); // If we have an existing image, delete it if (group.ImageBlob.BlobID > 0) { group.ImageBlob.Delete(); } // Create a new file Utility.ArenaImage arenaImage = new Utility.ArenaImage(); // Update everything arenaImage.ByteArray = parser.FileContents; arenaImage.MimeType = codec.MimeType; arenaImage.OriginalFileName = Path.GetFileNameWithoutExtension(parser.Filename) + "." + codec.FormatDescription.ToLower().Replace("jpeg", "jpg"); arenaImage.FileExtension = codec.FormatDescription.ToLower().Replace("jpeg", "jpg"); // Save the file group.ImageBlob = arenaImage; group.ImageBlob.Save(ArenaContext.Current.User.Identity.Name); group.Save(ArenaContext.Current.User.Identity.Name); } return(new Services.Contracts.ModifyResult() { Link = Contracts.SmallGroupMapper.getImageThumbnailUrl(group.ImageBlob), Successful = true.ToString() }); } catch (Exception ex) { return(new Services.Contracts.ModifyResult() { Successful = false.ToString(), ErrorMessage = string.Format("{0} - {1}", ex.GetType().ToString(), ex.Message) }); } }