Exemple #1
0
        async Task DownloadImagesAsync(IEnumerable <IEntityWithImage> entities, string folder)
        {
            var imagesFolder = Path.Combine(options.CurrentValue.ImagesOutDir, folder);

            Directory.CreateDirectory(imagesFolder);

            await Task.WhenAll(entities.Select(TryDownloadImagesAsync));

            async Task TryDownloadImagesAsync(IEntityWithImage entity)
            {
                try
                {
                    await repository.DownloadImageAsync(
                        entity,
                        () => CreateImageFile(entity),
                        applicationHostLifetime.ApplicationStopping);

                    logger.LogInformation("{Type} image found for : {Id}", entity.GetType().Name, entity.Id);
                }
                catch (Exception exception)
                {
                    logger.LogError(exception, "Failed loading {Type} image of {Id}", entity.GetType().Name, entity.Id);
                }
            }

            Task <Stream> CreateImageFile(IEntityWithImage entity)
            {
                Stream ouputStream = new FileStream(
                    Path.Combine(imagesFolder, entity.Id + ".jpg"),
                    FileMode.Create,
                    FileAccess.Write);

                return(Task.FromResult(ouputStream));
            }
        }