This repository was archived by the owner on Mar 31, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathPackageIconToCsvDriver.cs
More file actions
181 lines (156 loc) · 7.88 KB
/
PackageIconToCsvDriver.cs
File metadata and controls
181 lines (156 loc) · 7.88 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
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using ImageMagick;
namespace NuGet.Insights.Worker.PackageIconToCsv
{
public class PackageIconToCsvDriver : ICatalogLeafToCsvDriver<PackageIcon>, ICsvResultStorage<PackageIcon>
{
private static readonly IReadOnlyList<string> IgnoredAttributes = ["date:create", "date:modify", "signature"];
private readonly CatalogClient _catalogClient;
private readonly FlatContainerClient _flatContainerClient;
private readonly IOptions<NuGetInsightsWorkerSettings> _options;
private readonly ILogger<PackageIconToCsvDriver> _logger;
public PackageIconToCsvDriver(
CatalogClient catalogClient,
FlatContainerClient flatContainerClient,
IOptions<NuGetInsightsWorkerSettings> options,
ILogger<PackageIconToCsvDriver> logger)
{
_catalogClient = catalogClient;
_flatContainerClient = flatContainerClient;
_options = options;
_logger = logger;
}
public string ResultContainerName => _options.Value.PackageIconContainerName;
public bool SingleMessagePerId => false;
public Task InitializeAsync()
{
return Task.CompletedTask;
}
public Task DestroyAsync()
{
return Task.CompletedTask;
}
public async Task<DriverResult<IReadOnlyList<PackageIcon>>> ProcessLeafAsync(CatalogLeafScan leafScan)
{
(var resultType, var records) = await ProcessLeafInternalAsync(leafScan);
if (resultType == TempStreamResultType.SemaphoreNotAvailable)
{
return DriverResult.TryAgainLater<IReadOnlyList<PackageIcon>>();
}
return DriverResult.Success(records);
}
public async Task<(TempStreamResultType, IReadOnlyList<PackageIcon>)> ProcessLeafInternalAsync(CatalogLeafScan leafScan)
{
var scanId = Guid.NewGuid();
var scanTimestamp = DateTimeOffset.UtcNow;
if (leafScan.LeafType == CatalogLeafType.PackageDelete)
{
var leaf = (PackageDeleteCatalogLeaf)await _catalogClient.GetCatalogLeafAsync(leafScan.LeafType, leafScan.Url);
return (
TempStreamResultType.Success,
[new PackageIcon(scanId, scanTimestamp, leaf)]
);
}
else
{
var leaf = (PackageDetailsCatalogLeaf)await _catalogClient.GetCatalogLeafAsync(leafScan.LeafType, leafScan.Url);
var result = await _flatContainerClient.DownloadPackageIconToFileAsync(
leafScan.PackageId,
leafScan.PackageVersion,
CancellationToken.None);
if (result is null)
{
return (
TempStreamResultType.Success,
[new PackageIcon(scanId, scanTimestamp, leaf) { ResultType = PackageIconResultType.NoIcon }]
);
}
await using (result.Value.Body)
{
if (result.Value.Body.Type == TempStreamResultType.SemaphoreNotAvailable)
{
return (
TempStreamResultType.SemaphoreNotAvailable,
null
);
}
var output = new PackageIcon(scanId, scanTimestamp, leaf)
{
ResultType = PackageIconResultType.Available,
FileLength = result.Value.Body.Stream.Length,
FileSHA256 = result.Value.Body.Hash.SHA256.ToBase64(),
ContentType = result.Value.ContentType,
};
// Try to detect the format. ImageMagick appears to not detect .ico files when only given a stream.
result.Value.Body.Stream.Position = 0;
var format = FormatDetector.Detect(result.Value.Body.Stream);
output.HeaderFormat = format.ToString();
try
{
(var autoDetectedFormat, var frames) = GetMagickImageCollection(leaf, result.Value.Body, format);
using (frames)
{
using var image = frames.First();
// Maintain original (frame) order of formats and dimensions
var frameFormats = new List<string>();
var frameDimensions = new List<object>();
var uniqueFrameFormats = new HashSet<string>();
var uniqueFrameDimensions = new HashSet<(long, long)>();
var frameAttributeNames = new HashSet<string>();
foreach (var frame in frames)
{
var frameFormat = frame.Format.ToString();
if (uniqueFrameFormats.Add(frameFormat))
{
frameFormats.Add(frameFormat);
}
if (uniqueFrameDimensions.Add((frame.Width, frame.Height)))
{
frameDimensions.Add(new { frame.Width, frame.Height });
}
foreach (var attributeName in frame.AttributeNames)
{
frameAttributeNames.Add(attributeName);
}
}
output.Signature = ByteArrayExtensions.StringToByteArray(image.Signature).ToBase64();
output.AutoDetectedFormat = autoDetectedFormat;
output.Width = image.Width;
output.Height = image.Height;
output.IsOpaque = image.IsOpaque;
output.FrameCount = frames.Count;
output.FrameFormats = KustoDynamicSerializer.Serialize(frameFormats);
output.FrameDimensions = KustoDynamicSerializer.Serialize(frameDimensions);
output.FrameAttributeNames = KustoDynamicSerializer.Serialize(frameAttributeNames.Except(IgnoredAttributes).OrderBy(x => x, StringComparer.Ordinal).ToList());
}
}
catch (Exception ex) when (ex is not OutOfMemoryException)
{
_logger.LogWarning(ex, "Failed to process icon for {Id}/{Version}.", leaf.PackageId, leaf.PackageVersion);
output.ResultType = PackageIconResultType.Error;
}
return (TempStreamResultType.Success, [output]);
}
}
}
private (bool, MagickImageCollection) GetMagickImageCollection(CatalogLeaf leaf, TempStreamResult result, MagickFormat format)
{
try
{
result.Stream.Position = 0;
return (true, new MagickImageCollection(result.Stream));
}
catch (Exception ex) when (ex is not OutOfMemoryException)
{
_logger.LogInformation(
ex,
"ImageMagick failed to auto-detect format of icon for {Id}/{Version}.",
leaf.PackageId,
leaf.PackageVersion);
}
result.Stream.Position = 0;
return (false, new MagickImageCollection(result.Stream, format));
}
}
}