Beispiel #1
0
 /// <summary>
 /// Write a data to local cache folder.
 /// </summary>
 /// <param name="this">The image cacher</param>
 /// <param name="relativePath">Path relative to temporary folder to file where a data will be stored</param>
 /// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
 /// <param name="getStream">Provide a stream which must be written to cache</param>
 /// <returns>Full path to file where a cached data is stored</returns>
 /// <remarks>
 /// This function will create a subfolder and a file if not exists in path relative to cache folder. Then
 /// open it and call a getStream callback to copy an incomming data.
 /// </remarks>
 public static Task <string> CacheAsync(this IImageCacher @this, string relativePath, CancellationToken cancellationToken, Func <Stream> getStream)
 {
     return(@this.CacheAsync(relativePath, cancellationToken, async(s, ct) =>
     {
         using (var stream = getStream())
             await stream.CopyToAsync(s, 80 * 1024, ct);
     }));
 }
        public async Task <ToastImageSource> FromResourceAsync(string resourcePath, Assembly assembly, CancellationToken cancellationToken = default)
        {
            var asn    = assembly.GetName();
            var fullFn = await imageCacher.CacheAsync(
                resourceToFileNameStrategy.Convert(resourcePath, assembly),
                cancellationToken, () => assembly.GetManifestResourceStream(resourcePath));

            var result = await FromFileAsync(fullFn, cancellationToken);

            return(result);
        }
Beispiel #3
0
        public async Task <ToastImageSource> FromUriAsync(Uri uri, CancellationToken cancellationToken = default)
        {
            string contentType = "";
            var    fullFn      = await imageCacher.CacheAsync(uriToFileNameStrategy.Convert(uri), cancellationToken, async (stream, ct) =>
            {
                var hc = httpClientFactory.CreateClient(nameof(IToastImageSourceFactory));
                using (var response = await hc.GetAsync(uri))
                {
                    contentType = response.Content.Headers.ContentType.MediaType;
                    using (var src = await response.Content.ReadAsStreamAsync())
                        await src.CopyToAsync(stream, 1024 * 80, cancellationToken);
                }
            });

            if (string.IsNullOrEmpty(contentType))
            {
                using (var fs = File.OpenRead(fullFn))
                    contentType = await mimeDetector.DetectAsync(fs);
            }
            return(new SealedToastImageSource(CreateAttachment(uri.ToString(), NSUrl.FromFilename(fullFn),
                                                               UTType.CreatePreferredIdentifier(UTType.TagClassMIMEType, contentType, null))));
        }
Beispiel #4
0
 /// <summary>
 /// Write a data to local cache folder.
 /// </summary>
 /// <param name="this">The image cacher</param>
 /// <param name="relativePath">Path relative to temporary folder to file where a data will be stored</param>
 /// <param name="getStream">Provide a stream which must be written to cache</param>
 /// <returns>Full path to file where a cached data is stored</returns>
 /// <remarks>
 /// This function will create a subfolder and a file if not exists in path relative to cache folder. Then
 /// open it and call a getStream callback to copy an incomming data.
 /// </remarks>
 public static Task <string> CacheAsync(this IImageCacher @this, string relativePath, Func <Stream> getStream)
 => @this.CacheAsync(relativePath, default, getStream);
Beispiel #5
0
 /// <summary>
 /// Write a data to local cache folder.
 /// </summary>
 /// <param name="this">The image cacher</param>
 /// <param name="relativePath">Path relative to temporary folder to file where a data will be stored</param>
 /// <param name="getStreamAsync">Provide a stream which must be written to cache</param>
 /// <returns>Full path to file where a cached data is stored</returns>
 /// <remarks>
 /// This function will create a subfolder and a file if not exists in path relative to cache folder. Then
 /// open it and call a getStreamAsync callback to copy an incomming data.
 /// </remarks>
 public static Task <string> CacheAsync(this IImageCacher @this, string relativePath, Func <CancellationToken, Task <Stream> > getStreamAsync)
 => @this.CacheAsync(relativePath, default, getStreamAsync);
Beispiel #6
0
 /// <summary>
 /// Write a data to local cache folder.
 /// </summary>
 /// <param name="this">The image cacher</param>
 /// <param name="relativePath">Path relative to temporary folder to file where a data will be stored</param>
 /// <param name="copyToAsync">Callback which will write to provided file stream</param>
 /// <returns>Full path to file where a cached data is stored</returns>
 /// <remarks>
 /// This function will create a subfolder and a file if not exists in path relative to cache folder. Then
 /// open it and call a copyToAsync callback with a file stream as a parameter.
 /// </remarks>
 public static Task <string> CacheAsync(this IImageCacher @this, string relativePath, Func <Stream, CancellationToken, Task> copyToAsync)
 => @this.CacheAsync(relativePath, default, copyToAsync);