Beispiel #1
0
        public async Task Should_ignore_exception_if_app_image_to_backup_does_not_exist()
        {
            var imageStream = new MemoryStream();

            var context = CreateBackupContext();

            A.CallTo(() => context.Writer.OpenBlobAsync(A <string> ._, ct))
            .Returns(imageStream);

            A.CallTo(() => appImageStore.DownloadAsync(appId, imageStream, ct))
            .Throws(new AssetNotFoundException("Image"));

            await sut.BackupEventAsync(Envelope.Create(new AppImageUploaded()), context, ct);
        }
        public async Task Should_ignore_exception_when_app_image_to_backup_does_not_exist()
        {
            var imageStream = new MemoryStream();

            var context = CreateBackupContext();

            A.CallTo(() => context.Writer.WriteBlobAsync(A <string> .Ignored, A <Func <Stream, Task> > .Ignored))
            .Invokes((string _, Func <Stream, Task> handler) => handler(imageStream));

            A.CallTo(() => appImageStore.DownloadAsync(appId, imageStream, default))
            .Throws(new AssetNotFoundException("Image"));

            await sut.BackupEventAsync(Envelope.Create(new AppImageUploaded()), context);
        }
Beispiel #3
0
        public IActionResult GetImage(string app)
        {
            if (App.Image == null)
            {
                return(NotFound());
            }

            var etag = App.Image.Etag;

            Response.Headers[HeaderNames.ETag] = etag;

            var callback = new FileCallback(async(body, range, ct) =>
            {
                var resizedAsset = $"{App.Id}_{etag}_Resized";

                try
                {
                    await assetStore.DownloadAsync(resizedAsset, body, ct: ct);
                }
                catch (AssetNotFoundException)
                {
                    using (Profiler.Trace("Resize"))
                    {
                        using (var sourceStream = GetTempStream())
                        {
                            using (var destinationStream = GetTempStream())
                            {
                                using (Profiler.Trace("ResizeDownload"))
                                {
                                    await appImageStore.DownloadAsync(App.Id, sourceStream);
                                    sourceStream.Position = 0;
                                }

                                using (Profiler.Trace("ResizeImage"))
                                {
                                    await assetThumbnailGenerator.CreateThumbnailAsync(sourceStream, destinationStream, ResizeOptions);
                                    destinationStream.Position = 0;
                                }

                                using (Profiler.Trace("ResizeUpload"))
                                {
                                    await assetStore.UploadAsync(resizedAsset, destinationStream);
                                    destinationStream.Position = 0;
                                }

                                await destinationStream.CopyToAsync(body, ct);
                            }
                        }
                    }
                }
            });

            return(new FileCallbackResult(App.Image.MimeType, callback)
            {
                ErrorAs404 = true
            });
        }
Beispiel #4
0
 private Task WriteAssetAsync(Guid appId, IBackupWriter writer)
 {
     return(writer.WriteBlobAsync(AvatarFile, async stream =>
     {
         try
         {
             await appImageStore.DownloadAsync(appId, stream);
         }
         catch (AssetNotFoundException)
         {
         }
     }));
 }
Beispiel #5
0
 private async Task WriteAssetAsync(DomainId appId, IBackupWriter writer,
                                    CancellationToken ct)
 {
     try
     {
         await using (var stream = await writer.OpenBlobAsync(AvatarFile, ct))
         {
             await appImageStore.DownloadAsync(appId, stream, ct);
         }
     }
     catch (AssetNotFoundException)
     {
     }
 }
Beispiel #6
0
        private async Task ResizeAsync(string resizedAsset, string mimeType, Stream target,
                                       CancellationToken ct)
        {
#pragma warning disable MA0040 // Flow the cancellation token
            using var activity = Telemetry.Activities.StartActivity("Resize");

            await using var assetOriginal = new TempAssetFile(resizedAsset, mimeType, 0);
            await using var assetResized  = new TempAssetFile(resizedAsset, mimeType, 0);

            var resizeOptions = new ResizeOptions
            {
                TargetWidth  = 50,
                TargetHeight = 50
            };

            using (Telemetry.Activities.StartActivity("Read"))
            {
                await using (var originalStream = assetOriginal.OpenWrite())
                {
                    await appImageStore.DownloadAsync(App.Id, originalStream);
                }
            }

            using (Telemetry.Activities.StartActivity("Resize"))
            {
                try
                {
                    await using (var originalStream = assetOriginal.OpenRead())
                    {
                        await using (var resizeStream = assetResized.OpenWrite())
                        {
                            await assetThumbnailGenerator.CreateThumbnailAsync(originalStream, mimeType, resizeStream, resizeOptions);
                        }
                    }
                }
                catch
                {
                    await using (var originalStream = assetOriginal.OpenRead())
                    {
                        await using (var resizeStream = assetResized.OpenWrite())
                        {
                            await originalStream.CopyToAsync(resizeStream);
                        }
                    }
                }
            }

            using (Telemetry.Activities.StartActivity("Save"))
            {
                try
                {
                    await using (var resizeStream = assetResized.OpenRead())
                    {
                        await assetStore.UploadAsync(resizedAsset, resizeStream);
                    }
                }
                catch (AssetAlreadyExistsException)
                {
                    return;
                }
            }

            using (Telemetry.Activities.StartActivity("Write"))
            {
                await using (var resizeStream = assetResized.OpenRead())
                {
                    await resizeStream.CopyToAsync(target, ct);
                }
            }
#pragma warning restore MA0040 // Flow the cancellation token
        }