Files
flowframes/CodeLegacy/Media/GetMediaResolutionCached.cs

55 lines
1.6 KiB
C#
Raw Normal View History

using System.Collections.Generic;
2021-08-23 16:50:18 +02:00
using System.Drawing;
using System.IO;
using System.Linq;
2021-08-23 16:50:18 +02:00
using System.Threading.Tasks;
using Flowframes.Data;
using Flowframes.IO;
namespace Flowframes.Media
{
class GetMediaResolutionCached
{
private static Dictionary<QueryInfo, Size> cache = new Dictionary<QueryInfo, Size>();
2021-08-23 16:50:18 +02:00
public static async Task<Size> GetSizeAsync(string path)
{
long filesize = IoUtils.GetPathSize(path);
2021-09-14 18:18:41 +02:00
QueryInfo hash = new QueryInfo(path, filesize);
2021-08-23 16:50:18 +02:00
if (filesize > 0 && CacheContains(hash))
{
Size cachedVal = GetFromCache(hash);
Logger.Log($"Resolution of '{Path.GetFileName(path)}': {cachedVal.Width}x{cachedVal.Height} [Cached]", true);
return cachedVal;
2021-08-23 16:50:18 +02:00
}
Size size;
size = await IoUtils.GetVideoOrFramesRes(path);
if(size.Width > 0 && size.Height > 0)
{
cache.Add(hash, size);
}
2021-08-23 16:50:18 +02:00
Logger.Log($"Resolution of '{Path.GetFileName(path)}': {size.Width}x{size.Height}", true);
2021-08-23 16:50:18 +02:00
return size;
}
2021-09-14 18:18:41 +02:00
private static bool CacheContains(QueryInfo hash)
2021-08-23 16:50:18 +02:00
{
return cache.Any(entry => entry.Key.path == hash.path && entry.Key.filesize == hash.filesize);
2021-08-23 16:50:18 +02:00
}
2021-09-14 18:18:41 +02:00
private static Size GetFromCache(QueryInfo hash)
2021-08-23 16:50:18 +02:00
{
return cache.Where(entry => entry.Key.path == hash.path && entry.Key.filesize == hash.filesize).Select(entry => entry.Value).FirstOrDefault();
2021-08-23 16:50:18 +02:00
}
public static void Clear()
{
cache.Clear();
}
2021-08-23 16:50:18 +02:00
}
}