2016-05-27 14:26:28 +02:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Xml.XPath;
|
2019-04-12 16:05:43 +02:00
|
|
|
|
using Umbraco.Core;
|
2016-05-30 19:54:36 +02:00
|
|
|
|
using Umbraco.Core.Cache;
|
2016-05-27 14:26:28 +02:00
|
|
|
|
using Umbraco.Core.Models.PublishedContent;
|
|
|
|
|
|
using Umbraco.Core.Xml;
|
|
|
|
|
|
using Umbraco.Core.Xml.XPath;
|
|
|
|
|
|
using Umbraco.Web.PublishedCache.NuCache.Navigable;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Web.PublishedCache.NuCache
|
|
|
|
|
|
{
|
2016-11-05 12:22:57 +01:00
|
|
|
|
internal class MediaCache : PublishedCacheBase, IPublishedMediaCache, INavigableData, IDisposable
|
2016-05-27 14:26:28 +02:00
|
|
|
|
{
|
2017-07-12 14:09:31 +02:00
|
|
|
|
private readonly ContentStore.Snapshot _snapshot;
|
2019-06-07 11:15:58 +02:00
|
|
|
|
private readonly IVariationContextAccessor _variationContextAccessor;
|
2016-05-27 14:26:28 +02:00
|
|
|
|
|
|
|
|
|
|
#region Constructors
|
|
|
|
|
|
|
2019-06-07 11:15:58 +02:00
|
|
|
|
public MediaCache(bool previewDefault, ContentStore.Snapshot snapshot, IVariationContextAccessor variationContextAccessor)
|
2016-05-27 14:26:28 +02:00
|
|
|
|
: base(previewDefault)
|
|
|
|
|
|
{
|
|
|
|
|
|
_snapshot = snapshot;
|
2019-06-07 11:15:58 +02:00
|
|
|
|
_variationContextAccessor = variationContextAccessor;
|
2016-05-27 14:26:28 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region Get, Has
|
|
|
|
|
|
|
|
|
|
|
|
public override IPublishedContent GetById(bool preview, int contentId)
|
|
|
|
|
|
{
|
2017-12-07 13:22:32 +01:00
|
|
|
|
// ignore preview, there's only draft for media
|
2016-05-27 14:26:28 +02:00
|
|
|
|
var n = _snapshot.Get(contentId);
|
2019-01-28 14:15:47 +01:00
|
|
|
|
return n?.PublishedModel;
|
2016-05-27 14:26:28 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-11-03 10:31:44 +01:00
|
|
|
|
public override IPublishedContent GetById(bool preview, Guid contentId)
|
|
|
|
|
|
{
|
2017-12-07 13:22:32 +01:00
|
|
|
|
// ignore preview, there's only draft for media
|
2016-11-05 12:22:57 +01:00
|
|
|
|
var n = _snapshot.Get(contentId);
|
2019-01-28 14:15:47 +01:00
|
|
|
|
return n?.PublishedModel;
|
2016-11-03 10:31:44 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-04-12 16:05:43 +02:00
|
|
|
|
public override IPublishedContent GetById(bool preview, Udi contentId)
|
|
|
|
|
|
{
|
|
|
|
|
|
var guidUdi = contentId as GuidUdi;
|
|
|
|
|
|
if (guidUdi == null)
|
|
|
|
|
|
throw new ArgumentException($"Udi must be of type {typeof(GuidUdi).Name}.", nameof(contentId));
|
|
|
|
|
|
|
|
|
|
|
|
if (guidUdi.EntityType != Constants.UdiEntityType.Media)
|
|
|
|
|
|
throw new ArgumentException($"Udi entity type must be \"{Constants.UdiEntityType.Media}\".", nameof(contentId));
|
|
|
|
|
|
|
|
|
|
|
|
// ignore preview, there's only draft for media
|
|
|
|
|
|
var n = _snapshot.Get(guidUdi.Guid);
|
|
|
|
|
|
return n?.PublishedModel;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-05-27 14:26:28 +02:00
|
|
|
|
public override bool HasById(bool preview, int contentId)
|
|
|
|
|
|
{
|
|
|
|
|
|
var n = _snapshot.Get(contentId);
|
|
|
|
|
|
return n != null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-06-07 11:15:58 +02:00
|
|
|
|
IEnumerable<IPublishedContent> INavigableData.GetAtRoot(bool preview) => GetAtRoot(preview);
|
2016-05-27 14:26:28 +02:00
|
|
|
|
|
2019-06-07 11:15:58 +02:00
|
|
|
|
public override IEnumerable<IPublishedContent> GetAtRoot(bool preview, string culture = null)
|
2016-05-27 14:26:28 +02:00
|
|
|
|
{
|
2019-06-07 11:15:58 +02:00
|
|
|
|
// handle context culture for variant
|
|
|
|
|
|
if (culture == null)
|
|
|
|
|
|
culture = _variationContextAccessor?.VariationContext?.Culture ?? "";
|
2016-05-27 14:26:28 +02:00
|
|
|
|
|
2019-06-07 11:15:58 +02:00
|
|
|
|
var atRoot = _snapshot.GetAtRoot().Select(x => x.PublishedModel);
|
|
|
|
|
|
return culture == "*" ? atRoot : atRoot.Where(x => x.IsInvariantOrHasCulture(culture));
|
2016-05-27 14:26:28 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override bool HasContent(bool preview)
|
|
|
|
|
|
{
|
|
|
|
|
|
return _snapshot.IsEmpty == false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region XPath
|
|
|
|
|
|
|
|
|
|
|
|
public override IPublishedContent GetSingleByXPath(bool preview, string xpath, XPathVariable[] vars)
|
|
|
|
|
|
{
|
|
|
|
|
|
var navigator = CreateNavigator(preview);
|
|
|
|
|
|
var iterator = navigator.Select(xpath, vars);
|
|
|
|
|
|
return GetSingleByXPath(iterator);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override IPublishedContent GetSingleByXPath(bool preview, XPathExpression xpath, XPathVariable[] vars)
|
|
|
|
|
|
{
|
|
|
|
|
|
var navigator = CreateNavigator(preview);
|
|
|
|
|
|
var iterator = navigator.Select(xpath, vars);
|
|
|
|
|
|
return GetSingleByXPath(iterator);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static IPublishedContent GetSingleByXPath(XPathNodeIterator iterator)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (iterator.MoveNext() == false) return null;
|
|
|
|
|
|
|
|
|
|
|
|
var xnav = iterator.Current as NavigableNavigator;
|
|
|
|
|
|
if (xnav == null) return null;
|
|
|
|
|
|
|
|
|
|
|
|
var xcontent = xnav.UnderlyingObject as NavigableContent;
|
|
|
|
|
|
return xcontent == null ? null : xcontent.InnerContent;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override IEnumerable<IPublishedContent> GetByXPath(bool preview, string xpath, XPathVariable[] vars)
|
|
|
|
|
|
{
|
|
|
|
|
|
var navigator = CreateNavigator(preview);
|
|
|
|
|
|
var iterator = navigator.Select(xpath, vars);
|
|
|
|
|
|
return GetByXPath(iterator);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override IEnumerable<IPublishedContent> GetByXPath(bool preview, XPathExpression xpath, XPathVariable[] vars)
|
|
|
|
|
|
{
|
|
|
|
|
|
var navigator = CreateNavigator(preview);
|
|
|
|
|
|
var iterator = navigator.Select(xpath, vars);
|
|
|
|
|
|
return GetByXPath(iterator);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static IEnumerable<IPublishedContent> GetByXPath(XPathNodeIterator iterator)
|
|
|
|
|
|
{
|
|
|
|
|
|
while (iterator.MoveNext())
|
|
|
|
|
|
{
|
|
|
|
|
|
var xnav = iterator.Current as NavigableNavigator;
|
|
|
|
|
|
if (xnav == null) continue;
|
|
|
|
|
|
|
|
|
|
|
|
var xcontent = xnav.UnderlyingObject as NavigableContent;
|
|
|
|
|
|
if (xcontent == null) continue;
|
|
|
|
|
|
|
|
|
|
|
|
yield return xcontent.InnerContent;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override XPathNavigator CreateNavigator(bool preview)
|
|
|
|
|
|
{
|
|
|
|
|
|
var source = new Source(this, preview);
|
|
|
|
|
|
var navigator = new NavigableNavigator(source);
|
|
|
|
|
|
return navigator;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override XPathNavigator CreateNodeNavigator(int id, bool preview)
|
|
|
|
|
|
{
|
|
|
|
|
|
var source = new Source(this, preview);
|
|
|
|
|
|
var navigator = new NavigableNavigator(source);
|
|
|
|
|
|
return navigator.CloneWithNewRoot(id, 0);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region Content types
|
|
|
|
|
|
|
2019-04-15 13:04:14 +02:00
|
|
|
|
public override IPublishedContentType GetContentType(int id)
|
2016-05-27 14:26:28 +02:00
|
|
|
|
{
|
|
|
|
|
|
return _snapshot.GetContentType(id);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-04-15 13:04:14 +02:00
|
|
|
|
public override IPublishedContentType GetContentType(string alias)
|
2016-05-27 14:26:28 +02:00
|
|
|
|
{
|
|
|
|
|
|
return _snapshot.GetContentType(alias);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region IDisposable
|
|
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
|
{
|
|
|
|
|
|
_snapshot.Dispose();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|