-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileItem.cs
More file actions
75 lines (62 loc) · 2.33 KB
/
FileItem.cs
File metadata and controls
75 lines (62 loc) · 2.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
using System.Linq;
using MetadataExtractor;
using MetadataExtractor.Formats.Exif;
using SkiaSharp;
namespace SelectSight;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using Avalonia.Media.Imaging;
public class FileItem(string fullPath) : INotifyPropertyChanged
{
public string FullPath { get; } = fullPath;
public string Name { get; } = Path.GetFileName(fullPath);
private Bitmap? _thumbnail;
public Bitmap? Thumbnail
{
get => _thumbnail;
private set => SetField(ref _thumbnail, value);
}
public async Task LoadThumbnailAsync()
{
try
{
var extension = Path.GetExtension(FullPath).ToLowerInvariant();
if (extension is ".png" or ".jpg" or ".jpeg" or ".gif" or ".bmp" or ".tiff" or ".tif" or ".ico" or ".nef")
{
// Attempt to load image thumbnail
await using var stream = new FileStream(FullPath, FileMode.Open, FileAccess.Read);
Thumbnail = await ThumbnailUtils.GenerateBitmap(stream);
}
}
catch (Exception ex)
{
Console.WriteLine($"Error loading thumbnail for {FullPath}: {ex.Message}");
}
Thumbnail ??= GetDefaultIcon();
}
private static Bitmap? _defaultIcon;
private static Bitmap GetDefaultIcon()
{
if (_defaultIcon is not null) return _defaultIcon;
var uri = new Uri("avares://SelectSight/Assets/no-photo.png");
_defaultIcon = new Bitmap(Avalonia.Platform.AssetLoader.Open(uri));
return _defaultIcon;
}
public override bool Equals(object? obj)
=> obj is FileItem otherItem && FullPath.Equals(otherItem.FullPath, StringComparison.OrdinalIgnoreCase);
public override int GetHashCode() => FullPath.ToUpperInvariant().GetHashCode();
#region INotifyPropertyChanged
public event PropertyChangedEventHandler? PropertyChanged;
private bool SetField<T>(ref T field, T value, [CallerMemberName] string? propertyName = null)
{
if (EqualityComparer<T>.Default.Equals(field, value)) return false;
field = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
return true;
}
#endregion
}