public async Task <PhotoDescription> GetPhotoMetadata(string photo) { if (String.IsNullOrEmpty(photo)) { throw new ArgumentException("Value cannot be null or empty.", nameof(photo)); } PhotoDescription photoDescription = new PhotoDescription(); try { const string requestParameters = "visualFeatures=Categories,Description,Color,Faces,ImageType,Adult"; const string uri = BaseAddress + "?" + requestParameters; using (var client = new HttpClient()) { client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", Constant.COGNITIVE_SERVICE_API_KEY); var json = JsonConvert.SerializeObject(new { url = photo }); var request = new StringContent(json, Encoding.UTF8, "application/json"); var response = await client.PostAsync(uri, request); response.EnsureSuccessStatusCode(); var responseJson = await response.Content.ReadAsStringAsync(); var result = JsonConvert.DeserializeObject <CognitiveServicesResponse>(responseJson); photoDescription = new PhotoDescription { Url = photo, Description = result }; } } catch (Exception ex) { _logger.LogError($"CognitiveServicesService->GetDescription: {ex.Message} - {ex.StackTrace}"); throw; } return(photoDescription); }
public async Task <IActionResult> Edit(int id, PhotoDescriptionsViewModel model) { var currentUser = await GetCurrentUserAsync(); if (currentUser.Id != model.Photo.UserId) { return(NotFound()); } if (id != model.Photo.Id) { return(NotFound()); } if (ModelState.IsValid) { try { List <Description> descriptions = await _context.Descriptions.ToListAsync(); List <PhotoDescription> existingPhotoDescriptions = await _context.PhotoDescriptions.Where(pd => pd.PhotoId == id).ToListAsync(); // Remove all photoDescription joint tables on the current photo. existingPhotoDescriptions.ForEach(pd => _context.PhotoDescriptions.Remove(pd)); if (model.CheckedKeywords != null) { foreach (string keyword in model.CheckedKeywords) { // If the keyword already exists in the Description database use that description id and add new joint table if (descriptions.Any(m => m.Keyword.ToLower() == keyword.ToLower())) { var existingDescription = descriptions.Find(m => m.Keyword.ToLower() == keyword.ToLower()); PhotoDescription photoDescription = new PhotoDescription(); photoDescription.PhotoId = model.Photo.Id; photoDescription.DescriptionId = existingDescription.Id; _context.Add(photoDescription); } // Else create a new description and add both the new description and joint table else { Description description = new Description(); description.Keyword = keyword; _context.Add(description); PhotoDescription photoDescription = new PhotoDescription(); photoDescription.PhotoId = model.Photo.Id; photoDescription.DescriptionId = description.Id; _context.Add(photoDescription); } } } _context.Update(model.Photo); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!PhotoExists(model.Photo.Id)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } return(View(model)); }
public BXRatingVotingTotals Vote(int photoId, string sign) { if (!EnableVoting || !IsVotingAllowed) return null; var photo = BXIBlockElement.GetById(photoId); if (photo == null || photo.IBlockId!=IBlockId) return null; var desc = new PhotoDescription(); desc.Element = photo; desc.CurUserId = CurrentUserId; bool sgn = sign == "true"; BXRatingVote vote = new BXRatingVote(); vote.Active = true; vote.RatingVotingId = desc.Voting.Id; if (desc.VotingTotals.UserHasVoted || !desc.IsVotingAllowed) { return null; } //vote.Value = sign ? Voting.Config.PlusValue : Voting.Config.MinusValue; vote.Value = sgn ? PositiveVoteValue : NegativeVoteValue; vote.UserId = CurrentUserId; vote.UserIP = Request.UserHostAddress; vote.Create(); desc.VotingTotals = null; return desc.VotingTotals; }
//METHODS protected void Page_Load(object sender, EventArgs e) { if (IBlockId > 0) { //if (EnableSef) //{ // MapVariable(Parameters.Get<string>("SEFFolder", "/photogallery"), Parameters.Get<string>("SEF_Album"), Results); //} //else //{ // BXParamsBag<string> variableAlias = new BXParamsBag<string>(); // variableAlias["AlbumID"] = Parameters.Get<string>("ParamAlbum", "Album"); // variableAlias["PageID"] = Parameters.Get<string>("ParamAlbumPage", "Album"); // variableAlias["PageShowAll"] = Parameters.Get<string>("ParamAlbumShowAll", "Album"); // MapVariable(variableAlias, Results); // if (AlbumId > 0) // { // Results["AlbumParentID"] = BXInfoBlockSectionManagerOld.GetById(AlbumId).ParentSectionId; // } //} int albumParent = 0; if (BXIBlock.IsUserCanOperate(IBlockId, BXIBlock.Operations.IBlockModifySections)) CanModify = true; if (AlbumId > 0) { //load album AlbumItem = BXIBlockSection.GetById(AlbumId, Bitrix.Services.Text.BXTextEncoder.EmptyTextEncoder); if (AlbumItem != null) { albumParent = AlbumItem.SectionId; if (Page is BXPublicPage) ((BXPublicPage)Page).MasterTitle = Server.HtmlDecode(AlbumItem.Name); Page.Title = AlbumItem.Name; IsNested = true; BXFile image = AlbumItem.Image; if (image != null) { Bitrix.Services.Image.BXImageInfo imageInfo = Bitrix.Services.Image. BXImageUtility.GetResizedImage(image, Parameters.Get("PreviewWidth", 75), Parameters.Get("PreviewHeight", 75)); AlbumCoverUrl = imageInfo.GetUri(); } FillNavChain(); } } BackUrl = GetAlbumUrl(albumParent); UploadUrl = MakeLink(Parameters.Get<string>("UrlTemplateUpload", BXConfigurationUtility.Constants.ErrorHref), AlbumId); EditUrl = MakeLink(Parameters.Get<string>("UrlTemplateAlbumEdit", BXConfigurationUtility.Constants.ErrorHref), AlbumId); AddUrl = MakeLink(Parameters.Get<string>("UrlTemplateAlbumAdd", BXConfigurationUtility.Constants.ErrorHref), AlbumId); BXFilter filter = new BXFilter(); BXPagingParams pagingParams = new BXPagingParams(); if (AlbumItem != null) { filter.Add(new BXFilterItem(BXIBlockSection.Fields.IBlock.ID, BXSqlFilterOperators.Equal, Parameters.Get("IBlockId", 0))); filter.Add(new BXFilterItem(BXIBlockSection.Fields.SectionId, BXSqlFilterOperators.Equal, AlbumItem.Id)); filter.Add(new BXFilterItem(BXIBlockSection.Fields.DepthLevel, BXSqlFilterOperators.Less, Album.DepthLevel + 2)); BXFilter photoFilter = new BXFilter(); photoFilter.Add(new BXFilterItem(BXIBlockElement.Fields.IBlock.ID, BXSqlFilterOperators.Equal, Parameters.Get("IBlockId", 0))); photoFilter.Add(new BXFilterItem(BXIBlockElement.Fields.Sections.Section.ID, BXSqlFilterOperators.Equal, Album.SectionId)); BXParamsBag<object> mapParams = new BXParamsBag<object>(); mapParams["AlbumID"] = AlbumId; pagingParams.AllowPaging = true; string pageId = Request.QueryString["page"]; int photoId = Parameters.Get<int>("PhotoId",0);// если шаблон фото детально передал параметр, //значит нужно показать элемент вместе с текущей страницей для вывода в слайдер pagingParams = PreparePagingParams(); if (photoId > 0 && pageId==null) { //Determine page BXIBlockElement currentPhoto = BXIBlockElement.GetById(photoId); int count = BXIBlockElement.Count(new BXFilter(new BXFilterItem(BXIBlockElement.Fields.Sections.Section.ID, BXSqlFilterOperators.Equal, AlbumItem.Id), new BXFilterItem(BXIBlockElement.Fields.IBlock.ID, BXSqlFilterOperators.Equal, currentPhoto.IBlockId))); if (currentPhoto != null) { BXFilter countFilter = new BXFilter(); countFilter.Add(new BXFilterItem(BXIBlockElement.Fields.ID, BXSqlFilterOperators.Less, currentPhoto.Id)); countFilter.Add(new BXFilterItem(BXIBlockElement.Fields.IBlock.ID, BXSqlFilterOperators.Equal, currentPhoto.IBlockId)); countFilter.Add(new BXFilterItem(BXIBlockElement.Fields.Sections.Section.ID, BXSqlFilterOperators.Equal, AlbumItem.Id)); int index = BXIBlockElement.Count(countFilter); BXPagingHelper helper = ResolvePagingHelper(count, pagingParams); pagingParams.Page = helper.GetOuterIndex(helper.GetPageIndexForItem(index)); Component.Parameters["PagingPageId"] = (pagingParams.Page ?? 1).ToString(); Component.Parameters["PageId"] = (pagingParams.Page ?? 1).ToString(); } } BXQueryParams queryParams = PreparePaging( pagingParams, delegate { return BXIBlockElement.Count(photoFilter); }, mapParams, "PageID", Parameters.Get<string>("UrlTemplateAlbum"), Parameters.Get<string>("UrlTemplateAlbumPage"), Parameters.Get<string>("UrlTemplateAlbumShowAll") ); var photoOrderBy = new BXOrderBy(); photoOrderBy.Add(BXIBlockElement.Fields, String.Format("{0} {1}", this.PhotoSortBy, this.PhotoSortOrder)); PhotoItems = BXIBlockElement.GetList(photoFilter, photoOrderBy, null, queryParams, Bitrix.Services.Text.BXTextEncoder.EmptyTextEncoder); PhotoDescriptionDictionary = new Dictionary<BXIBlockElement, PhotoDescription>(); //Component.Parameters["PageId"] = (pagingParams.Page ?? 1).ToString(); ComponentCache["PageId"] = (pagingParams.Page ?? 1).ToString(); if (Photos.Count > 0) { foreach (BXIBlockElement photo in PhotoItems) { PhotoDescription photoDesc = new PhotoDescription(); photoDesc.Preview = string.Empty; BXFile image = photo.DetailImageId > 0 ? photo.DetailImage : photo.PreviewImage; if (image == null) continue; Bitrix.Services.Image.BXImageInfo imageInfo = Bitrix.Services.Image. BXImageUtility.GetResizedImage(image, Parameters.Get("PreviewWidth", 75), Parameters.Get("PreviewHeight", 75)); photoDesc.Preview = imageInfo.GetUri(); photoDesc.DetailUrl = image.GetUri(); BXParamsBag<object> replace = new BXParamsBag<object>(); replace.Add("PHOTOID", photo.Id); replace.Add("ALBUMID", AlbumId); if (!EnableSef) { photoDesc.PhotoUrl = string.Format("?{0}={1}", Parameters.Get<string>("ParamPhoto", "photo"), photo.Id); } else { photoDesc.PhotoUrl = MakeLink(Parameters.Get<string>("SEFFolder"), Parameters.Get<string>("SEF_Photo"), replace); } photoDesc.ActualCoverHeight = imageInfo.Height; photoDesc.ActualCoverWidth = imageInfo.Width; photoDesc.CurUserId = CurrentUserId; photoDesc.DetailHeight = image.Height; photoDesc.DetailWidth = image.Width; photoDesc.Element = photo; PhotoDescriptionDictionary[photo] = photoDesc; } } ParentAlbumId = Album.ParentSectionId; } else { filter.Add(new BXFilterItem(BXIBlockSection.Fields.IBlock.ID, BXSqlFilterOperators.Equal, Parameters.Get("IBlockId", 0))); filter.Add(new BXFilterItem(BXIBlockSection.Fields.DepthLevel, BXSqlFilterOperators.Less, 2)); if (Page is BXPublicPage) ((BXPublicPage)Page).MasterTitle = GetMessageRaw("Photogallery"); Page.Title = GetMessageRaw("Photogallery"); ParentAlbumId = 0; } var albumOrderBy = new BXOrderBy(); albumOrderBy.Add(BXIBlockSection.Fields, String.Format("{0} {1}", this.AlbumSortBy, this.AlbumSortOrder)); SectionItems = BXIBlockSection.GetList(filter, albumOrderBy, null, null, Bitrix.Services.Text.BXTextEncoder.EmptyTextEncoder); AlbumDescriptionDictionary = new Dictionary<BXIBlockSection, AlbumDescription>(); if ((Sections.Count != 0) && (AlbumId > 0)) IsParent = true; foreach (BXIBlockSection section in SectionItems) { AlbumDescription albumDesc = new AlbumDescription(); filter = new BXFilter(); filter.Add(new BXFilterItem(BXIBlockElement.Fields.IBlock.ID, BXSqlFilterOperators.Equal, Parameters.Get("IBlockId", 0))); filter.Add(new BXFilterItem(BXIBlockElement.Fields.Sections.Section.ID, BXSqlFilterOperators.Equal, section.Id)); BXIBlockElementCollection elements = BXIBlockElement.GetList(filter, null); albumDesc.CoverImageUrl = BXUri.ToRelativeUri( "~/bitrix/components/bitrix/photogallery.album/templates/.default/images/no_image.png"); albumDesc.Count = elements.Count; albumDesc.Url = GetAlbumUrl(section.Id); albumDesc.EditUrl = MakeLink(Parameters.Get<string>("UrlTemplateAlbumEdit", BXConfigurationUtility.Constants.ErrorHref), section.Id); BXFile image = section.Image; if (image == null) foreach (BXIBlockElement element in elements) { image = element.PreviewImage; if (image != null) break; } albumDesc.IsCoverEmpty = true; if (image != null) { Bitrix.Services.Image.BXImageInfo imageInfo = Bitrix.Services.Image. BXImageUtility.GetResizedImage( image, Parameters.Get("CoverWidth", 75), Parameters.Get("CoverHeight", 75)); albumDesc.CoverImageUrl = imageInfo.GetUri(); albumDesc.IsCoverEmpty = false; } AlbumDescriptionDictionary[section] = albumDesc; } } IncludeComponentTemplate(); }