Beispiel #1
0
        public static bool IsImage(this HttpPostedFileBase file)
        {
            if (file == null || string.IsNullOrEmpty(file.FileName))
            {
                return(false);
            }

            return(file.IsExtension(".jpg", ".jpeg", ".png", ".gif"));
        }
Beispiel #2
0
        public ActionResult Edit(Noticias noticias, HttpPostedFileBase audioUpload, HttpPostedFileBase fotoUpload, int[] CategoriaId, int[] EditoriaId, string ListaTags = "", string fotoExistente = "", string audioExistente = "")
        {
            EditoriaId  = EditoriaId.Where(id => id > 0).ToArray();
            CategoriaId = CategoriaId.Where(id => id > 0).ToArray();

            if (!string.IsNullOrEmpty(fotoExistente))
            {
                noticias.foto = fotoExistente;
                ModelState.Remove("fotoUpload");
            }
            if (!string.IsNullOrEmpty(audioExistente))
            {
                noticias.audio = audioExistente;
                ModelState.Remove("audio");
            }
            var padrao = @"<(img|a)[^>]*>(?<content>[^<]*)<";
            var regex  = new Regex(padrao);

            if (noticias.fotoCredito != null && regex.IsMatch(noticias.fotoCredito))
            {
                ModelState.AddModelError("fotoCredito", "O campo não pode conter código HTML.");
            }
            if (noticias.chamada != null && regex.IsMatch(noticias.chamada))
            {
                ModelState.AddModelError("chamada", "O campo não pode conter código HTML.");
            }
            if ((fotoUpload == null && string.IsNullOrWhiteSpace(noticias.foto)) && (noticias.TipoDestaque.Value == (int)TipoDestaque.Normal1 || noticias.TipoDestaque.Value == (int)TipoDestaque.Normal2 || noticias.TipoDestaque.Value == (int)TipoDestaque.Normal3 || noticias.TipoDestaque.Value == (int)TipoDestaque.Urgente1130 || noticias.TipoDestaque.Value == (int)TipoDestaque.Urgente360))
            {
                ModelState.AddModelError("TipoDestaque", "A notícia deve ter uma foto para poder ser colocada em destaque.");
            }
            if (audioUpload != null && !audioUpload.IsExtension("mp3", "aac"))
            {
                ModelState.AddModelError("audio", "A extensão do áudio deve ser apenas nos formatos: .mp3 e .aac .");
            }
            if (CategoriaId == null || CategoriaId.Length == 0)
            {
                ModelState.AddModelError("CategoriaId", "Selecione uma categoria");
            }
            if (EditoriaId == null || EditoriaId.Length == 0)
            {
                ModelState.AddModelError("EditoriaId", "Selecione uma editoria");
            }
            if (ModelState.IsValid)
            {
                if (noticias.TipoDestaque.Value == (int)TipoDestaque.Editoria)
                {
                    noticias.destaqueEditoria = true;
                    noticias.TipoDestaque     = null;
                }

                if (!string.IsNullOrEmpty(noticias.videoYoutube))
                {
                    if (noticias.videoYoutube.Contains("="))
                    {
                        string[] keyYoutube = noticias.videoYoutube.Split(new char[] { '=' });
                        noticias.videoYoutube = keyYoutube[1];
                    }
                }

                #region uploads
                if (fotoUpload != null)
                {
                    var path1361x750 = Server.MapPath(string.Format(pathFoto1361x750, noticias.id));
                    var path744x500  = Server.MapPath(string.Format(pathFoto744x500, noticias.id));
                    var path405x270  = Server.MapPath(string.Format(pathFoto405x270, noticias.id));
                    var path365x240  = Server.MapPath(string.Format(pathFoto365x240, noticias.id));
                    var path260x173  = Server.MapPath(string.Format(pathFoto260x173, noticias.id));

                    var fileOriginal = Server.MapPath(string.Format(pathOriginal, noticias.id));

                    noticias.foto = Utils.SaveFileBase(fileOriginal, fotoUpload);

                    noticias.fotoMini = noticias.foto;

                    Utils.resizeImageAndSave3(Path.Combine(fileOriginal, noticias.foto), 744, 500, path744x500);
                    Utils.resizeImageAndSave3(Path.Combine(fileOriginal, noticias.foto), 405, 270, path405x270);
                    Utils.resizeImageAndSave3(Path.Combine(fileOriginal, noticias.foto), 365, 240, path365x240);
                    Utils.resizeImageAndSave3(Path.Combine(fileOriginal, noticias.foto), 260, 173, path260x173);

                    if (noticias.destaqueEditoria)
                    {
                        Utils.resizeImageAndSave3(Path.Combine(fileOriginal, noticias.foto), 1361, 750, path1361x750);
                    }
                }

                int suffix = 0;

                do
                {
                    noticias.url =
                        (!string.IsNullOrWhiteSpace(noticias.chamada) ?
                         noticias.chamada.GenerateSlug() :
                         !string.IsNullOrWhiteSpace(noticias.TituloCapa) ?
                         noticias.TituloCapa.GenerateSlug() : noticias.titulo.GenerateSlug()) + (suffix > 0 ? suffix.ToString() : string.Empty);
                    suffix++;
                } while (db.Noticias.Where(o => o.url == noticias.url && o.id != noticias.id).Count() > 0);


                if (audioUpload != null)
                {
                    var path = Server.MapPath("~/conteudo/noticias/" + noticias.id + "/audio/");
                    noticias.audio = Utils.SaveFileBase(path, audioUpload);
                }

                var bdDataAtualizacao = db.Noticias.AsNoTracking().FirstOrDefault(noticia => noticia.id == noticias.id).dataAtualizacao;

                //noticias.dataAtualizacao = DateTime.Now;
                if (noticias.dataAtualizacao == null || bdDataAtualizacao == noticias.dataAtualizacao)
                {
                    noticias.dataAtualizacao = DateTime.Now;
                }
                db.Entry(noticias).State = EntityState.Modified;
                db.SaveChanges();
                #endregion

                #region categorias

                db.Entry(noticias).Collection("Categorias").Load();
                noticias.Categorias.Clear();

                if (CategoriaId.Length > 0)
                {
                    foreach (var item in CategoriaId)
                    {
                        int idCategoria = Convert.ToInt32(item);
                        var categoria   = db.Categorias.FirstOrDefault(x => x.Id == item);
                        if (categoria != null)
                        {
                            noticias.Categorias.Add(categoria);
                            db.SaveChanges();
                        }
                    }
                }


                #endregion


                #region Editoriais
                db.Entry(noticias).Collection("Editoriais").Load();
                noticias.Editoriais.Clear();

                if (EditoriaId.Length > 0)
                {
                    foreach (var item in EditoriaId)
                    {
                        int idEditorial = Convert.ToInt32(item);
                        var editorial   = db.Editoriais.FirstOrDefault(x => x.id == item);
                        if (editorial != null)
                        {
                            noticias.Editoriais.Add(editorial);
                            db.SaveChanges();
                        }
                    }
                }

                #endregion

                #region Tags
                db.Entry(noticias).Collection("Tags").Load();
                noticias.Tags.Clear();


                if (!string.IsNullOrEmpty(ListaTags))
                {
                    noticias.Tags = new List <Tags>();
                    string[] tags = ListaTags.Split(new char[] { ',' });

                    foreach (var item in tags)
                    {
                        var hasTag = db.Tags.FirstOrDefault(x => x.Titulo.ToLower() == item.ToLower());

                        if (hasTag != null)
                        {
                            noticias.Tags.Add(hasTag);
                        }
                        else
                        {
                            var obj = new Tags
                            {
                                chave        = item.GenerateSlug(),
                                DataCadastro = DateTime.Now,
                                Excluido     = false,
                                Titulo       = item
                            };

                            db.Tags.Add(obj);
                            db.SaveChanges();

                            noticias.Tags.Add(obj);
                        }
                    }
                }
                #endregion


                db.Entry(noticias).State = EntityState.Modified;
                db.SaveChanges();
                Site.Services.RedisService.FlushAll(noticias.url);
                Site.Services.RedisService.FlushAll(noticias.id.ToString());
                GerenciaLogs.saveLog(ref db, WebSecurity.GetUserId(User.Identity.Name), areaADM, TipoAcesso.Edicao, noticias.id);
                if (noticias.Editoriais.FirstOrDefault().especial)
                {
                    return(RedirectToAction("Index", "EditorialNoticias", new { editorial = noticias.Editoriais.FirstOrDefault().chave }));
                }
                return(RedirectToAction("Index"));
            }

            if (!string.IsNullOrEmpty(noticias.videoYoutube))
            {
                noticias.videoYoutube = "https://www.youtube.com/watch?v=" + noticias.videoYoutube;
            }

            noticias.Editoriais.Clear();
            foreach (var item in EditoriaId)
            {
                int idEditorial = Convert.ToInt32(item);
                var editorial   = db.Editoriais.FirstOrDefault(x => x.id == item);
                if (editorial != null)
                {
                    noticias.Editoriais.Add(editorial);
                }
            }

            ViewBag.CategoriaMapaId = new SelectList(db.CategoriasMapa.Where(a => !a.Excluido), "Id", "Titulo", noticias.CategoriaMapaId);
            ViewBag.idColunista     = new SelectList(db.Colunista.Where(a => !a.excluido), "id", "nome", noticias.idColunista);
            ViewBag.idGaleria       = new SelectList(db.Galeria.Where(a => !a.excluido && !a.Fixa).OrderByDescending(a => a.dataCadastro), "id", "titulo", noticias.idGaleria);
            ViewBag.RegiaoId        = new SelectList(db.NoticiasRegioes.Where(a => !a.Excluido), "Id", "Titulo", noticias.RegiaoId);
            ViewBag.TiposDestaques  = (from Enum item in Enum.GetValues(typeof(TipoDestaque))
                                       select new SelectListItem
            {
                Value = ((int)(object)item).ToString(),
                Text = item.GetDescription()
            }).ToList();


            int catId = noticias.Categorias.Count > 0 ? noticias.Categorias.First().Id : db.Categorias.FirstOrDefault(x => !x.Excluido).Id;

            var editoriais = db.Editoriais.Where(a => !a.excluido && !a.esportes).ToList();

            var edtrl = editoriais.FirstOrDefault(a => !a.excluido && a.Categorias.Any(x => x.Id == catId));

            int firstId = edtrl == null?editoriais.First().id : edtrl.id;

            ViewBag.EditoriaId  = editoriais;
            ViewBag.CategoriaId = new SelectList(db.Categorias.Where(a => !a.Excluido && a.EditoriaId == firstId).ToList(), "Id", "Titulo", catId);

            ViewBag.AutoCompleteTags = db.Tags.Where(a => !a.Excluido).Select(x => x.Titulo).ToArray();

            ViewBag.tagsNoticia = ListaTags;

            return(View(noticias));
        }
Beispiel #3
0
        public ActionResult Edit(Noticias noticias, HttpPostedFileBase audioUpload, HttpPostedFileBase fotoUpload, int[] EditoriaId, string ListaTags = "", string fotoExistente = "")
        {
            if (!string.IsNullOrEmpty(fotoExistente))
            {
                noticias.foto = fotoExistente;
                ModelState.Remove("fotoUpload");
            }
            if (audioUpload != null && !audioUpload.IsExtension("mp3", "aac"))
            {
                ModelState.AddModelError("audio", "A extensão do áudio deve ser apenas nos formatos: .mp3 e .aac .");
            }
            if ((fotoUpload == null && string.IsNullOrWhiteSpace(noticias.foto)) && (noticias.TipoDestaque.Value == (int)TipoDestaque.Normal1 || noticias.TipoDestaque.Value == (int)TipoDestaque.Normal2 || noticias.TipoDestaque.Value == (int)TipoDestaque.Normal3 || noticias.TipoDestaque.Value == (int)TipoDestaque.Urgente1130 || noticias.TipoDestaque.Value == (int)TipoDestaque.Urgente360))
            {
                ModelState.AddModelError("TipoDestaque", "A notícia deve ter uma foto para poder ser colocada em destaque.");
            }
            else if (ModelState.IsValid)
            {
                if (!string.IsNullOrEmpty(noticias.videoYoutube))
                {
                    if (noticias.videoYoutube.Contains("="))
                    {
                        string[] keyYoutube = noticias.videoYoutube.Split(new char[] { '=' });
                        noticias.videoYoutube = keyYoutube[1];
                    }
                }

                #region uploads
                if (fotoUpload != null)
                {
                    var path744x500 = Server.MapPath(string.Format(pathFoto744x500, noticias.id));
                    var path405x270 = Server.MapPath(string.Format(pathFoto405x270, noticias.id));
                    var path365x240 = Server.MapPath(string.Format(pathFoto365x240, noticias.id));
                    var path260x173 = Server.MapPath(string.Format(pathFoto260x173, noticias.id));

                    var fileOriginal = Server.MapPath(string.Format(pathOriginal, noticias.id));

                    noticias.foto = Utils.SaveFileBase(fileOriginal, fotoUpload);

                    noticias.fotoMini = noticias.foto;

                    Utils.resizeImageAndSave3(Path.Combine(fileOriginal, noticias.foto), 744, 500, path744x500);
                    Utils.resizeImageAndSave3(Path.Combine(fileOriginal, noticias.foto), 405, 270, path405x270);
                    Utils.resizeImageAndSave3(Path.Combine(fileOriginal, noticias.foto), 365, 240, path365x240);
                    Utils.resizeImageAndSave3(Path.Combine(fileOriginal, noticias.foto), 260, 173, path260x173);
                }

                int suffix = 0;

                do
                {
                    noticias.url = (!string.IsNullOrWhiteSpace(noticias.chamada) ?
                                    noticias.chamada.GenerateSlug() :
                                    !string.IsNullOrWhiteSpace(noticias.TituloCapa) ?
                                    noticias.TituloCapa.GenerateSlug() : noticias.titulo.GenerateSlug()) + (suffix > 0 ? suffix.ToString() : "");
                    suffix++;
                } while (db.Noticias.Where(o => o.url == noticias.url && o.id != noticias.id).Count() > 0);


                if (audioUpload != null)
                {
                    var path = Server.MapPath("~/conteudo/noticias/" + noticias.id + "/audio/");
                    noticias.audio = Utils.SaveFileBase(path, audioUpload);
                }

                var bdDataAtualizacao = db.Noticias.AsNoTracking().FirstOrDefault(noticia => noticia.id == noticias.id).dataAtualizacao;

                //noticias.dataAtualizacao = DateTime.Now;
                if (noticias.dataAtualizacao == null || bdDataAtualizacao == noticias.dataAtualizacao)
                {
                    noticias.dataAtualizacao = DateTime.Now;
                }

                db.Entry(noticias).State = EntityState.Modified;
                db.SaveChanges();
                #endregion


                #region Tags
                db.Entry(noticias).Collection("Tags").Load();
                noticias.Tags.Clear();


                if (!string.IsNullOrEmpty(ListaTags))
                {
                    noticias.Tags = new List <Tags>();
                    string[] tags = ListaTags.Split(new char[] { ',' });

                    foreach (var item in tags)
                    {
                        var hasTag = db.Tags.FirstOrDefault(x => x.Titulo.ToLower() == item.ToLower());

                        if (hasTag != null)
                        {
                            noticias.Tags.Add(hasTag);
                        }
                        else
                        {
                            var obj = new Tags
                            {
                                chave        = item.GenerateSlug(),
                                DataCadastro = DateTime.Now,
                                Excluido     = false,
                                Titulo       = item
                            };

                            db.Tags.Add(obj);
                            db.SaveChanges();

                            noticias.Tags.Add(obj);
                        }
                    }
                }
                #endregion

                #region Editoriais
                if (EditoriaId.Count() > 0)
                {
                    db.Entry(noticias).Collection("Editoriais").Load();
                    noticias.Editoriais.Clear();

                    foreach (var id in EditoriaId)
                    {
                        int idEditorial = Convert.ToInt32(id);
                        var editorial   = db.Editoriais.FirstOrDefault(x => x.id == id);
                        if (editorial != null)
                        {
                            noticias.Editoriais.Add(editorial);
                        }
                    }
                }
                #endregion
                db.Entry(noticias).State = EntityState.Modified;

                db.Database.Log = m => Log(m);
                db.SaveChanges();
                Site.Services.RedisService.FlushAll(noticias.url);
                Site.Services.RedisService.FlushAll(noticias.id.ToString());
                GerenciaLogs.saveLog(ref db, WebSecurity.GetUserId(User.Identity.Name), areaADM, TipoAcesso.Edicao, noticias.id);
                return(RedirectToAction("Index"));
            }

            foreach (var item in EditoriaId)
            {
                int idEditorial = Convert.ToInt32(item);
                var editorial   = db.Editoriais.FirstOrDefault(x => x.id == item);
                if (editorial != null)
                {
                    noticias.Editoriais.Add(editorial);
                }
            }

            ViewBag.CategoriaMapaId = new SelectList(db.CategoriasMapa.Where(a => !a.Excluido), "Id", "Titulo", noticias.CategoriaMapaId);
            ViewBag.idColunista     = new SelectList(db.Colunista.Where(a => !a.excluido), "id", "nome", noticias.idColunista);
            ViewBag.idGaleria       = new SelectList(db.Galeria.Where(a => !a.excluido && !a.Fixa).OrderByDescending(a => a.dataCadastro), "id", "titulo", noticias.idGaleria);
            ViewBag.RegiaoId        = new SelectList(db.NoticiasRegioes.Where(a => !a.Excluido), "Id", "Titulo", noticias.RegiaoId);
            ViewBag.TiposDestaques  = (from Enum item in Enum.GetValues(typeof(TipoDestaque))
                                       select new SelectListItem
            {
                Value = ((int)(object)item).ToString(),
                Text = item.GetDescription()
            }).ToList();

            var times      = db.Times.Where(time => time.Ativo && !time.Excluido);
            var editoriais = db.Editoriais.Where(a => !a.excluido && a.ativo && a.esportes && a.id != 1 && !times.Any(time => time.EditoriaId == a.id));

            ViewBag.AutoCompleteTags = db.Tags.Where(a => !a.Excluido).Select(x => x.Titulo).ToArray();



            var tagsNoticia = noticias.Tags.Select(x => x.Titulo).ToArray();
            var editoriaIds = noticias.Editoriais.Select(editoria => editoria.id).ToArray();
            ViewBag.Times = times.Select(time => new SelectListItem {
                Value = time.EditoriaId.ToString(), Text = time.Nome, Selected = editoriaIds.Any(editoria => editoria == time.EditoriaId)
            }).ToList();
            ViewBag.Campeonatos = editoriais.Select(editorial => new SelectListItem {
                Value = editorial.id.ToString(), Text = editorial.nome, Selected = editoriaIds.Any(editoria => editoria == editorial.id)
            }).ToList();

            ViewBag.tagsNoticia = ListaTags;

            return(View(noticias));
        }