public ActionResult DoUpsert(Media model) { try { var service = new MediaService(); model.UserId = User.Identity.GetUserId(); if (service.FillInfoMedia(model) == false) { return Json(new ResponseMessageModel { HasError = false, Title = ResShared.TITLE_REGISTER_FAILED, Message = ResMediaRep.ERROR_NOMEDIA_INFO }); } ModelState.Clear(); ValidateModel(model); if (ModelState.IsValid == false) { return Json(new ResponseMessageModel { HasError = true, Title = ResShared.TITLE_REGISTER_FAILED, Message = ResShared.ERROR_INVALID_MODEL }); } var respMsg = service.DoUpsert(Db, model); return Json(respMsg); } catch (Exception ex) { SharedLogger.LogError(ex, model.RealName, model.MediaId, model.UserId); return Json(new ResponseMessageModel { HasError = true, Title = ResShared.TITLE_REGISTER_FAILED, Message = ResShared.ERROR_UNKOWN }); } }
public bool FillInfoMedia(Media model) { try { //Revisar primero si es local o externo switch (model.SourceType) { case SharedConstants.MEDIA_TYPE_LOCAL: var filePath = Path.Combine(HttpContext.Current.Server.MapPath("~/" + UploadMediaService.MediaRelativePath), model.RealName); using (var fileInfo = File.OpenRead(filePath)) { model.Size = fileInfo.Length; } model.Length = 0; model.Url = model.RealName; var extFile = model.RealName.FileExtensionWoDot(); model.CatMimeId = UploadMediaService.DicMimeExtensions.Where(e => e.Value.LstMimes.Contains(extFile)).Select(e => e.Value.CatMimeId).First(); break; case SharedConstants.MEDIA_TYPE_EXTERNAL: model.Length = 0; model.LogicName = model.Url; model.RealName = model.Url; model.Size = 0; model.CatMimeId = UploadMediaService.DicMimeExtensions.Where(e => e.Value.LstMimes.Contains(SharedConstants.MEDIA_EXTENSION_EXTERNAL)).Select(e => e.Value.CatMimeId).First(); break; } model.Timestamp = DateTime.Now; model.Type = "NA"; } catch (Exception ex) { SharedLogger.LogError(ex); return false; } return true; }
public ResponseMessageModel DoUpsert(SignageDigitalEntities db, Media model) { var repository = new GenericRepository<Media>(db); if (model.MediaId == EntityConstants.NULL_VALUE) { repository.Add(model); } else { repository.Update(model); } return new ResponseMessageModel { HasError = false, Title = ResShared.TITLE_REGISTER_SUCCESS, Message = ResShared.INFO_REGISTER_SAVED }; }
public ActionResult Upsert(int? id) { Media model; try { if (id.HasValue) { model = new GenericRepository<Media>(Db).FindById(id); ViewBag.Preview = UrlApp + UploadMediaService.MediaRelativePath + model.RealName; } else { model = new Media { MediaId = EntityConstants.NULL_VALUE }; } } catch (Exception ex) { SharedLogger.LogError(ex); return new HttpStatusCodeResult(HttpStatusCode.InternalServerError); } return View(model); }