2021-11-27 18:09:27 +01:00
|
|
|
//using Microsoft.AspNetCore.Http;
|
|
|
|
|
//using System.Collections.Generic;
|
|
|
|
|
//using System.Linq;
|
|
|
|
|
//using System.Threading.Tasks;
|
|
|
|
|
//using zero.Extensions;
|
|
|
|
|
//using zero.Media;
|
2021-11-23 11:56:42 +01:00
|
|
|
|
2021-11-27 18:09:27 +01:00
|
|
|
//namespace zero.Web.ViewHelpers;
|
2021-11-23 11:56:42 +01:00
|
|
|
|
2021-11-27 18:09:27 +01:00
|
|
|
//public class ZeroPageHelper : IZeroPageHelper
|
|
|
|
|
//{
|
|
|
|
|
// HttpContext HttpContext;
|
2021-11-23 11:56:42 +01:00
|
|
|
|
2021-11-27 18:09:27 +01:00
|
|
|
// IMediaStore Media;
|
2021-11-23 11:56:42 +01:00
|
|
|
|
2021-11-27 18:09:27 +01:00
|
|
|
// /// <summary>
|
|
|
|
|
// /// Media cache for repetitive queries within an HTTP request
|
|
|
|
|
// /// </summary>
|
|
|
|
|
// Dictionary<string, Media> Cache { get; set; } = new Dictionary<string, Media.Media>();
|
2021-11-23 11:56:42 +01:00
|
|
|
|
|
|
|
|
|
2021-11-27 18:09:27 +01:00
|
|
|
// public ZeroPageHelper(IHttpContextAccessor httpContextAccessor, IMediaStore media)
|
|
|
|
|
// {
|
|
|
|
|
// HttpContext = httpContextAccessor.HttpContext;
|
|
|
|
|
// Media = media;
|
|
|
|
|
// }
|
2021-11-23 11:56:42 +01:00
|
|
|
|
|
|
|
|
|
2021-11-27 18:09:27 +01:00
|
|
|
// /// <inheritdoc />
|
|
|
|
|
// public async Task<Media> GetById(string id)
|
|
|
|
|
// {
|
|
|
|
|
// if (id.IsNullOrEmpty())
|
|
|
|
|
// {
|
|
|
|
|
// return null;
|
|
|
|
|
// }
|
2021-11-23 11:56:42 +01:00
|
|
|
|
2021-11-27 18:09:27 +01:00
|
|
|
// if (!Cache.TryGetValue(id, out Media media))
|
|
|
|
|
// {
|
|
|
|
|
// media = await Media.Load(id);
|
|
|
|
|
// Cache.Add(id, media);
|
|
|
|
|
// }
|
2021-11-23 11:56:42 +01:00
|
|
|
|
2021-11-27 18:09:27 +01:00
|
|
|
// return media;
|
|
|
|
|
// }
|
2021-11-23 11:56:42 +01:00
|
|
|
|
|
|
|
|
|
2021-11-27 18:09:27 +01:00
|
|
|
// /// <inheritdoc />
|
|
|
|
|
// public async Task<Dictionary<string, Media>> GetByIds(string[] ids)
|
|
|
|
|
// {
|
|
|
|
|
// HashSet<string> remoteIds = new HashSet<string>();
|
|
|
|
|
// Dictionary<string, Media> items = new Dictionary<string, Media>();
|
2021-11-23 11:56:42 +01:00
|
|
|
|
2021-11-27 18:09:27 +01:00
|
|
|
// foreach (string id in ids)
|
|
|
|
|
// {
|
|
|
|
|
// if (Cache.TryGetValue(id, out Media media))
|
|
|
|
|
// {
|
|
|
|
|
// items.Add(id, media);
|
|
|
|
|
// }
|
|
|
|
|
// else
|
|
|
|
|
// {
|
|
|
|
|
// remoteIds.Add(id);
|
|
|
|
|
// }
|
|
|
|
|
// }
|
2021-11-23 11:56:42 +01:00
|
|
|
|
2021-11-27 18:09:27 +01:00
|
|
|
// if (remoteIds.Count > 0)
|
|
|
|
|
// {
|
|
|
|
|
// Dictionary<string, Media> remoteItems = await Media.Load(remoteIds);
|
2021-11-23 11:56:42 +01:00
|
|
|
|
2021-11-27 18:09:27 +01:00
|
|
|
// foreach (var item in remoteItems)
|
|
|
|
|
// {
|
|
|
|
|
// items.Add(item.Key, item.Value);
|
|
|
|
|
// Cache.Add(item.Key, item.Value);
|
|
|
|
|
// }
|
|
|
|
|
// }
|
2021-11-23 11:56:42 +01:00
|
|
|
|
2021-11-27 18:09:27 +01:00
|
|
|
// return items;
|
|
|
|
|
// }
|
2021-11-23 11:56:42 +01:00
|
|
|
|
|
|
|
|
|
2021-11-27 18:09:27 +01:00
|
|
|
// /// <inheritdoc />
|
|
|
|
|
// public async Task<string> GetUrl(string id, bool isAbsolute = false)
|
|
|
|
|
// {
|
|
|
|
|
// Media media = await GetById(id);
|
|
|
|
|
// return media?.Source;
|
|
|
|
|
// }
|
2021-11-23 11:56:42 +01:00
|
|
|
|
|
|
|
|
|
2021-11-27 18:09:27 +01:00
|
|
|
// /// <inheritdoc />
|
|
|
|
|
// public async Task<Dictionary<string, string>> GetUrls(string[] ids, bool isAbsolute = false)
|
|
|
|
|
// {
|
|
|
|
|
// Dictionary<string, Media> medias = await GetByIds(ids);
|
|
|
|
|
// return medias.ToDictionary(x => x.Key, x => x.Value?.Source);
|
|
|
|
|
// }
|
|
|
|
|
//}
|
2021-11-23 11:56:42 +01:00
|
|
|
|
|
|
|
|
|
2021-11-27 18:09:27 +01:00
|
|
|
//public interface IZeroPageHelper
|
|
|
|
|
//{
|
|
|
|
|
// /// <summary>
|
|
|
|
|
// /// Get media by Id
|
|
|
|
|
// /// </summary>
|
|
|
|
|
// Task<Media> GetById(string id);
|
2021-11-23 11:56:42 +01:00
|
|
|
|
2021-11-27 18:09:27 +01:00
|
|
|
// /// <summary>
|
|
|
|
|
// /// Get media items by Ids
|
|
|
|
|
// /// </summary>
|
|
|
|
|
// Task<Dictionary<string, Media>> GetByIds(string[] ids);
|
2021-11-23 11:56:42 +01:00
|
|
|
|
2021-11-27 18:09:27 +01:00
|
|
|
// /// <summary>
|
|
|
|
|
// /// Get source for a media item
|
|
|
|
|
// /// </summary>
|
|
|
|
|
// Task<string> GetUrl(string id, bool isAbsolute = false);
|
2021-11-23 11:56:42 +01:00
|
|
|
|
2021-11-27 18:09:27 +01:00
|
|
|
// /// <summary>
|
|
|
|
|
// /// Get source for media items
|
|
|
|
|
// /// </summary>
|
|
|
|
|
// Task<Dictionary<string, string>> GetUrls(string[] ids, bool isAbsolute = false);
|
|
|
|
|
//}
|