2021-11-23 11:56:42 +01:00
|
|
|
using System.Collections.Concurrent;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using zero.Extensions;
|
2022-01-08 00:24:04 +01:00
|
|
|
using zero.Media;
|
2021-11-23 11:56:42 +01:00
|
|
|
using zero.Persistence;
|
|
|
|
|
|
|
|
|
|
namespace zero.Web.ViewHelpers;
|
|
|
|
|
|
|
|
|
|
public class ZeroMediaHelper : IZeroMediaHelper
|
|
|
|
|
{
|
|
|
|
|
IZeroStore Store;
|
|
|
|
|
|
|
|
|
|
public IZeroMediaHelper Core { get; private set; }
|
|
|
|
|
|
2022-01-08 00:24:04 +01:00
|
|
|
protected IMediaManagement MediaManagement { get; private set; }
|
|
|
|
|
|
2021-11-23 11:56:42 +01:00
|
|
|
protected bool Global { get; set; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Media cache for repetitive queries within an HTTP request
|
|
|
|
|
/// </summary>
|
2021-11-27 18:09:27 +01:00
|
|
|
ConcurrentDictionary<string, Media.Media> Cache { get; set; } = new();
|
2021-11-23 11:56:42 +01:00
|
|
|
|
|
|
|
|
|
2022-01-08 00:24:04 +01:00
|
|
|
public ZeroMediaHelper(IZeroStore store, IMediaManagement mediaManagement, bool global = false)
|
2021-11-23 11:56:42 +01:00
|
|
|
{
|
|
|
|
|
Store = store;
|
2022-01-08 00:24:04 +01:00
|
|
|
MediaManagement = mediaManagement;
|
2021-11-23 11:56:42 +01:00
|
|
|
|
|
|
|
|
if (!global)
|
|
|
|
|
{
|
2022-01-08 00:24:04 +01:00
|
|
|
Core = new ZeroMediaHelper(store, MediaManagement, true);
|
2021-11-23 11:56:42 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2021-11-27 18:09:27 +01:00
|
|
|
public async Task<Media.Media> GetById(string id)
|
2021-11-23 11:56:42 +01:00
|
|
|
{
|
|
|
|
|
if (id.IsNullOrEmpty())
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-27 18:09:27 +01:00
|
|
|
if (!Cache.TryGetValue(id, out Media.Media media))
|
2021-11-23 11:56:42 +01:00
|
|
|
{
|
2021-11-27 18:09:27 +01:00
|
|
|
media = await Store.Session(Global).LoadAsync<Media.Media>(id);
|
2021-11-23 11:56:42 +01:00
|
|
|
Cache.TryAdd(id, media);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return media;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2021-11-27 18:09:27 +01:00
|
|
|
public async Task<Dictionary<string, Media.Media>> GetByIds(string[] ids)
|
2021-11-23 11:56:42 +01:00
|
|
|
{
|
|
|
|
|
HashSet<string> remoteIds = new HashSet<string>();
|
2021-11-27 18:09:27 +01:00
|
|
|
Dictionary<string, Media.Media> items = new Dictionary<string, Media.Media>();
|
2021-11-23 11:56:42 +01:00
|
|
|
|
|
|
|
|
foreach (string id in ids)
|
|
|
|
|
{
|
2021-11-27 18:09:27 +01:00
|
|
|
if (Cache.TryGetValue(id, out Media.Media media))
|
2021-11-23 11:56:42 +01:00
|
|
|
{
|
|
|
|
|
items.TryAdd(id, media);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
remoteIds.Add(id);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (remoteIds.Count > 0)
|
|
|
|
|
{
|
2021-11-27 18:09:27 +01:00
|
|
|
Dictionary<string, Media.Media> remoteItems = await Store.Session(Global).LoadAsync<Media.Media>(remoteIds);
|
2021-11-23 11:56:42 +01:00
|
|
|
|
|
|
|
|
foreach (var item in remoteItems)
|
|
|
|
|
{
|
|
|
|
|
items.TryAdd(item.Key, item.Value);
|
|
|
|
|
Cache.TryAdd(item.Key, item.Value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return items;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public async Task<string> GetUrl(string id, bool isAbsolute = false)
|
|
|
|
|
{
|
2021-11-27 18:09:27 +01:00
|
|
|
Media.Media media = await GetById(id);
|
2022-01-08 00:24:04 +01:00
|
|
|
|
|
|
|
|
if (media == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (media.Path.StartsWith("url://"))
|
|
|
|
|
{
|
|
|
|
|
return media.Path.TrimStart("url://");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return MediaManagement.GetPublicFilePath(media);
|
2021-11-23 11:56:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public async Task<Dictionary<string, string>> GetUrls(string[] ids, bool isAbsolute = false)
|
|
|
|
|
{
|
2022-01-08 00:24:04 +01:00
|
|
|
Dictionary<string, string> result = new();
|
2021-11-27 18:09:27 +01:00
|
|
|
Dictionary<string, Media.Media> medias = await GetByIds(ids);
|
2022-01-08 00:24:04 +01:00
|
|
|
|
|
|
|
|
foreach ((string id, Media.Media media) in medias)
|
|
|
|
|
{
|
|
|
|
|
if (media == null)
|
|
|
|
|
{
|
|
|
|
|
result.Add(id, null);
|
|
|
|
|
}
|
|
|
|
|
else if (media.Path.StartsWith("url://"))
|
|
|
|
|
{
|
|
|
|
|
result.Add(id, media.Path.TrimStart("url://"));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
result.Add(id, MediaManagement.GetPublicFilePath(media));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
2021-11-23 11:56:42 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public interface IZeroMediaHelper
|
|
|
|
|
{
|
|
|
|
|
IZeroMediaHelper Core { get; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Get media by Id
|
|
|
|
|
/// </summary>
|
2021-11-27 18:09:27 +01:00
|
|
|
Task<Media.Media> GetById(string id);
|
2021-11-23 11:56:42 +01:00
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Get media items by Ids
|
|
|
|
|
/// </summary>
|
2021-11-27 18:09:27 +01:00
|
|
|
Task<Dictionary<string, Media.Media>> GetByIds(string[] ids);
|
2021-11-23 11:56:42 +01:00
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Get source for a media item
|
|
|
|
|
/// </summary>
|
|
|
|
|
Task<string> GetUrl(string id, bool isAbsolute = false);
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Get source for media items
|
|
|
|
|
/// </summary>
|
|
|
|
|
Task<Dictionary<string, string>> GetUrls(string[] ids, bool isAbsolute = false);
|
|
|
|
|
}
|