mirror of
https://github.com/azaion/annotations.git
synced 2026-04-22 13:06:31 +00:00
fix editing non-timed annotations
This commit is contained in:
@@ -9,10 +9,15 @@ using Size = System.Windows.Size;
|
||||
|
||||
namespace Azaion.Annotator;
|
||||
|
||||
public delegate void ThumbnailsUpdatedEventHandler(double thumbnailsPercentage);
|
||||
|
||||
public class GalleryManager(Config config, ILogger<GalleryManager> logger) : IGalleryManager
|
||||
{
|
||||
public int ThumbnailsCount { get; set; }
|
||||
public int ImagesCount { get; set; }
|
||||
public event ThumbnailsUpdatedEventHandler ThumbnailsUpdate;
|
||||
private const int UPDATE_STEP = 20;
|
||||
private readonly SemaphoreSlim _updateLock = new(1);
|
||||
|
||||
public double ThumbnailsPercentage { get; set; }
|
||||
|
||||
private DirectoryInfo? _thumbnailsDirectory;
|
||||
private DirectoryInfo ThumbnailsDirectory
|
||||
@@ -30,35 +35,54 @@ public class GalleryManager(Config config, ILogger<GalleryManager> logger) : IGa
|
||||
}
|
||||
}
|
||||
|
||||
public void ClearThumbnails()
|
||||
{
|
||||
foreach(var file in new DirectoryInfo(config.ThumbnailsDirectory).GetFiles())
|
||||
file.Delete();
|
||||
}
|
||||
|
||||
public async Task RefreshThumbnails()
|
||||
{
|
||||
var prefixLen = Config.ThumbnailPrefix.Length;
|
||||
|
||||
var thumbnails = ThumbnailsDirectory.GetFiles()
|
||||
.Select(x => Path.GetFileNameWithoutExtension(x.Name)[..^prefixLen])
|
||||
.GroupBy(x => x)
|
||||
.Select(gr => gr.Key)
|
||||
.ToHashSet();
|
||||
ThumbnailsCount = thumbnails.Count;
|
||||
|
||||
var files = new DirectoryInfo(config.ImagesDirectory).GetFiles();
|
||||
ImagesCount = files.Length;
|
||||
|
||||
foreach (var img in files)
|
||||
await _updateLock.WaitAsync();
|
||||
try
|
||||
{
|
||||
var imgName = Path.GetFileNameWithoutExtension(img.Name);
|
||||
if (thumbnails.Contains(imgName))
|
||||
continue;
|
||||
try
|
||||
var prefixLen = Config.ThumbnailPrefix.Length;
|
||||
|
||||
var thumbnails = ThumbnailsDirectory.GetFiles()
|
||||
.Select(x => Path.GetFileNameWithoutExtension(x.Name)[..^prefixLen])
|
||||
.GroupBy(x => x)
|
||||
.Select(gr => gr.Key)
|
||||
.ToHashSet();
|
||||
var thumbnailsCount = thumbnails.Count;
|
||||
|
||||
var files = new DirectoryInfo(config.ImagesDirectory).GetFiles();
|
||||
var imagesCount = files.Length;
|
||||
|
||||
for (int i = 0; i < files.Length; i++)
|
||||
{
|
||||
await CreateThumbnail(img.FullName);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
logger.LogError(e, $"Failed to generate thumbnail for {img.Name}");
|
||||
var img = files[i];
|
||||
ThumbnailsPercentage = imagesCount == 0 ? 0 : Math.Min(100, i * 100 / (double)imagesCount);
|
||||
var imgName = Path.GetFileNameWithoutExtension(img.Name);
|
||||
if (i % UPDATE_STEP == 0)
|
||||
ThumbnailsUpdate?.Invoke(ThumbnailsPercentage);
|
||||
if (thumbnails.Contains(imgName))
|
||||
continue;
|
||||
try
|
||||
{
|
||||
await CreateThumbnail(img.FullName);
|
||||
thumbnailsCount++;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
logger.LogError(e, $"Failed to generate thumbnail for {img.Name}! Error: {e.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
ThumbnailsCount++;
|
||||
await Task.Delay(10000);
|
||||
}
|
||||
finally
|
||||
{
|
||||
_updateLock.Release();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -152,8 +176,9 @@ public class GalleryManager(Config config, ILogger<GalleryManager> logger) : IGa
|
||||
|
||||
public interface IGalleryManager
|
||||
{
|
||||
int ThumbnailsCount { get; set; }
|
||||
int ImagesCount { get; set; }
|
||||
event ThumbnailsUpdatedEventHandler ThumbnailsUpdate;
|
||||
double ThumbnailsPercentage { get; set; }
|
||||
Task CreateThumbnail(string imgPath);
|
||||
Task RefreshThumbnails();
|
||||
void ClearThumbnails();
|
||||
}
|
||||
Reference in New Issue
Block a user