Files
flowframes/Flowframes/Media/GetMediaResolutionCached.cs

66 lines
1.8 KiB
C#
Raw Permalink Normal View History

using System.Collections.Generic;
2021-08-23 16:50:18 +02:00
using System.Drawing;
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)
{
Logger.Log($"Getting media resolution ({path})", true);
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))
{
Logger.Log($"Cache contains this hash, using cached value.", true);
return GetFromCache(hash);
}
else
{
Logger.Log($"Hash not cached, reading resolution.", true);
}
Size size;
size = await IoUtils.GetVideoOrFramesRes(path);
if(size.Width > 0 && size.Height > 0)
{
Logger.Log($"Adding hash with value {size} to cache.", true);
cache.Add(hash, size);
}
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
{
2021-09-14 18:18:41 +02:00
foreach (KeyValuePair<QueryInfo, Size> entry in cache)
2021-08-23 16:50:18 +02:00
if (entry.Key.path == hash.path && entry.Key.filesize == hash.filesize)
return true;
return false;
}
2021-09-14 18:18:41 +02:00
private static Size GetFromCache(QueryInfo hash)
2021-08-23 16:50:18 +02:00
{
2021-09-14 18:18:41 +02:00
foreach (KeyValuePair<QueryInfo, Size> entry in cache)
2021-08-23 16:50:18 +02:00
if (entry.Key.path == hash.path && entry.Key.filesize == hash.filesize)
return entry.Value;
return new Size();
}
public static void Clear()
{
cache.Clear();
}
2021-08-23 16:50:18 +02:00
}
}