public async Task <WithLoadingResult <Stream> > GetStream(string identifier, CancellationToken token)
        {
            StorageFile file = null;

            try
            {
                var filePath = Path.GetDirectoryName(identifier);

                if (!string.IsNullOrWhiteSpace(filePath))
                {
                    file = await StorageFile.GetFileFromPathAsync(identifier);
                }
            }
            catch (Exception)
            {
            }

            if (file != null)
            {
                var imageInformation = new ImageInformation();
                imageInformation.SetPath(identifier);
                imageInformation.SetFilePath(identifier);

                return(WithLoadingResult.Encapsulate(await file.OpenStreamForReadAsync(), LoadingResult.Disk, imageInformation));
            }

            return(WithLoadingResult.Encapsulate <Stream>(null, LoadingResult.Disk));
        }
Esempio n. 2
0
        public async Task <UIImageData> GetData(string identifier, CancellationToken token)
        {
            var downloadedData = await DownloadCache.GetAsync(identifier, token, Parameters.CacheDuration, Parameters.CustomCacheKey, Parameters.CacheType).ConfigureAwait(false);

            var bytes  = downloadedData.Bytes;
            var path   = downloadedData.CachedPath;
            var result = downloadedData.RetrievedFromDiskCache ? LoadingResult.DiskCache : LoadingResult.Internet;

            var imageInformation = new ImageInformation();

            imageInformation.SetPath(identifier);
            var allowDiskCaching = Parameters.CacheType.HasValue == false || Parameters.CacheType == CacheType.All || Parameters.CacheType == CacheType.Disk;

            if (allowDiskCaching)
            {
                imageInformation.SetFilePath(await DownloadCache.GetDiskCacheFilePathAsync(identifier, Parameters.CustomCacheKey));
            }

            return(new UIImageData()
            {
                Data = bytes,
                Result = result,
                ResultIdentifier = path,
                ImageInformation = imageInformation
            });
        }
Esempio n. 3
0
        public Task <WithLoadingResult <Stream> > GetStream(string identifier, CancellationToken token)
        {
            try
            {
                var imageInformation = new ImageInformation();
                imageInformation.SetPath(identifier);
                imageInformation.SetFilePath(null);

                var result = WithLoadingResult.Encapsulate(Context.Assets.Open(identifier, Access.Streaming),
                                                           LoadingResult.ApplicationBundle, imageInformation);
                return(Task.FromResult(result));
            }
            catch (Java.IO.FileNotFoundException)
            {
                return(Task.FromResult(WithLoadingResult.Encapsulate((Stream)null, LoadingResult.NotFound)));
            }
            catch (Java.IO.IOException)
            {
                return(Task.FromResult(WithLoadingResult.Encapsulate((Stream)null, LoadingResult.NotFound)));
            }
            catch (FileNotFoundException)
            {
                return(Task.FromResult(WithLoadingResult.Encapsulate((Stream)null, LoadingResult.NotFound)));
            }
            catch (IOException)
            {
                return(Task.FromResult(WithLoadingResult.Encapsulate((Stream)null, LoadingResult.NotFound)));
            }
        }
Esempio n. 4
0
        public Task <WithLoadingResult <Stream> > GetStream(string identifier, CancellationToken token)
        {
            // Resource name is always without extension
            string resourceName = Path.GetFileNameWithoutExtension(identifier);

            int resourceId = 0;

            if (!_resourceIdentifiersCache.TryGetValue(resourceName, out resourceId))
            {
                resourceId = Context.Resources.GetIdentifier(resourceName.ToLower(), "drawable", Context.PackageName);
                _resourceIdentifiersCache.TryAdd(resourceName.ToLower(), resourceId);
            }

            Stream stream = null;

            if (resourceId != 0)
            {
                stream = Context.Resources.OpenRawResource(resourceId);
            }
            else
            {
                return(Task.FromResult(WithLoadingResult.Encapsulate((Stream)null, LoadingResult.NotFound)));
            }

            var imageInformation = new ImageInformation();

            imageInformation.SetPath(identifier);
            imageInformation.SetFilePath(null);

            var result = WithLoadingResult.Encapsulate(stream, LoadingResult.CompiledResource, imageInformation);

            return(Task.FromResult(result));
        }
        public Task<WithLoadingResult<Stream>> GetStream(string identifier, CancellationToken token)
        {
            // Resource name is always without extension
            string resourceName = Path.GetFileNameWithoutExtension(identifier);

            int resourceId = 0;
            if (!_resourceIdentifiersCache.TryGetValue(resourceName, out resourceId))
            {
                resourceId = Context.Resources.GetIdentifier(resourceName.ToLower(), "drawable", Context.PackageName);
                _resourceIdentifiersCache.TryAdd(resourceName.ToLower(), resourceId);
            }

            Stream stream = null;
            if (resourceId != 0)
            {
                stream = Context.Resources.OpenRawResource(resourceId);
            }
            else
            {
                return Task.FromResult(WithLoadingResult.Encapsulate((Stream)null, LoadingResult.NotFound));
            }

            var imageInformation = new ImageInformation();
            imageInformation.SetPath(identifier);
            imageInformation.SetFilePath(null);

            var result = WithLoadingResult.Encapsulate(stream, LoadingResult.CompiledResource, imageInformation);
            return Task.FromResult(result);
        }
        public async virtual Task<Tuple<Stream, LoadingResult, ImageInformation>> Resolve(string identifier, TaskParameter parameters, CancellationToken token)
        {
            StorageFile file = null;

            try
            {
                var filePath = Path.GetDirectoryName(identifier);

                if (!string.IsNullOrWhiteSpace(filePath))
                {
                    file = await StorageFile.GetFileFromPathAsync(identifier);
                }
            }
            catch (Exception)
            {
            }

            if (file != null)
            {
                var imageInformation = new ImageInformation();
                imageInformation.SetPath(identifier);
                imageInformation.SetFilePath(identifier);

                token.ThrowIfCancellationRequested();
                var stream = await file.OpenStreamForReadAsync();

                return new Tuple<Stream, LoadingResult, ImageInformation>(stream, LoadingResult.Disk, imageInformation);
            }

            throw new FileNotFoundException(identifier);
        }
        public virtual Task<Tuple<Stream, LoadingResult, ImageInformation>> Resolve(string identifier, TaskParameter parameters, CancellationToken token)
        {
            // Resource name is always without extension
            string resourceName = Path.GetFileNameWithoutExtension(identifier);

            int resourceId = 0;
            if (!_resourceIdentifiersCache.TryGetValue(resourceName, out resourceId))
            {
                token.ThrowIfCancellationRequested();
                resourceId = Context.Resources.GetIdentifier(resourceName.ToLower(), "drawable", Context.PackageName);
                _resourceIdentifiersCache.TryAdd(resourceName.ToLower(), resourceId);
            }

            if (resourceId == 0)
                throw new FileNotFoundException(identifier);

            token.ThrowIfCancellationRequested();
            Stream stream  = Context.Resources.OpenRawResource(resourceId);

            if (stream == null)
                throw new FileNotFoundException(identifier);

            var imageInformation = new ImageInformation();
            imageInformation.SetPath(identifier);
            imageInformation.SetFilePath(identifier);

            return Task.FromResult(new Tuple<Stream, LoadingResult, ImageInformation>(
                stream, LoadingResult.CompiledResource, imageInformation));
        }
        public async virtual Task <Tuple <Stream, LoadingResult, ImageInformation> > Resolve(string identifier, TaskParameter parameters, CancellationToken token)
        {
            StorageFile file = null;

            try
            {
                var filePath = Path.GetDirectoryName(identifier);

                if (!string.IsNullOrWhiteSpace(filePath))
                {
                    file = await StorageFile.GetFileFromPathAsync(identifier);
                }
            }
            catch (Exception)
            {
            }

            if (file != null)
            {
                var imageInformation = new ImageInformation();
                imageInformation.SetPath(identifier);
                imageInformation.SetFilePath(identifier);

                var stream = await file.OpenStreamForReadAsync();

                return(new Tuple <Stream, LoadingResult, ImageInformation>(stream, LoadingResult.Disk, imageInformation));
            }

            throw new FileNotFoundException(identifier);
        }
Esempio n. 9
0
        public async virtual Task <DataResolverResult> Resolve(string identifier, TaskParameter parameters, CancellationToken token)
        {
            StorageFile file = null;

            try
            {
                var filePath = Path.GetDirectoryName(identifier);
                if (!string.IsNullOrWhiteSpace(filePath))
                {
                    file = await Cache.FFSourceBindingCache.GetFileAsync(identifier);
                }
            }
            catch (Exception)
            {
            }

            if (file != null)
            {
                var imageInformation = new ImageInformation();
                imageInformation.SetPath(identifier);
                imageInformation.SetFilePath(identifier);

                token.ThrowIfCancellationRequested();
                var stream = await file.OpenStreamForReadAsync();

                return(new DataResolverResult(stream, LoadingResult.Disk, imageInformation));
            }

            throw new FileNotFoundException(identifier);
        }
		public Task<WithLoadingResult<Stream>> GetStream(string identifier, CancellationToken token)
		{
			try
			{
				var imageInformation = new ImageInformation();
				imageInformation.SetPath(identifier);
				imageInformation.SetFilePath(null);

				var result = WithLoadingResult.Encapsulate(Context.Assets.Open(identifier, Access.Streaming), 
					LoadingResult.ApplicationBundle, imageInformation);
				return Task.FromResult(result);
			}
			catch (Java.IO.FileNotFoundException)
			{
				return Task.FromResult(WithLoadingResult.Encapsulate((Stream)null, LoadingResult.NotFound));
			}
			catch (Java.IO.IOException)
			{
				return Task.FromResult(WithLoadingResult.Encapsulate((Stream)null, LoadingResult.NotFound));
			}
			catch (FileNotFoundException)
			{
				return Task.FromResult(WithLoadingResult.Encapsulate((Stream)null, LoadingResult.NotFound));
			}
			catch (IOException)
			{
				return Task.FromResult(WithLoadingResult.Encapsulate((Stream)null, LoadingResult.NotFound));
			}
		}
        public virtual Task <Tuple <Stream, LoadingResult, ImageInformation> > Resolve(string identifier, TaskParameter parameters, CancellationToken token)
        {
            // Resource name is always without extension
            string resourceName = Path.GetFileNameWithoutExtension(identifier);

            int resourceId = 0;

            if (!_resourceIdentifiersCache.TryGetValue(resourceName, out resourceId))
            {
                token.ThrowIfCancellationRequested();
                resourceId = Context.Resources.GetIdentifier(resourceName.ToLower(), "drawable", Context.PackageName);
                _resourceIdentifiersCache.TryAdd(resourceName.ToLower(), resourceId);
            }

            if (resourceId == 0)
            {
                throw new FileNotFoundException(identifier);
            }

            token.ThrowIfCancellationRequested();
            Stream stream = Context.Resources.OpenRawResource(resourceId);

            if (stream == null)
            {
                throw new FileNotFoundException(identifier);
            }

            var imageInformation = new ImageInformation();

            imageInformation.SetPath(identifier);
            imageInformation.SetFilePath(identifier);

            return(Task.FromResult(new Tuple <Stream, LoadingResult, ImageInformation>(
                                       stream, LoadingResult.CompiledResource, imageInformation)));
        }
Esempio n. 12
0
        public Task <Tuple <Stream, LoadingResult, ImageInformation> > Resolve(string identifier, TaskParameter parameters, CancellationToken token)
        {
            var imageInformation = new ImageInformation();

            imageInformation.SetPath(identifier);

            if (identifier.StartsWith("<", StringComparison.OrdinalIgnoreCase))
            {
                var streamXML = new MemoryStream(Encoding.UTF8.GetBytes(identifier));

                return(Task.FromResult(new Tuple <Stream, LoadingResult, ImageInformation>(
                                           streamXML, LoadingResult.EmbeddedResource, imageInformation)));
            }

            var mime     = string.Empty;
            var data     = string.Empty;
            var encoding = "base64";
            var match1   = _regex1.Match(identifier);
            var success  = false;

            if (match1.Success)
            {
                mime     = match1.Groups["mime"].Value;
                encoding = match1.Groups["encoding"].Value;
                data     = match1.Groups["data"].Value;
                success  = true;
            }
            else
            {
                var match2 = _regex2.Match(identifier);
                if (match2.Success)
                {
                    mime    = match2.Groups["mime"].Value;
                    data    = match2.Groups["data"].Value;
                    success = true;
                }
            }

            if (!success || (!mime.StartsWith("image/", StringComparison.OrdinalIgnoreCase) &&
                             !mime.StartsWith("text/", StringComparison.OrdinalIgnoreCase)))
            {
                throw new NotImplementedException("Data type not supported");
            }

            if (!encoding.Equals("base64", StringComparison.OrdinalIgnoreCase) ||
                data.StartsWith("<", StringComparison.OrdinalIgnoreCase))
            {
                var stream = new MemoryStream(Encoding.UTF8.GetBytes(data));

                return(Task.FromResult(new Tuple <Stream, LoadingResult, ImageInformation>(
                                           stream, LoadingResult.EmbeddedResource, imageInformation)));
            }

            var streamBase64 = new MemoryStream(Convert.FromBase64String(data));

            return(Task.FromResult(new Tuple <Stream, LoadingResult, ImageInformation>(
                                       streamBase64, LoadingResult.EmbeddedResource, imageInformation)));
        }
        public virtual Task <Tuple <Stream, LoadingResult, ImageInformation> > Resolve(string identifier, TaskParameter parameters, CancellationToken token)
        {
            string file = null;

            int scale = (int)ScaleHelper.Scale;

            if (scale > 1)
            {
                var          filename  = Path.GetFileNameWithoutExtension(identifier);
                var          extension = Path.GetExtension(identifier);
                const string pattern   = "{0}@{1}x{2}";

                while (scale > 1)
                {
                    token.ThrowIfCancellationRequested();

                    var tmpFile = string.Format(pattern, filename, scale, extension);
                    if (FileStore.Exists(tmpFile))
                    {
                        file = tmpFile;
                        break;
                    }
                    scale--;
                }
            }

            if (string.IsNullOrEmpty(file) && FileStore.Exists(identifier))
            {
                file = identifier;
            }

            token.ThrowIfCancellationRequested();

            if (!string.IsNullOrEmpty(file))
            {
                var stream           = FileStore.GetInputStream(file, true);
                var imageInformation = new ImageInformation();
                imageInformation.SetPath(identifier);
                imageInformation.SetFilePath(file);

                var result = (LoadingResult)(int)parameters.Source;

                if (parameters.LoadingPlaceholderPath == identifier)
                {
                    result = (LoadingResult)(int)parameters.LoadingPlaceholderSource;
                }
                else if (parameters.ErrorPlaceholderPath == identifier)
                {
                    result = (LoadingResult)(int)parameters.ErrorPlaceholderSource;
                }

                return(Task.FromResult(new Tuple <Stream, LoadingResult, ImageInformation>(
                                           stream, result, imageInformation)));
            }

            throw new FileNotFoundException(identifier);
        }
Esempio n. 14
0
        public Task <Tuple <Stream, LoadingResult, ImageInformation> > Resolve(string identifier, TaskParameter parameters, CancellationToken token)
        {
            var imageInformation = new ImageInformation();

            imageInformation.SetPath(identifier);

            if (!identifier.StartsWith("data:", StringComparison.OrdinalIgnoreCase))
            {
                if (parameters.DataEncoding == DataEncodingType.Base64Encoded)
                {
                    return(GetBase64Stream(identifier, imageInformation));
                }

                return(GetRAWStream(identifier, imageInformation));
            }

            var mime     = string.Empty;
            var data     = string.Empty;
            var encoding = "base64";
            var match1   = _regex1.Match(identifier);
            var success  = false;

            if (match1.Success)
            {
                mime     = match1.Groups["mime"].Value;
                encoding = match1.Groups["encoding"].Value;
                data     = match1.Groups["data"].Value;
                success  = true;
            }
            else
            {
                var match2 = _regex2.Match(identifier);
                if (match2.Success)
                {
                    mime    = match2.Groups["mime"].Value;
                    data    = match2.Groups["data"].Value;
                    success = true;
                }
            }

            if (!success || (!mime.StartsWith("image/", StringComparison.OrdinalIgnoreCase) &&
                             !mime.StartsWith("text/", StringComparison.OrdinalIgnoreCase)))
            {
                throw new NotImplementedException("Data type not supported");
            }

            if (!encoding.Equals("base64", StringComparison.OrdinalIgnoreCase) ||
                data.StartsWith("<", StringComparison.OrdinalIgnoreCase))
            {
                return(GetRAWStream(data, imageInformation));
            }

            return(GetBase64Stream(data, imageInformation));
        }
Esempio n. 15
0
        public async Task <WithLoadingResult <Stream> > GetStream(string identifier, CancellationToken token)
        {
            var cachedStream = await DownloadCache.GetStreamAsync(identifier, token, Parameters.CacheDuration, Parameters.CustomCacheKey).ConfigureAwait(false);

            var imageInformation = new ImageInformation();

            imageInformation.SetPath(identifier);
            imageInformation.SetFilePath(await DownloadCache.GetDiskCacheFilePathAsync(identifier, Parameters.CustomCacheKey));

            return(WithLoadingResult.Encapsulate(cachedStream.ImageStream,
                                                 cachedStream.RetrievedFromDiskCache ? LoadingResult.DiskCache : LoadingResult.Internet, imageInformation));
        }
 public virtual async Task <DataResolverResult> Resolve(string identifier, TaskParameter parameters, CancellationToken token)
 {
     if (File.Exists(identifier))
     {
         ImageInformation imageInformation = new ImageInformation();
         imageInformation.SetPath(identifier);
         imageInformation.SetFilePath(identifier);
         token.ThrowIfCancellationRequested();
         return(new DataResolverResult(await Task.Run(() => File.OpenRead(identifier)), LoadingResult.Disk, imageInformation));
     }
     throw new FileNotFoundException(identifier);
 }
        public virtual Task<Tuple<Stream, LoadingResult, ImageInformation>> Resolve(string identifier, TaskParameter parameters, CancellationToken token)
        {
            string file = null;

            int scale = (int)ScaleHelper.Scale;
            if (scale > 1)
            {
                var filename = Path.GetFileNameWithoutExtension(identifier);
                var extension = Path.GetExtension(identifier);
                const string pattern = "{0}@{1}x{2}";

                while (scale > 1)
                {
                    token.ThrowIfCancellationRequested();

                    var tmpFile = string.Format(pattern, filename, scale, extension);
                    if (FileStore.Exists(tmpFile))
                    {
                        file = tmpFile;
                        break;
                    }
                    scale--;
                }
            }

            if (string.IsNullOrEmpty(file) && FileStore.Exists(identifier))
            {
                file = identifier;
            }

            token.ThrowIfCancellationRequested();

            if (!string.IsNullOrEmpty(file))
            {
                var stream = FileStore.GetInputStream(file, true);
                var imageInformation = new ImageInformation();
                imageInformation.SetPath(identifier);
                imageInformation.SetFilePath(file);

                var result = (LoadingResult)(int)parameters.Source;

                if (parameters.LoadingPlaceholderPath == identifier)
                    result = (LoadingResult)(int)parameters.LoadingPlaceholderSource;
                else if (parameters.ErrorPlaceholderPath == identifier)
                    result = (LoadingResult)(int)parameters.ErrorPlaceholderSource;

                return Task.FromResult(new Tuple<Stream, LoadingResult, ImageInformation>(
                    stream, result, imageInformation));
            }

            throw new FileNotFoundException(identifier);
        }
        public Task<WithLoadingResult<Stream>> GetStream(string identifier, CancellationToken token)
        {
            if (!FileStore.Exists(identifier))
            {
                return Task.FromResult(WithLoadingResult.Encapsulate((Stream)null, LoadingResult.NotFound));
            }

            var imageInformation = new ImageInformation();
            imageInformation.SetPath(identifier);
            imageInformation.SetFilePath(identifier);

            var result = WithLoadingResult.Encapsulate(FileStore.GetInputStream(identifier, false), LoadingResult.Disk, imageInformation);
            return Task.FromResult(result);
        }
		public async Task<WithLoadingResult<Stream>> GetStream(string identifier, CancellationToken token)
		{
			var cachedStream = await DownloadCache.GetStreamAsync(identifier, token, Parameters.CacheDuration, Parameters.CustomCacheKey, Parameters.CacheType).ConfigureAwait(false);

			var imageInformation = new ImageInformation();
			imageInformation.SetPath(identifier);
            var allowDiskCaching = Parameters.CacheType.HasValue == false || Parameters.CacheType == CacheType.All || Parameters.CacheType == CacheType.Disk;
            if (allowDiskCaching)
		    {
                imageInformation.SetFilePath(await DownloadCache.GetDiskCacheFilePathAsync(identifier, Parameters.CustomCacheKey));
            }

			return WithLoadingResult.Encapsulate(cachedStream.ImageStream,
				cachedStream.RetrievedFromDiskCache ? LoadingResult.DiskCache : LoadingResult.Internet, imageInformation);
		}
        public virtual Task<Tuple<Stream, LoadingResult, ImageInformation>> Resolve(string identifier, TaskParameter parameters, CancellationToken token)
        {
            token.ThrowIfCancellationRequested();
            var stream = Context.Assets.Open(identifier, Access.Streaming);

            if (stream == null)
                throw new FileNotFoundException(identifier);

            var imageInformation = new ImageInformation();
            imageInformation.SetPath(identifier);
            imageInformation.SetFilePath(null);

            return Task.FromResult(new Tuple<Stream, LoadingResult, ImageInformation>(
                stream, LoadingResult.ApplicationBundle, imageInformation));
        }
Esempio n. 21
0
        public Task <WithLoadingResult <Stream> > GetStream(string identifier, CancellationToken token)
        {
            if (!FileStore.Exists(identifier))
            {
                return(Task.FromResult(WithLoadingResult.Encapsulate((Stream)null, LoadingResult.NotFound)));
            }

            var imageInformation = new ImageInformation();

            imageInformation.SetPath(identifier);
            imageInformation.SetFilePath(identifier);

            var result = WithLoadingResult.Encapsulate(FileStore.GetInputStream(identifier), LoadingResult.Disk, imageInformation);

            return(Task.FromResult(result));
        }
Esempio n. 22
0
        private async Task <DataResolverResult> ResolveFromNamedResourceAsync(string fileName, string identifier, TaskParameter parameters, CancellationToken token)
        {
            PImage image = null;

            try
            {
#if __IOS__ || __TVOS__
                await ImageService.Instance.Config.MainThreadDispatcher.PostAsync(() => image = PImage.FromBundle(identifier)).ConfigureAwait(false);
#elif __MACOS__
                await ImageService.Instance.Config.MainThreadDispatcher.PostAsync(() => image = PImage.ImageNamed(identifier)).ConfigureAwait(false);
#endif
            }
            catch { }

            if (image == null && fileName != identifier)
            {
                try
                {
#if __IOS__ || __TVOS__
                    await ImageService.Instance.Config.MainThreadDispatcher.PostAsync(() => image = PImage.FromBundle(fileName)).ConfigureAwait(false);
#elif __MACOS__
                    await ImageService.Instance.Config.MainThreadDispatcher.PostAsync(() => image = PImage.ImageNamed(fileName)).ConfigureAwait(false);
#endif
                }
                catch { }
            }

            if (image != null)
            {
                token.ThrowIfCancellationRequested();

                var imageInformation = new ImageInformation();
                imageInformation.SetPath(identifier);
                imageInformation.SetFilePath(null);

                var container = new DecodedImage <object>()
                {
                    Image = image
                };

                var result = new DataResolverResult(container, LoadingResult.CompiledResource, imageInformation);

                return(result);
            }

            return(null);
        }
        public virtual async Task <DataResolverResult> Resolve(string identifier, TaskParameter parameters, CancellationToken token)
        {
            ImageService.Instance.Config.Logger.Debug("Resolving resource " + identifier + ".");
            string name = Path.GetFileNameWithoutExtension(identifier.ToLower());

            if (_resourceNames.TryGetValue(name, out var resourceName))
            {
                ImageService.Instance.Config.Logger.Debug("Resource " + identifier + " resolved as " + resourceName + ".");
                Stream stream = await Task.Run(() => _entryAssembly.GetManifestResourceStream(resourceName));

                ImageInformation imageInformation = new ImageInformation();
                imageInformation.SetPath(identifier);
                return(new DataResolverResult(stream, LoadingResult.EmbeddedResource, imageInformation));
            }
            ImageService.Instance.Config.Logger.Error("Resource " + identifier + " not resolved!");
            throw new FileNotFoundException(identifier);
        }
Esempio n. 24
0
        public virtual Task <DataResolverResult> Resolve(string identifier, TaskParameter parameters, CancellationToken token)
        {
            token.ThrowIfCancellationRequested();
            var stream = Context.Assets.Open(identifier, Access.Streaming);

            if (stream == null)
            {
                throw new FileNotFoundException(identifier);
            }

            var imageInformation = new ImageInformation();

            imageInformation.SetPath(identifier);
            imageInformation.SetFilePath(null);

            return(Task.FromResult(new DataResolverResult(stream, LoadingResult.ApplicationBundle, imageInformation)));
        }
        public async virtual Task<Tuple<Stream, LoadingResult, ImageInformation>> Resolve(string identifier, TaskParameter parameters, CancellationToken token)
        {
            var downloadedData = await DownloadCache.DownloadAndCacheIfNeededAsync(identifier, parameters, Configuration, token).ConfigureAwait(false);

            if (token.IsCancellationRequested)
            {
                downloadedData?.ImageStream?.Dispose();
                token.ThrowIfCancellationRequested();
            }

            var imageInformation = new ImageInformation();
            imageInformation.SetPath(identifier);
            imageInformation.SetFilePath(downloadedData?.FilePath);

            return new Tuple<Stream, LoadingResult, ImageInformation>(
                downloadedData?.ImageStream, downloadedData.RetrievedFromDiskCache ? LoadingResult.DiskCache : LoadingResult.Internet, imageInformation);
        }
Esempio n. 26
0
        public Task <Tuple <Stream, LoadingResult, ImageInformation> > Resolve(string identifier, TaskParameter parameters, CancellationToken token)
        {
            if (!FileStore.Exists(identifier))
            {
                throw new FileNotFoundException(identifier);
            }

            var stream = FileStore.GetInputStream(identifier, true);

            var imageInformation = new ImageInformation();

            imageInformation.SetPath(identifier);
            imageInformation.SetFilePath(identifier);

            return(Task.FromResult(new Tuple <Stream, LoadingResult, ImageInformation>(
                                       stream, LoadingResult.Disk, imageInformation)));
        }
Esempio n. 27
0
        public Task <Tuple <Stream, LoadingResult, ImageInformation> > Resolve(string identifier, TaskParameter parameters, CancellationToken token)
        {
            var stream = Context.Assets.Open(identifier, Access.Streaming);

            if (stream == null)
            {
                throw new FileNotFoundException(identifier);
            }

            var imageInformation = new ImageInformation();

            imageInformation.SetPath(identifier);
            imageInformation.SetFilePath(null);

            return(Task.FromResult(new Tuple <Stream, LoadingResult, ImageInformation>(
                                       stream, LoadingResult.ApplicationBundle, imageInformation)));
        }
        public async virtual Task <DataResolverResult> Resolve(string identifier, TaskParameter parameters, CancellationToken token)
        {
            if (!File.Exists(identifier))
            {
                return(await resource.Resolve(identifier, parameters, token));
            }

            var imageInformation = new ImageInformation();

            imageInformation.SetPath(identifier);
            imageInformation.SetFilePath(identifier);

            token.ThrowIfCancellationRequested();
            var stream = File.Open(identifier, FileMode.Open);

            return(new DataResolverResult(stream, LoadingResult.Disk, imageInformation));
        }
        public async Task <UIImageData> GetData(string identifier, CancellationToken token)
        {
            UIImage image = null;
            await _mainThreadDispatcher.PostAsync(() => image = UIImage.FromBundle(identifier)).ConfigureAwait(false);

            var imageInformation = new ImageInformation();

            imageInformation.SetPath(identifier);
            imageInformation.SetFilePath(null);

            return(new UIImageData()
            {
                Image = image,
                Result = LoadingResult.CompiledResource,
                ResultIdentifier = identifier,
                ImageInformation = imageInformation
            });
        }
        public virtual Task<Tuple<Stream, LoadingResult, ImageInformation>> Resolve(string identifier, TaskParameter parameters, CancellationToken token)
        {
            if (!FileStore.Exists(identifier))
            {
                throw new FileNotFoundException(identifier);
            }

            token.ThrowIfCancellationRequested();

            var stream = FileStore.GetInputStream(identifier, true);

            var imageInformation = new ImageInformation();
            imageInformation.SetPath(identifier);
            imageInformation.SetFilePath(identifier);

            return Task.FromResult(new Tuple<Stream, LoadingResult, ImageInformation>(
                stream, LoadingResult.Disk, imageInformation));          
        }
Esempio n. 31
0
        public async virtual Task <DataResolverResult> Resolve(string identifier, TaskParameter parameters, CancellationToken token)
        {
            var downloadedData = await DownloadCache.DownloadAndCacheIfNeededAsync(identifier, parameters, Configuration, token).ConfigureAwait(false);

            if (token.IsCancellationRequested)
            {
                downloadedData?.ImageStream.TryDispose();
                token.ThrowIfCancellationRequested();
            }

            var imageInformation = new ImageInformation();

            imageInformation.SetPath(identifier);
            imageInformation.SetFilePath(downloadedData?.FilePath);

            return(new DataResolverResult(
                       downloadedData?.ImageStream, downloadedData.RetrievedFromDiskCache ? LoadingResult.DiskCache : LoadingResult.Internet, imageInformation));
        }
Esempio n. 32
0
		public async Task<UIImageData> GetData(string identifier, CancellationToken token)
		{
			var downloadedData = await DownloadCache.GetAsync(identifier, token, Parameters.CacheDuration, Parameters.CustomCacheKey, Parameters.CacheType).ConfigureAwait(false);
			var bytes = downloadedData.Bytes;
			var path = downloadedData.CachedPath;
			var result = downloadedData.RetrievedFromDiskCache ? LoadingResult.DiskCache : LoadingResult.Internet;

			var imageInformation = new ImageInformation();
			imageInformation.SetPath(identifier);
			imageInformation.SetFilePath(await DownloadCache.GetDiskCacheFilePathAsync(identifier, Parameters.CustomCacheKey));

			return new UIImageData() { 
				Data = bytes, 
				Result = result, 
				ResultIdentifier = path,
				ImageInformation = imageInformation
			};
		}
        public async virtual Task<Tuple<Stream, LoadingResult, ImageInformation>> Resolve(string identifier, TaskParameter parameters, CancellationToken token)
        {
            StorageFile file = null;

            try
            {
                string resPath = identifier.TrimStart('\\', '/');

                if (!resPath.StartsWith(@"Assets\") && !resPath.StartsWith("Assets/"))
                {
                    resPath = @"Assets\" + resPath;
                }

                var imgUri = new Uri("ms-appx:///" + resPath);
                file = await StorageFile.GetFileFromApplicationUriAsync(imgUri);
            }
            catch (Exception)
            {
                try
                {
                    var imgUri = new Uri("ms-appx:///" + identifier);
                    file = await StorageFile.GetFileFromApplicationUriAsync(imgUri);
                }
                catch (Exception)
                {
                }
            }


            if (file != null)
            {
                var imageInformation = new ImageInformation();
                imageInformation.SetPath(identifier);
                imageInformation.SetFilePath(file.Path);

                token.ThrowIfCancellationRequested();
                var stream = await file.OpenStreamForReadAsync();

                return new Tuple<Stream, LoadingResult, ImageInformation>(stream, LoadingResult.CompiledResource, imageInformation);
            }

            throw new FileNotFoundException(identifier);
        }
        public async virtual Task <Tuple <Stream, LoadingResult, ImageInformation> > Resolve(string identifier, TaskParameter parameters, CancellationToken token)
        {
            StorageFile file = null;

            try
            {
                string resPath = identifier.TrimStart('\\', '/');

                if (!resPath.StartsWith(@"Assets\") && !resPath.StartsWith("Assets/"))
                {
                    resPath = @"Assets\" + resPath;
                }

                var imgUri = new Uri("ms-appx:///" + resPath);
                file = await StorageFile.GetFileFromApplicationUriAsync(imgUri);
            }
            catch (Exception)
            {
                try
                {
                    var imgUri = new Uri("ms-appx:///" + identifier);
                    file = await StorageFile.GetFileFromApplicationUriAsync(imgUri);
                }
                catch (Exception)
                {
                }
            }


            if (file != null)
            {
                var imageInformation = new ImageInformation();
                imageInformation.SetPath(identifier);
                imageInformation.SetFilePath(file.Path);

                token.ThrowIfCancellationRequested();
                var stream = await file.OpenStreamForReadAsync();

                return(new Tuple <Stream, LoadingResult, ImageInformation>(stream, LoadingResult.CompiledResource, imageInformation));
            }

            throw new FileNotFoundException(identifier);
        }
Esempio n. 35
0
        public virtual Task <DataResolverResult> Resolve(string identifier, TaskParameter parameters, CancellationToken token)
        {
            if (!FileStore.Exists(identifier))
            {
                throw new FileNotFoundException(identifier);
            }

            token.ThrowIfCancellationRequested();

            var stream = FileStore.GetInputStream(identifier, true);

            var imageInformation = new ImageInformation();

            imageInformation.SetPath(identifier);
            imageInformation.SetFilePath(identifier);

            return(Task.FromResult(new DataResolverResult(
                                       stream, LoadingResult.Disk, imageInformation)));
        }
        private async Task <UIImageData> GetDataInternal(string identifier, CancellationToken token)
        {
            var bytes = await FileStore.ReadBytesAsync(identifier, token).ConfigureAwait(false);

            var result = (LoadingResult)(int)_source;             // Some values of ImageSource and LoadingResult are shared

            var imageInformation = new ImageInformation();

            imageInformation.SetPath(identifier);
            imageInformation.SetFilePath(identifier);

            return(new UIImageData()
            {
                Data = bytes,
                Result = result,
                ResultIdentifier = identifier,
                ImageInformation = imageInformation
            });
        }
        public async Task <WithLoadingResult <Stream> > GetStream(string identifier, CancellationToken token)
        {
            StorageFile file = null;

            try
            {
                string resPath = identifier.TrimStart('\\', '/');

                if (!resPath.StartsWith(@"Assets\") && !resPath.StartsWith("Assets/"))
                {
                    resPath = @"Assets\" + resPath;
                }

                var imgUri = new Uri("ms-appx:///" + resPath);
                file = await StorageFile.GetFileFromApplicationUriAsync(imgUri);
            }
            catch (Exception)
            {
                try
                {
                    var imgUri = new Uri("ms-appx:///" + identifier);
                    file = await StorageFile.GetFileFromApplicationUriAsync(imgUri);
                }
                catch (Exception)
                {
                }
            }


            if (file != null)
            {
                var imageInformation = new ImageInformation();
                imageInformation.SetPath(identifier);
                imageInformation.SetFilePath(file.Path);

                return(WithLoadingResult.Encapsulate(await file.OpenStreamForReadAsync(), LoadingResult.CompiledResource, imageInformation));
            }

            return(WithLoadingResult.Encapsulate <Stream>(null, LoadingResult.CompiledResource));
        }
Esempio n. 38
0
        public Task <Tuple <Stream, LoadingResult, ImageInformation> > Resolve(string identifier, TaskParameter parameters, CancellationToken token)
        {
            var fileName  = Path.GetFileNameWithoutExtension(identifier);
            var extension = Path.GetExtension(identifier).TrimStart('.');

            var bundle = NSBundle._AllBundles.FirstOrDefault(bu => !string.IsNullOrEmpty(bu.PathForResource(fileName, extension)));

            if (bundle != null)
            {
                var url    = bundle.GetUrlForResource(fileName, extension);
                var stream = FileStore.GetInputStream(url.Path, true);

                var imageInformation = new ImageInformation();
                imageInformation.SetPath(identifier);
                imageInformation.SetFilePath(url.Path);

                return(Task.FromResult(new Tuple <Stream, LoadingResult, ImageInformation>(
                                           stream, LoadingResult.CompiledResource, imageInformation)));
            }

            throw new FileNotFoundException(identifier);
        }
Esempio n. 39
0
        private async Task <DataResolverResult> ResolveFromAssetCatalogAsync(string fileName, string identifier, TaskParameter parameters, CancellationToken token)
        {
#if __IOS__ || __TVOS__
            if (!UIDevice.CurrentDevice.CheckSystemVersion(9, 0))
            {
                return(null);
            }
#endif

            NSDataAsset asset = null;

            try
            {
                await ImageService.Instance.Config.MainThreadDispatcher.PostAsync(() => asset = new NSDataAsset(identifier, NSBundle.MainBundle)).ConfigureAwait(false);
            }
            catch { }

            if (asset == null && fileName != identifier)
            {
                try
                {
                    await ImageService.Instance.Config.MainThreadDispatcher.PostAsync(() => asset = new NSDataAsset(fileName, NSBundle.MainBundle)).ConfigureAwait(false);
                }
                catch { }
            }

            if (asset != null)
            {
                token.ThrowIfCancellationRequested();
                var stream           = asset.Data?.AsStream();
                var imageInformation = new ImageInformation();
                imageInformation.SetPath(identifier);
                imageInformation.SetFilePath(null);

                return(new DataResolverResult(stream, LoadingResult.CompiledResource, imageInformation));
            }

            return(null);
        }
Esempio n. 40
0
        public Task <Tuple <Stream, LoadingResult, ImageInformation> > Resolve(string identifier, TaskParameter parameters, CancellationToken token)
        {
            if (!identifier.StartsWith("resource://", StringComparison.OrdinalIgnoreCase))
            {
                throw new Exception("Only resource:// scheme is supported");
            }


            Uri      uri      = new Uri(identifier);
            Assembly assembly = null;

            var    parts        = uri.OriginalString.Substring(11).Split('?');
            string resourceName = parts.First();

            if (parts.Count() > 1)
            {
                var name         = Uri.UnescapeDataString(uri.Query.Substring(10));
                var assemblyName = new AssemblyName(name);
                assembly = Assembly.Load(assemblyName);
            }

            if (assembly == null)
            {
                MethodInfo callingAssemblyMethod = typeof(Assembly).GetTypeInfo().GetDeclaredMethod("GetCallingAssembly");
                assembly = (Assembly)callingAssemblyMethod.Invoke(null, new object[0]);
            }

            var imageInformation = new ImageInformation();

            imageInformation.SetPath(identifier);
            imageInformation.SetFilePath(identifier);

            var stream = assembly.GetManifestResourceStream(resourceName);

            return(Task.FromResult(new Tuple <Stream, LoadingResult, ImageInformation>(
                                       stream, LoadingResult.EmbeddedResource, imageInformation)));
        }
Esempio n. 41
0
#pragma warning disable CS1998 // 이 비동기 메서드에는 'await' 연산자가 없으며 메서드가 동시에 실행됩니다.
        public virtual async Task <DataResolverResult> Resolve(string identifier, TaskParameter parameters, CancellationToken token)
#pragma warning restore CS1998 // 이 비동기 메서드에는 'await' 연산자가 없으며 메서드가 동시에 실행됩니다.
        {
            try
            {
                if (TrimmedResource.ContainsKey(identifier))
                {
                    identifier = TrimmedResource[identifier];
                }

                var data  = Application.GetResourceStream(new Uri(identifier, UriKind.Relative));
                var image = new ImageInformation();
                image.SetPath(identifier);
                image.SetFilePath(identifier);

                token.ThrowIfCancellationRequested();

                return(new DataResolverResult(data.Stream, LoadingResult.CompiledResource, image));
            }
            catch (IOException io)
            {
                throw new FileNotFoundException(identifier, io);
            }
        }
        public virtual async Task <Tuple <Stream, LoadingResult, ImageInformation> > Resolve(string identifier, TaskParameter parameters, CancellationToken token)
        {
            NSBundle bundle       = null;
            var      filename     = Path.GetFileNameWithoutExtension(identifier);
            var      tmpPath      = Path.GetDirectoryName(identifier).Trim('/');
            var      filenamePath = string.IsNullOrWhiteSpace(tmpPath) ? null : tmpPath + "/";

            foreach (var fileType in fileTypes)
            {
                string file      = null;
                var    extension = Path.HasExtension(identifier) ? Path.GetExtension(identifier) : string.IsNullOrWhiteSpace(fileType) ? string.Empty : "." + fileType;

                token.ThrowIfCancellationRequested();

                int scale = (int)ScaleHelper.Scale;
                if (scale > 1)
                {
                    while (scale > 1)
                    {
                        token.ThrowIfCancellationRequested();

                        var tmpFile = string.Format("{0}@{1}x{2}", filename, scale, extension);
                        bundle = NSBundle._AllBundles.FirstOrDefault(bu =>
                        {
                            var path = string.IsNullOrWhiteSpace(filenamePath) ?
                                       bu.PathForResource(tmpFile, null) :
                                       bu.PathForResource(tmpFile, null, filenamePath);
                            return(!string.IsNullOrWhiteSpace(path));
                        });

                        if (bundle != null)
                        {
                            file = tmpFile;
                            break;
                        }
                        scale--;
                    }
                }

                token.ThrowIfCancellationRequested();

                if (file == null)
                {
                    var tmpFile = string.Format(filename + extension);
                    file   = tmpFile;
                    bundle = NSBundle._AllBundles.FirstOrDefault(bu =>
                    {
                        var path = string.IsNullOrWhiteSpace(filenamePath) ?
                                   bu.PathForResource(tmpFile, null) :
                                   bu.PathForResource(tmpFile, null, filenamePath);

                        return(!string.IsNullOrWhiteSpace(path));
                    });
                }

                token.ThrowIfCancellationRequested();

                if (bundle != null)
                {
                    string path = !string.IsNullOrEmpty(filenamePath) ? bundle.PathForResource(file, null, filenamePath) : bundle.PathForResource(file, null);

                    var stream           = FileStore.GetInputStream(path, true);
                    var imageInformation = new ImageInformation();
                    imageInformation.SetPath(identifier);
                    imageInformation.SetFilePath(path);

                    return(new Tuple <Stream, LoadingResult, ImageInformation>(
                               stream, LoadingResult.CompiledResource, imageInformation));
                }

                token.ThrowIfCancellationRequested();

                if (string.IsNullOrEmpty(fileType))
                {
                    //Asset catalog
                    NSDataAsset asset = null;

                    try
                    {
                        await MainThreadDispatcher.Instance.PostAsync(() => asset = new NSDataAsset(filename)).ConfigureAwait(false);
                    }
                    catch (Exception) { }

                    if (asset != null)
                    {
                        token.ThrowIfCancellationRequested();
                        var stream           = asset.Data?.AsStream();
                        var imageInformation = new ImageInformation();
                        imageInformation.SetPath(identifier);
                        imageInformation.SetFilePath(null);

                        return(new Tuple <Stream, LoadingResult, ImageInformation>(
                                   stream, LoadingResult.CompiledResource, imageInformation));
                    }


                    NSImage image = null;

                    try
                    {
                        await MainThreadDispatcher.Instance.PostAsync(() => image = NSImage.ImageNamed(filename)).ConfigureAwait(false);
                    }
                    catch (Exception) { }

                    if (image != null)
                    {
                        token.ThrowIfCancellationRequested();
                        var imageRep = new NSBitmapImageRep(image.AsTiff());
                        var stream   = imageRep.RepresentationUsingTypeProperties(NSBitmapImageFileType.Png)
                                       .AsStream();
                        //var stream = image.AsPNG()?.AsStream();
                        var imageInformation = new ImageInformation();
                        imageInformation.SetPath(identifier);
                        imageInformation.SetFilePath(null);

                        return(new Tuple <Stream, LoadingResult, ImageInformation>(
                                   stream, LoadingResult.CompiledResource, imageInformation));
                    }
                }
            }

            throw new FileNotFoundException(identifier);
        }
        public virtual async Task<Tuple<Stream, LoadingResult, ImageInformation>> Resolve(string identifier, TaskParameter parameters, CancellationToken token)
        {
            NSBundle bundle = null;
            string file = null;
            var filename = Path.GetFileNameWithoutExtension(identifier);
            var extension = Path.GetExtension(identifier);
            const string pattern = "{0}@{1}x{2}";

            foreach (var fileType in fileTypes)
            {
                token.ThrowIfCancellationRequested();

                int scale = (int)ScaleHelper.Scale;
                if (scale > 1)
                {
                    while (scale > 1)
                    {
                        token.ThrowIfCancellationRequested();

                        var tmpFile = string.Format(pattern, filename, scale, extension);
                        bundle = NSBundle._AllBundles.FirstOrDefault(bu =>
                        {
                            var path = bu.PathForResource(tmpFile, fileType);
                            return !string.IsNullOrWhiteSpace(path);
                        });

                        if (bundle != null)
                        {
                            file = tmpFile;
                            break;
                        }
                        scale--;
                    }
                }

                token.ThrowIfCancellationRequested();

                if (file == null)
                {
                    file = identifier;
                    bundle = NSBundle._AllBundles.FirstOrDefault(bu =>
                    {
                        var path = bu.PathForResource(file, fileType);
                        return !string.IsNullOrWhiteSpace(path);
                    });
                }

                token.ThrowIfCancellationRequested();

                if (bundle != null)
                {
                    var path = bundle.PathForResource(file, fileType);

                    var stream = FileStore.GetInputStream(path, true);
                    var imageInformation = new ImageInformation();
                    imageInformation.SetPath(identifier);
                    imageInformation.SetFilePath(path);

                    return new Tuple<Stream, LoadingResult, ImageInformation>(
                        stream, LoadingResult.CompiledResource, imageInformation);
                }
            }

            //Asset catalog
            token.ThrowIfCancellationRequested();

            if (UIDevice.CurrentDevice.CheckSystemVersion(9, 0))
            {
                NSDataAsset asset = null;

                try
                {
                    await MainThreadDispatcher.Instance.PostAsync(() => asset = new NSDataAsset(filename)).ConfigureAwait(false);
                }
                catch (Exception) { }

                if (asset != null)
                {
                    token.ThrowIfCancellationRequested();
                    var stream = asset.Data?.AsStream();
                    var imageInformation = new ImageInformation();
                    imageInformation.SetPath(identifier);
                    imageInformation.SetFilePath(null);

                    return new Tuple<Stream, LoadingResult, ImageInformation>(
                        stream, LoadingResult.CompiledResource, imageInformation);
                }
            }
            else if (UIDevice.CurrentDevice.CheckSystemVersion(7, 0))
            {
                UIImage image = null;

                try
                {
                    await MainThreadDispatcher.Instance.PostAsync(() => image = UIImage.FromBundle(filename)).ConfigureAwait(false);
                }
                catch (Exception) { }

                if (image != null)
                {
                    token.ThrowIfCancellationRequested();
                    var stream = image.AsPNG()?.AsStream();
                    var imageInformation = new ImageInformation();
                    imageInformation.SetPath(identifier);
                    imageInformation.SetFilePath(null);

                    return new Tuple<Stream, LoadingResult, ImageInformation>(
                        stream, LoadingResult.CompiledResource, imageInformation);
                }
            }

            throw new FileNotFoundException(identifier);
        }