2023-02-15 12:27:36 +01:00
|
|
|
|
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
|
|
|
|
|
|
{
|
2022-04-05 18:07:05 +02:00
|
|
|
|
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);
|
|
|
|
|
|
|
2023-02-15 12:27:36 +01:00
|
|
|
|
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);
|
|
|
|
|
|
|
2022-04-05 18:07:05 +02:00
|
|
|
|
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();
|
|
|
|
|
|
}
|
2022-04-05 18:07:05 +02:00
|
|
|
|
|
|
|
|
|
|
public static void Clear()
|
|
|
|
|
|
{
|
|
|
|
|
|
cache.Clear();
|
|
|
|
|
|
}
|
2021-08-23 16:50:18 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|