Ejemplo n.º 1
0
        public static async Task <IRandomAccessStream> GetIRandomAccessStreamFromUrlAsync(string url,
                                                                                          CancellationToken?token)
        {
            if (string.IsNullOrEmpty(url))
            {
                throw new UriFormatException("The url is null or empty.");
            }

            using (var client = new HttpClient())
            {
                if (token == null)
                {
                    token = CancellationTokenSourceFactory.CreateDefault(15000).Create().Token;
                }

                var downloadTask = client.GetAsync(new Uri(url), token.Value);

                token?.ThrowIfCancellationRequested();

                var response = await downloadTask;
                response.EnsureSuccessStatusCode();

                var streamTask = response.Content.ReadAsStreamAsync();

                token?.ThrowIfCancellationRequested();

                var stream = await streamTask;

                return(stream.AsRandomAccessStream());
            }
        }
Ejemplo n.º 2
0
        public static async Task <bool> GetStreamFromUrlAsync(string url,
                                                              CancellationToken?token, StorageFile file)
        {
            if (string.IsNullOrEmpty(url))
            {
                throw new UriFormatException("The url is null or empty.");
            }

            using (var client = new HttpClient())
            {
                if (token == null)
                {
                    token = CancellationTokenSourceFactory.CreateDefault().Create().Token;
                }

                using (var fs = await file.OpenStreamForWriteAsync())
                {
                    var resp = await client.GetAsync(new Uri(url), HttpCompletionOption.ResponseHeadersRead, token.Value);

                    var stream = await resp.Content.ReadAsStreamAsync();

                    await stream.CopyToAsync(fs);

                    stream.Dispose();
                }

                return(true);
            }
        }
Ejemplo n.º 3
0
        public async Task LoadBitmapAsync(bool setBitmap = true)
        {
            if (string.IsNullOrEmpty(RemoteUrl))
            {
                return;
            }

            var cacheKey = _cacheKeyFactory.ProvideKey(RemoteUrl);
            var file     = await _cacheSupplier.TryGetCacheAsync(cacheKey);

            if (file != null)
            {
                Debug.WriteLine($"====Find cache file: {cacheKey}");
                await SetImageSourceAsync(file);

                return;
            }

            Debug.WriteLine($"====Download file for: {cacheKey}");

            var token = CancellationTokenSourceFactory.CreateDefault(ImageDownloader.TIMEOUT_MILLIS).Create().Token;

            using (var stream = await ImageDownloader.GetEncodedImageFromUrlAsync(RemoteUrl, token))
            {
                var savedFile = await SaveEncodedImageToFileAsync(stream.AsStreamForRead());

                if (stream != null && setBitmap)
                {
                    stream.Seek(0);
                    await SetImageSourceAsync(stream);
                }
            }
        }
Ejemplo n.º 4
0
 public static async Task <IRandomAccessStream> GetEncodedImageFromUrlAsync(string url, CancellationToken?token)
 {
     if (token == null)
     {
         token = CancellationTokenSourceFactory.CreateDefault(TIMEOUT_MILLIS).Create().Token;
     }
     return(await GetEncodedImageFromUrlInternalAsync(url, token.Value));
 }
Ejemplo n.º 5
0
        public async Task TestSearchHasNoResultImages()
        {
            var service = new SearchImageService(new UnsplashImageFactory(false),
                                                 CancellationTokenSourceFactory.CreateDefault(), "dfafefsdfasfsdf");
            var result = await service.GetImagesAsync();

            Assert.IsTrue(result?.Count() == 0);
        }
Ejemplo n.º 6
0
 public HighlightImageService(UnsplashImageFactory factory,
                              CancellationTokenSourceFactory ctsFactory) : base(factory, ctsFactory)
 {
 }
Ejemplo n.º 7
0
 public SearchImageService(UnsplashImageFactory factory,
                           CancellationTokenSourceFactory ctsFactory,
                           string query) : base(factory, ctsFactory)
 {
     Query = query;
 }
Ejemplo n.º 8
0
 public ImageServiceBase(UnsplashImageFactory factory, CancellationTokenSourceFactory ctsFactory)
 {
     _ImageFactory = factory;
     _ctsFactory   = ctsFactory;
 }
Ejemplo n.º 9
0
 public VolService(string url, LuoVolFactory factory,
                   CancellationTokenSourceFactory ctsFactory) : base(factory, ctsFactory)
 {
     RequestUrl = url;
 }
Ejemplo n.º 10
0
 public RandomImageService(int count, UnsplashImageFactory factory,
                           CancellationTokenSourceFactory ctsFactory) : base(factory, ctsFactory)
 {
     Count = count;
 }
Ejemplo n.º 11
0
 public ImageService(string url, UnsplashImageFactory factory,
                     CancellationTokenSourceFactory ctsFactory) : base(factory, ctsFactory)
 {
     RequestUrl = url;
 }
        private async Task DownloadFromRemoteUrlAsync(bool setBitmap = true)
        {
            var cachedFolder = ApplicationData.Current.TemporaryFolder;

            if (!string.IsNullOrEmpty(ExpectedFileName))
            {
                var file = await cachedFolder.TryGetFileAsync(ExpectedFileName);

                if (file != null)
                {
                    LocalPath = file.Path;
                    File      = file;
                    await SetImageSourceAsync(file);

                    return;
                }
            }
            else
            {
                ExpectedFileName = GenerateRandomFileName();
            }

            if (string.IsNullOrEmpty(RemoteUrl))
            {
                return;
            }

            using (var stream = await FileDownloader.GetIRandomAccessStreamFromUrlAsync(this.RemoteUrl,
                                                                                        CancellationTokenSourceFactory.CreateDefault().Create().Token))
            {
                var file = await SaveStreamIntoFileAsync(stream.AsStream(), ExpectedFileName, cachedFolder);

                if (file != null)
                {
                    LocalPath = file.Path;
                    File      = file;
                }
                stream.Seek(0);
                if (stream != null && setBitmap)
                {
                    await SetImageSourceAsync(stream);
                }
            }
        }