Skip to content

Commit 03de422

Browse files
committed
Use unity based image download
1 parent 9910bb5 commit 03de422

File tree

1 file changed

+11
-32
lines changed

1 file changed

+11
-32
lines changed

Editor/Inspector/FigmaUpdater.cs

Lines changed: 11 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System.IO;
55
using System.Linq;
66
using System.Net;
7-
using System.Net.Http;
87
using System.Reflection;
98
using System.Threading;
109
using System.Threading.Tasks;
@@ -13,6 +12,7 @@
1312
using Unity.VectorGraphics.Editor;
1413
using UnityEditor;
1514
using UnityEngine;
15+
using UnityEngine.Networking;
1616
using Object = UnityEngine.Object;
1717

1818
namespace Figma.Inspectors
@@ -230,45 +230,24 @@ async Task WriteInvalidPngAsync(string assetPath)
230230
}
231231
async Task GetImageAsync(string nodeID, string url, string extension)
232232
{
233-
(bool fileExists, string test) = GetAssetPath(nodeID, extension);
233+
(bool fileExists, string _) = GetAssetPath(nodeID, extension);
234234

235235
Progress.SetStepLabel(progress, $"{url}");
236236

237-
HttpClient client = new();
238-
foreach (KeyValuePair<string, string> header in headers) client.DefaultRequestHeaders.Add(header.Key, header.Value);
239-
if (fileExists && remaps.TryGetValue(nodeID, out string etag)) client.DefaultRequestHeaders.Add("If-None-Match", $"\"{etag}\"");
240-
HttpResponseMessage response = await client.GetAsync(url, token);
237+
Dictionary<string, string> responseHeaders = new();
238+
Dictionary<string, string> requestHeaders = headers.ToDictionary(header => header.Key, header => header.Value);
239+
if (fileExists && remaps.TryGetValue(nodeID, out string etag)) requestHeaders.Add("If-None-Match", $"\"{etag}\"");
241240

242-
if (response.Headers.TryGetValues("ETag", out IEnumerable<string> values))
243-
remaps[nodeID] = values.First().Trim('"');
241+
UnityWebRequest request = await url.HttpGetRequestAsync(requestHeaders, responseHeaders, cancellationToken: token);
242+
243+
if (responseHeaders.TryGetValue("ETag", out etag))
244+
remaps[nodeID] = etag.Trim('"');
244245

245246
(bool _, string assetPath) = GetAssetPath(nodeID, extension);
246247
string relativePath = Path.Combine(relativeFolder, assetPath).Replace('\\', '/');
247248

248-
if (response.StatusCode == HttpStatusCode.OK)
249-
{
250-
byte[] bytes = await response.Content.ReadAsByteArrayAsync();
251-
switch (bytes.Length)
252-
{
253-
case 0:
254-
Debug.LogWarning($"Response is empty for node={nodeID}, url={url}");
255-
switch (extension)
256-
{
257-
case "svg":
258-
await WriteInvalidSvgAsync(assetPath);
259-
break;
260-
261-
default:
262-
await WriteInvalidPngAsync(assetPath);
263-
break;
264-
}
265-
break;
266-
267-
default:
268-
await File.WriteAllBytesAsync(relativePath, bytes, token);
269-
break;
270-
}
271-
}
249+
if (request.result == UnityWebRequest.Result.Success && request.responseCode == (long)HttpStatusCode.OK &&
250+
request.downloadHandler.data is { Length: > 0 } data) await File.WriteAllBytesAsync(relativePath, data, token);
272251
}
273252
async Task WriteGradientsAsync(int progress, CancellationToken token)
274253
{

0 commit comments

Comments
 (0)