public IHttpActionResult Get(int id)
        {
            var leftPart = Request.RequestUri.GetLeftPart(UriPartial.Authority);
            var mediaUrlResolverService = new UmbracoContextFullUrlResolverService(UmbracoContext);
            var service = new ImagePickerApiService(Umbraco, mediaUrlResolverService);
            IPublishedContent root;

            if (service.TryFindRoot(id, out root) == false)
            {
                if (root == null)
                {
                    return(NotFound());
                }
                else
                {
                    return(StatusCode(HttpStatusCode.UnsupportedMediaType));
                }
            }

            var images = service.GetImages(root);

            return(Ok(images));
        }
Exemplo n.º 2
0
        protected void AddNodesToQueue(IEnumerable <XElement> nodes, string type)
        {
            if (_searchConfiguration.EnableIndexing == false)
            {
                LogHelper.Info <ElasticPublishedContentMediaIndexer>("Skipping AddNodesToQueue. EnableIndexing is configured to disabled.");
                return;
            }

            var umbracoContext = GetUmbracoContext();

            if (umbracoContext == null)
            {
                LogHelper.Info <ElasticPublishedContentMediaIndexer>(() => "Skipping AddNodesToQueue as a valid UmbracoContext was unobtainable.");
                return;
            }

            var fullUrlResolverService = new UmbracoContextFullUrlResolverService(umbracoContext);

            using (var searchIndexingQueueService = new SearchIndexingQueueService(_searchConfiguration))
            {
                LogHelper.Info <ElasticPublishedContentMediaIndexer>("AddNodesToQueue: {0} node(s) for index type {1}.", () => nodes.Count(), () => type);

                foreach (var node in nodes)
                {
                    DataService.LogService.AddVerboseLog((int)node.Attribute("id"), string.Format("AddSingleNodeToIndex with type: {0}", type));

                    int nodeId = int.Parse(node.Attribute(XName.Get("id")).Value);

                    var values = GetDataToIndex(node, type);
                    //raise the event and assign the value to the returned data from the event
                    var indexingNodeDataArgs = new IndexingNodeDataEventArgs(node, nodeId, values, type);
                    OnGatheringNodeData(indexingNodeDataArgs);
                    values = indexingNodeDataArgs.Fields;

                    values.TryGetValue("nodeName", out string nodeName);
                    string publishDate = string.Empty;
                    if (!values.TryGetValue("reviewedDate", out publishDate))
                    {
                        if (!values.TryGetValue("publishedDate", out publishDate))
                        {
                            if (values.TryGetValue("updateDate", out publishDate))
                            {
                                // Defaulted to updateDate property
                            }
                        }
                    }

                    var document = new SearchIndexDocumentModel()
                    {
                        NodeId    = nodeId,
                        Site      = _site,
                        Published = DateTime.Parse(publishDate),
                        Title     = nodeName
                    };

                    // Determine if this is content or media
                    if (string.Equals(type, UmbracoExamine.IndexTypes.Content, StringComparison.OrdinalIgnoreCase))
                    {
                        var url = fullUrlResolverService.ResolveContentFullUrlById(nodeId);

                        // Get content based on content fields in order of priority
                        var contentBuilder = new StringBuilder();

                        foreach (var contentField in values.Where(x => IndexerData.UserFields.Select(y => y.Name).Contains(x.Key)))
                        {
                            var contentFieldValue = contentField.Value;
                            // Check if it has a value and append it
                            if (string.IsNullOrEmpty(contentFieldValue) == false)
                            {
                                if (contentFieldValue.DetectIsJson() && JsonUtility.TryParseJson(contentFieldValue, out object parsedJson))
                                {
                                    var processedJsonValue = ProcessJsonValue(parsedJson);

                                    if (string.IsNullOrEmpty(processedJsonValue) == false)
                                    {
                                        contentBuilder.AppendLine(processedJsonValue);
                                    }
                                }
                                else
                                {
                                    var sanitisedValue = contentFieldValue.StripHtml().Trim();

                                    if (string.IsNullOrWhiteSpace(sanitisedValue) == false)
                                    {
                                        contentBuilder.AppendLine(sanitisedValue);
                                    }
                                }
                            }
                        }


                        string content = contentBuilder.ToString().Trim();

                        document.Url     = url;
                        document.Content = content;

                        // index the node
                        searchIndexingQueueService.QueueUpsert(document);
                    }
                    else if (string.Equals(type, UmbracoExamine.IndexTypes.Media, StringComparison.OrdinalIgnoreCase))
                    {
                        var fileExtension = node.Elements().FirstOrDefault(x =>
                        {
                            if (x.Attribute("alias") != null)
                            {
                                return((string)x.Attribute("alias") == this.UmbracoExtensionProperty);
                            }
                            else
                            {
                                return(x.Name == this.UmbracoExtensionProperty);
                            }
                        });

                        if (HasNode(fileExtension) == false)
                        {
                            LogHelper.Warn <ElasticPublishedContentMediaIndexer>("Media name " + nodeName + " with ID " + nodeId + " has not been pushed up to SQS. Reason: " + UmbracoExtensionProperty + " value was not present.");
                            continue;
                        }

                        if (!SupportedExtensions.Contains(fileExtension.Value, StringComparer.OrdinalIgnoreCase))
                        {
                            LogHelper.Info <ElasticPublishedContentMediaIndexer>("Media name " + nodeName + " with ID " + nodeId + " has not been pushed up to SQS. Reason: File extension, " + fileExtension.Value + ", is not supported.");
                            continue;
                        }

                        var filePath = node.Elements().FirstOrDefault(x =>
                        {
                            if (x.Attribute("alias") != null)
                            {
                                return((string)x.Attribute("alias") == this.UmbracoFileProperty);
                            }
                            else
                            {
                                return(x.Name == this.UmbracoFileProperty);
                            }
                        });


                        if (HasNode(filePath) == false)
                        {
                            LogHelper.Warn <ElasticPublishedContentMediaIndexer>("Media name " + nodeName + " with ID " + nodeId + " has not been pushed up to SQS. Reason: " + UmbracoFileProperty + " value was not present.");
                            continue;
                        }

                        //get the file path from the data service
                        var fullPath = this.DataService.MapPath((string)filePath);

                        if (System.IO.File.Exists(fullPath) == false)
                        {
                            LogHelper.Warn <ElasticPublishedContentMediaIndexer>("Media name " + nodeName + " with ID " + nodeId + " has not been pushed up to SQS. Reason: Physical file does not exist.");
                            continue;
                        }

                        var fileInfo = new FileInfo(fullPath);
                        var url      = fullUrlResolverService.ResolveMediaFullUrl(filePath.Value);
                        // index the node

                        var pdf        = System.IO.File.ReadAllBytes(fullPath);
                        var pdfEncoded = Convert.ToBase64String(pdf);

                        document.Url               = url;
                        document.Content           = "Umbraco Media File";
                        document.FileBase64Encoded = pdfEncoded;
                        document.FileExtension     = fileInfo.Extension;
                        document.FileSizeInBytes   = fileInfo.Length;

                        searchIndexingQueueService.QueueUpsert(document);
                    }
                }
            }
        }