Ejemplo n.º 1
0
        private IActionResult DeliverAsset(IAssetEntity?asset, AssetQuery query)
        {
            query ??= new AssetQuery();

            if (asset == null || asset.FileVersion < query.Version)
            {
                return(NotFound());
            }

            if (asset.IsProtected && !this.HasPermission(Permissions.AppAssetsRead))
            {
                return(StatusCode(403));
            }

            var fileVersion = query.Version;

            if (fileVersion <= EtagVersion.Any)
            {
                fileVersion = asset.FileVersion;
            }

            Response.Headers[HeaderNames.ETag] = fileVersion.ToString();

            if (query.CacheDuration > 0)
            {
                Response.Headers[HeaderNames.CacheControl] = $"public,max-age={query.CacheDuration}";
            }

            var handler = new Func <Stream, Task>(async bodyStream =>
            {
                var resizeOptions = query.ToResizeOptions(asset);

                if (asset.Type == AssetType.Image && resizeOptions != null)
                {
                    var resizedAsset = $"{asset.Id}_{asset.FileVersion}_{resizeOptions}";

                    try
                    {
                        await assetStore.DownloadAsync(resizedAsset, bodyStream);
                    }
                    catch (AssetNotFoundException)
                    {
                        using (Profiler.Trace("Resize"))
                        {
                            using (var sourceStream = GetTempStream())
                            {
                                using (var destinationStream = GetTempStream())
                                {
                                    using (Profiler.Trace("ResizeDownload"))
                                    {
                                        await assetFileStore.DownloadAsync(asset.Id, fileVersion, 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(bodyStream);
                                }
                            }
                        }
                    }
                }
                else
                {
                    await assetFileStore.DownloadAsync(asset.Id, fileVersion, bodyStream);
                }
            });

            if (query.Download == 1 || asset.Type != AssetType.Image)
            {
                return(new FileCallbackResult(asset.MimeType, asset.FileName, true, handler));
            }
            else
            {
                return(new FileCallbackResult(asset.MimeType, null, true, handler));
            }
        }
Ejemplo n.º 2
0
        private IActionResult DeliverAsset(IAssetEntity?asset, AssetQuery query)
        {
            query ??= new AssetQuery();

            if (asset == null || asset.FileVersion < query.Version)
            {
                return(NotFound());
            }

            if (asset.IsProtected && !this.HasPermission(Permissions.AppAssetsRead))
            {
                return(StatusCode(403));
            }

            var fileVersion = query.Version;

            if (fileVersion <= EtagVersion.Any)
            {
                fileVersion = asset.FileVersion;
            }

            Response.Headers[HeaderNames.ETag] = fileVersion.ToString();

            if (query.CacheDuration > 0)
            {
                Response.Headers[HeaderNames.CacheControl] = $"public,max-age={query.CacheDuration}";
            }

            var handler = new Func <Stream, Task>(async bodyStream =>
            {
                var resizeOptions = query.ToResizeOptions(asset);

                if (asset.Type == AssetType.Image && resizeOptions.IsValid)
                {
                    var resizedAsset = $"{asset.Id}_{asset.FileVersion}_{resizeOptions}";

                    if (query.ForceResize)
                    {
                        await ResizeAsync(asset, bodyStream, resizedAsset, fileVersion, resizeOptions, true);
                    }
                    else
                    {
                        try
                        {
                            await assetStore.DownloadAsync(resizedAsset, bodyStream);
                        }
                        catch (AssetNotFoundException)
                        {
                            await ResizeAsync(asset, bodyStream, resizedAsset, fileVersion, resizeOptions, false);
                        }
                    }
                }
                else
                {
                    await assetFileStore.DownloadAsync(asset.Id, fileVersion, bodyStream);
                }
            });

            if (query.Download == 1 || asset.Type != AssetType.Image)
            {
                return(new FileCallbackResult(asset.MimeType, asset.FileName, true, handler));
            }
            else
            {
                return(new FileCallbackResult(asset.MimeType, null, true, handler));
            }
        }