< Summary

Class:DCL.NFTAssetLoadHelper
Assembly:DCL.Components.NFT
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/LoadableShapes/NFTShape/NFTAsset/NFTAssetLoadHelper.cs
Covered lines:34
Uncovered lines:29
Coverable lines:63
Total lines:147
Line coverage:53.9% (34 of 63)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
LoadNFTAsset()0%1312080.95%
Dispose()0%110100%
GetHeaders()0%56700%
FetchGif()0%12300%
FetchImage()0%12300%
UnloadPromises()0%330100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/LoadableShapes/NFTShape/NFTAsset/NFTAssetLoadHelper.cs

#LineLine coverage
 1using System;
 2using System.Collections;
 3using System.Collections.Generic;
 4using DCL.Helpers;
 5using NFTShape_Internal;
 6using UnityEngine;
 7using UnityEngine.Networking;
 8
 9namespace DCL
 10{
 11    public interface INFTAssetLoadHelper : IDisposable
 12    {
 13        IEnumerator LoadNFTAsset(string url, Action<INFTAsset> OnSuccess, Action<Exception> OnFail);
 14    }
 15
 16    public class NFTAssetLoadHelper : INFTAssetLoadHelper
 17    {
 18        protected AssetPromise_Texture imagePromise = null;
 19        protected AssetPromise_Gif gifPromise = null;
 20
 21        public IEnumerator LoadNFTAsset(string url, Action<INFTAsset> OnSuccess, Action<Exception> OnFail)
 22        {
 623            if (string.IsNullOrEmpty(url))
 24            {
 025                OnFail?.Invoke(new Exception($"Image url is null!"));
 026                yield break;
 27            }
 28
 29            const string CONTENT_TYPE = "Content-Type";
 30            const string CONTENT_LENGTH = "Content-Length";
 31            const string CONTENT_TYPE_GIF = "image/gif";
 32            const long PREVIEW_IMAGE_SIZE_LIMIT = 500000;
 33
 634            HashSet<string> headers = new HashSet<string>() {CONTENT_TYPE, CONTENT_LENGTH};
 635            Dictionary<string, string> responseHeaders = new Dictionary<string, string>();
 636            string headerRequestError = string.Empty;
 37
 1238            yield return GetHeaders(url, headers, result => responseHeaders = result, (x) => headerRequestError = x);
 39
 640            if (!string.IsNullOrEmpty(headerRequestError))
 41            {
 042                OnFail?.Invoke(new Exception($"Error fetching headers! ({headerRequestError})"));
 043                yield break;
 44            }
 45
 646            string contentType = responseHeaders[CONTENT_TYPE];
 647            long.TryParse(responseHeaders[CONTENT_LENGTH], out long contentLength);
 648            bool isGif = contentType == CONTENT_TYPE_GIF;
 49
 650            if (isGif)
 51            {
 252                yield return FetchGif(url,
 53                    OnSuccess: (promise) =>
 54                    {
 255                        UnloadPromises();
 256                        this.gifPromise = promise;
 257                        OnSuccess?.Invoke(new NFTAsset_Gif(promise.asset));
 258                    },
 059                    OnFail: (exception) => { OnFail?.Invoke(exception); }
 60                );
 61
 262                yield break;
 63            }
 64
 465            if (contentLength > PREVIEW_IMAGE_SIZE_LIMIT)
 66            {
 167                OnFail?.Invoke(new System.Exception($"Image is too big! {contentLength} > {PREVIEW_IMAGE_SIZE_LIMIT}"));
 168                yield break;
 69            }
 70
 371            yield return FetchImage(url,
 72                OnSuccess: (promise) =>
 73                {
 374                    UnloadPromises();
 375                    this.imagePromise = promise;
 376                    OnSuccess?.Invoke(new NFTAsset_Image(promise.asset));
 377                },
 078                OnFail: (exc) => { OnFail?.Invoke(exc); });
 379        }
 80
 81        public void Dispose()
 82        {
 583            UnloadPromises();
 584        }
 85
 86        protected virtual IEnumerator GetHeaders(string url, HashSet<string> headerField,
 87            Action<Dictionary<string, string>> OnSuccess, Action<string> OnFail)
 88        {
 089            using (var request = UnityWebRequest.Head(url))
 90            {
 091                yield return request.SendWebRequest();
 92
 093                if (request.WebRequestSucceded())
 94                {
 095                    var result = new Dictionary<string, string>();
 96
 097                    foreach (var key in headerField)
 98                    {
 099                        result.Add(key, request.GetResponseHeader(key));
 100                    }
 101
 0102                    OnSuccess?.Invoke(result);
 0103                }
 104                else
 105                {
 0106                    OnFail?.Invoke(request.error);
 107                }
 0108            }
 0109        }
 110
 111        protected virtual IEnumerator FetchGif(string url, Action<AssetPromise_Gif> OnSuccess,
 112            Action<Exception> OnFail = null)
 113        {
 0114            AssetPromise_Gif gifPromise = new AssetPromise_Gif(url);
 0115            gifPromise.OnSuccessEvent += texture => { OnSuccess?.Invoke(gifPromise); };
 0116            gifPromise.OnFailEvent += (x, error) => OnFail?.Invoke(error);
 117
 0118            AssetPromiseKeeper_Gif.i.Keep(gifPromise);
 119
 0120            yield return gifPromise;
 0121        }
 122
 123        protected virtual IEnumerator FetchImage(string url, Action<AssetPromise_Texture> OnSuccess,
 124            Action<Exception> OnFail = null)
 125        {
 0126            AssetPromise_Texture texturePromise = new AssetPromise_Texture(url);
 0127            texturePromise.OnSuccessEvent += texture => { OnSuccess?.Invoke(texturePromise); };
 0128            texturePromise.OnFailEvent += (x, error) => OnFail?.Invoke(error);
 129
 0130            AssetPromiseKeeper_Texture.i.Keep(texturePromise);
 131
 0132            yield return texturePromise;
 0133        }
 134
 135        protected virtual void UnloadPromises()
 136        {
 10137            if (gifPromise != null)
 2138                AssetPromiseKeeper_Gif.i.Forget(gifPromise);
 139
 10140            if (imagePromise != null)
 3141                AssetPromiseKeeper_Texture.i.Forget(imagePromise);
 142
 10143            gifPromise = null;
 10144            imagePromise = null;
 10145        }
 146    }
 147}