Files
mixtape/zero.Web/ViewHelpers/ZeroMediaHelper.cs
T

129 lines
2.9 KiB
C#
Raw Normal View History

2021-09-30 12:28:21 +02:00
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
2021-09-30 12:28:21 +02:00
using zero.Core.Database;
using zero.Core.Entities;
using zero.Core.Extensions;
namespace zero.Web.ViewHelpers
{
public class ZeroMediaHelper : IZeroMediaHelper
{
2021-10-28 12:11:07 +02:00
IZeroStore Store;
2021-09-30 12:28:21 +02:00
public IZeroMediaHelper Core { get; private set; }
2021-10-28 12:11:07 +02:00
protected bool Global { get; set; }
/// <summary>
/// Media cache for repetitive queries within an HTTP request
/// </summary>
2021-07-16 14:52:41 +02:00
ConcurrentDictionary<string, Media> Cache { get; set; } = new();
2021-10-28 12:11:07 +02:00
public ZeroMediaHelper(IZeroStore store, bool global = false)
{
2021-10-28 12:11:07 +02:00
Store = store;
2021-09-30 12:28:21 +02:00
2021-10-28 12:11:07 +02:00
if (!global)
2021-09-30 12:28:21 +02:00
{
2021-10-28 12:11:07 +02:00
Core = new ZeroMediaHelper(store, true);
2021-09-30 12:28:21 +02:00
}
}
/// <inheritdoc />
2021-05-04 17:23:52 +02:00
public async Task<Media> GetById(string id)
{
if (id.IsNullOrEmpty())
{
return null;
}
2021-05-04 17:23:52 +02:00
if (!Cache.TryGetValue(id, out Media media))
{
2021-10-28 12:11:07 +02:00
media = await Store.Session(Global).LoadAsync<Media>(id);
2021-07-16 14:52:41 +02:00
Cache.TryAdd(id, media);
}
return media;
}
/// <inheritdoc />
2021-05-04 17:23:52 +02:00
public async Task<Dictionary<string, Media>> GetByIds(string[] ids)
{
HashSet<string> remoteIds = new HashSet<string>();
2021-05-04 17:23:52 +02:00
Dictionary<string, Media> items = new Dictionary<string, Media>();
foreach (string id in ids)
{
2021-05-04 17:23:52 +02:00
if (Cache.TryGetValue(id, out Media media))
{
2021-07-16 14:52:41 +02:00
items.TryAdd(id, media);
}
else
{
remoteIds.Add(id);
}
}
if (remoteIds.Count > 0)
{
2021-10-28 12:11:07 +02:00
Dictionary<string, Media> remoteItems = await Store.Session(Global).LoadAsync<Media>(remoteIds);
foreach (var item in remoteItems)
{
2021-07-16 14:52:41 +02:00
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-05-04 17:23:52 +02:00
Media media = await GetById(id);
2020-11-25 16:04:00 +01:00
return media?.Source.TrimStart("url://");
}
/// <inheritdoc />
public async Task<Dictionary<string, string>> GetUrls(string[] ids, bool isAbsolute = false)
{
2021-05-04 17:23:52 +02:00
Dictionary<string, Media> medias = await GetByIds(ids);
2020-11-25 16:04:00 +01:00
return medias.ToDictionary(x => x.Key, x => x.Value?.Source.TrimStart("url://"));
}
}
public interface IZeroMediaHelper
{
2021-09-30 12:28:21 +02:00
IZeroMediaHelper Core { get; }
/// <summary>
/// Get media by Id
/// </summary>
2021-05-04 17:23:52 +02:00
Task<Media> GetById(string id);
/// <summary>
/// Get media items by Ids
/// </summary>
2021-05-04 17:23:52 +02:00
Task<Dictionary<string, Media>> GetByIds(string[] ids);
/// <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);
}
}