public IHttpActionResult DeleteAgendaMedia(Models.AgendaMedia media) { var mediaDelete = DataBase.AgendaMedias.Get(p => p.IdAgenda == media.IdAgenda && p.IdRuta == media.IdRuta && p.IdMedia == media.IdMedia).SingleOrDefault(); if (mediaDelete != null && !string.IsNullOrWhiteSpace(mediaDelete.Path)) { string deleteImagePath = System.Web.HttpContext.Current.Server.MapPath(mediaDelete.Path); FileInfo FileDetele = new FileInfo(deleteImagePath); if (FileDetele.Exists) { FileDetele.Delete(); } deleteImagePath = System.Web.HttpContext.Current.Server.MapPath(mediaDelete.PathMedium); FileDetele = new FileInfo(deleteImagePath); if (FileDetele.Exists) { FileDetele.Delete(); } deleteImagePath = System.Web.HttpContext.Current.Server.MapPath(mediaDelete.PathSmall); FileDetele = new FileInfo(deleteImagePath); if (FileDetele.Exists) { FileDetele.Delete(); } deleteImagePath = System.Web.HttpContext.Current.Server.MapPath(mediaDelete.PathMin); FileDetele = new FileInfo(deleteImagePath); if (FileDetele.Exists) { FileDetele.Delete(); } } mediaDelete.Estado = Constantes.Estado.Eliminado; DataBase.AgendaMedias.Update(mediaDelete); return(Ok()); }
public IHttpActionResult SetAgendaMedia(Models.AgendaMedia media) { var mediaUpdate = DataBase.AgendaMedias.Get(p => p.IdAgenda == media.IdAgenda && p.IdRuta == media.IdRuta && p.IdMedia == media.IdMedia).SingleOrDefault(); bool delete = false; short resultId = media.IdMedia; string deletePath = string.Empty; if (mediaUpdate == null) { mediaUpdate = new AgendaComercial.Models.Ruta.AgendaMedia(); mediaUpdate.TipoMedia = Constantes.TiposAgendaMedia.Imagenes; mediaUpdate.TipoMediaTabla = Constantes.TiposAgendaMedia.Tabla; mediaUpdate.Estado = Constantes.Estado.Activo; mediaUpdate.EstadoTabla = Constantes.Estado.Tabla; mediaUpdate.IdAgenda = media.IdAgenda; mediaUpdate.IdRuta = media.IdRuta; mediaUpdate.IdMedia = media.IdMedia; resultId = DataBase.AgendaMedias.GetMaxValue <short>(p => p.IdMedia, 0, p => p.IdRuta == media.IdRuta && p.IdAgenda == media.IdAgenda); if (resultId == 3) { return(Ok(mediaUpdate)); } mediaUpdate.IdMedia = ++resultId; DataBase.AgendaMedias.Insert(mediaUpdate); } else { delete = true; deletePath = mediaUpdate.Path; DataBase.AgendaMedias.Update(mediaUpdate); } mediaUpdate.FecMod = GetCurrentDateTime(); mediaUpdate.UsrMod = CurrentUser.LogonName; byte[] content = Convert.FromBase64String(media.Contenido); var newfileName = String.Format("{0}{1}", Guid.NewGuid(), System.IO.Path.GetExtension(media.Nombre)); string filePath = Path.Combine(System.Web.HttpContext.Current.Server.MapPath(AgendaComercial.Models.Constantes.AgendaMedia.FilePath), newfileName); Image image; using (MemoryStream ms = new MemoryStream(content)) { image = Image.FromStream(ms); ms.Flush(); image.Save(filePath); } PropertyItem property = null; double proportion = 1; string bigger = "h"; try { FileInfo fileDelete = new FileInfo(filePath); Bitmap src = Image.FromFile(filePath) as Bitmap; if (src.Height > src.Width) { proportion = (double)src.Height / (double)src.Width; } else { proportion = (double)src.Width / (double)src.Height; bigger = "w"; } property = src.GetPropertyItem(0x112); if (property.Value[0] == 6) //90 { src.RotateFlip(RotateFlipType.Rotate90FlipNone); } else if (property.Value[0] == 8)//-90 { src.RotateFlip(RotateFlipType.Rotate90FlipX); } else if (property.Value[0] == 3)//180 { src.RotateFlip(RotateFlipType.Rotate180FlipNone); } newfileName = String.Format("{0}{1}", Guid.NewGuid(), System.IO.Path.GetExtension(media.Nombre)); filePath = Path.Combine(System.Web.HttpContext.Current.Server.MapPath(Url.Content(AgendaComercial.Models.Constantes.AgendaMedia.FilePath)), newfileName); src.Save(filePath); fileDelete.Delete(); } catch { } if (bigger == "h") { Thumbnail.SaveThumbnail(filePath, Constantes.ProfileFotosSize.ProfilePictuteMinWidth, (int)(Math.Round(Constantes.ProfileFotosSize.ProfilePictuteMinHeight * proportion)), "min"); Thumbnail.SaveThumbnail(filePath, Constantes.ProfileFotosSize.ProfilePictuteMedWidth, (int)(Math.Round(Constantes.ProfileFotosSize.ProfilePictuteMedHeight * proportion)), "med"); Thumbnail.SaveThumbnail(filePath, Constantes.ProfileFotosSize.ProfilePictuteSmaWidth, (int)(Math.Round(Constantes.ProfileFotosSize.ProfilePictuteSmaHeight * proportion)), "sma"); } else { Thumbnail.SaveThumbnail(filePath, (int)(Math.Round(Constantes.ProfileFotosSize.ProfilePictuteMinWidth * proportion)), Constantes.ProfileFotosSize.ProfilePictuteMinHeight, "min"); Thumbnail.SaveThumbnail(filePath, (int)(Math.Round(Constantes.ProfileFotosSize.ProfilePictuteMedWidth * proportion)), Constantes.ProfileFotosSize.ProfilePictuteMedHeight, "med"); Thumbnail.SaveThumbnail(filePath, (int)(Math.Round(Constantes.ProfileFotosSize.ProfilePictuteSmaWidth * proportion)), Constantes.ProfileFotosSize.ProfilePictuteSmaHeight, "sma"); } try { if (delete && !string.IsNullOrWhiteSpace(deletePath)) { string deleteImagePath = System.Web.HttpContext.Current.Server.MapPath(mediaUpdate.Path); FileInfo FileDetele = new FileInfo(deleteImagePath); if (FileDetele.Exists) { FileDetele.Delete(); } deleteImagePath = System.Web.HttpContext.Current.Server.MapPath(mediaUpdate.PathMedium); FileDetele = new FileInfo(deleteImagePath); if (FileDetele.Exists) { FileDetele.Delete(); } deleteImagePath = System.Web.HttpContext.Current.Server.MapPath(mediaUpdate.PathSmall); FileDetele = new FileInfo(deleteImagePath); if (FileDetele.Exists) { FileDetele.Delete(); } deleteImagePath = System.Web.HttpContext.Current.Server.MapPath(mediaUpdate.PathMin); FileDetele = new FileInfo(deleteImagePath); if (FileDetele.Exists) { FileDetele.Delete(); } } } catch { } mediaUpdate.Path = Path.Combine(Constantes.AgendaMedia.FilePath, newfileName); DataBase.Save(); return(Ok(mediaUpdate)); }