-
Notifications
You must be signed in to change notification settings - Fork 0
/
LightroomService.cs
100 lines (91 loc) · 4.43 KB
/
LightroomService.cs
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
using System.Runtime.CompilerServices;
using ldam.co.za.lib;
using ldam.co.za.lib.Lightroom;
using ImageInfo = ldam.co.za.lib.ImageInfo;
namespace ldam.co.za.fnapp.Services;
public interface ILightroomService
{
IAsyncEnumerable<AlbumInfo> GetAlbums(CancellationToken cancellationToken = default);
IAsyncEnumerable<ImageInfo> GetImageList(string albumId, CancellationToken cancellationToken = default);
Task<Stream> GetImageStream(string assetId, string size, CancellationToken cancellationToken = default);
}
public class LightroomService : ILightroomService
{
private readonly ILightroomClient lightroomClient;
private readonly Lazy<CatalogResponse> catalog;
public LightroomService(ILightroomClient lightroomClient)
{
this.lightroomClient = lightroomClient;
catalog = new Lazy<CatalogResponse>(() => lightroomClient.GetCatalog().GetAwaiter().GetResult());
}
public async IAsyncEnumerable<AlbumInfo> GetAlbums([EnumeratorCancellation]CancellationToken cancellationToken = default)
{
string after = null;
do
{
cancellationToken.ThrowIfCancellationRequested();
var albumsResponse = await lightroomClient.GetAlbums(this.catalog.Value.Id, after, cancellationToken);
Link next = null;
albumsResponse.Links?.TryGetValue("next", out next);
var afterHref = next?.Href;
after = !string.IsNullOrWhiteSpace(afterHref) ? afterHref[afterHref.IndexOf('=')..] : null;
foreach (var albumResponse in albumsResponse.Resources.Where(x => x.Subtype == "collection"))
{
cancellationToken.ThrowIfCancellationRequested();
yield return new AlbumInfo
{
Id = albumResponse.Id,
Created = DateTime.Parse(albumResponse.Created),
Updated = DateTime.Parse(albumResponse.Updated),
Title = albumResponse.Payload.Name,
ParentId = albumResponse.Payload.Parent?.Id,
};
}
}
while (!string.IsNullOrEmpty(after));
}
public async IAsyncEnumerable<ImageInfo> GetImageList(string albumId, [EnumeratorCancellation] CancellationToken cancellationToken = default)
{
string after = null;
do
{
cancellationToken.ThrowIfCancellationRequested();
var albumAssetResponse = await lightroomClient.GetAlbumAssets(this.catalog.Value.Id, albumId, after, cancellationToken);
Link next = null;
albumAssetResponse.Links?.TryGetValue("next", out next);
var afterHref = next?.Href;
after = !string.IsNullOrWhiteSpace(afterHref) ? afterHref[afterHref.IndexOf('=')..] : null;
foreach (var asset in albumAssetResponse.Resources)
{
cancellationToken.ThrowIfCancellationRequested();
var make = asset.Asset.Payload.Xmp.Tiff.Make;
var model = asset.Asset.Payload.Xmp.Tiff.Model;
yield return new ImageInfo
{
AssetId = asset.Asset.Id,
FileName = asset.Asset.Payload.ImportSource.FileName,
CaptureDate = asset.Asset.Payload.CaptureDate,
FileSize = asset.Asset.Payload.ImportSource.FileSize,
ShutterSpeed = asset.Asset.Payload.Xmp.Exif.ExposureTime,
FNumber = asset.Asset.Payload.Xmp.Exif.FNumber,
FocalLength = asset.Asset.Payload.Xmp.Exif.FocalLength.First(),
ISO = asset.Asset.Payload.Xmp.Exif.ISOSpeedRatings,
Lens = asset.Asset.Payload.Xmp.Aux.Lens,
Make = make,
Model = model,
Title = asset.Asset.Payload.Xmp.Dc.Title,
Caption = asset.Asset.Payload.Xmp.Dc.Description,
LastModified = DateTime.Parse(asset.Asset.Updated),
Width = asset.Asset.Payload.Develop.CroppedWidth,
Height = asset.Asset.Payload.Develop.CroppedHeight,
};
}
}
while (!string.IsNullOrEmpty(after));
}
public async Task<Stream> GetImageStream(string assetId, string size, CancellationToken cancellationToken = default)
{
var stream = await lightroomClient.GetImageStream(this.catalog.Value.Id, assetId, size, cancellationToken);
return stream;
}
}