|
4 | 4 | using System.IO; |
5 | 5 | using System.Linq; |
6 | 6 | using System.Net; |
7 | | -using System.Net.Http; |
8 | 7 | using System.Reflection; |
9 | 8 | using System.Threading; |
10 | 9 | using System.Threading.Tasks; |
|
13 | 12 | using Unity.VectorGraphics.Editor; |
14 | 13 | using UnityEditor; |
15 | 14 | using UnityEngine; |
| 15 | +using UnityEngine.Networking; |
16 | 16 | using Object = UnityEngine.Object; |
17 | 17 |
|
18 | 18 | namespace Figma.Inspectors |
@@ -230,45 +230,24 @@ async Task WriteInvalidPngAsync(string assetPath) |
230 | 230 | } |
231 | 231 | async Task GetImageAsync(string nodeID, string url, string extension) |
232 | 232 | { |
233 | | - (bool fileExists, string test) = GetAssetPath(nodeID, extension); |
| 233 | + (bool fileExists, string _) = GetAssetPath(nodeID, extension); |
234 | 234 |
|
235 | 235 | Progress.SetStepLabel(progress, $"{url}"); |
236 | 236 |
|
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}\""); |
241 | 240 |
|
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('"'); |
244 | 245 |
|
245 | 246 | (bool _, string assetPath) = GetAssetPath(nodeID, extension); |
246 | 247 | string relativePath = Path.Combine(relativeFolder, assetPath).Replace('\\', '/'); |
247 | 248 |
|
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); |
272 | 251 | } |
273 | 252 | async Task WriteGradientsAsync(int progress, CancellationToken token) |
274 | 253 | { |
|
0 commit comments