Exemplo n.º 1
0
        private async Task <FileOperationResult <IImage> > GetImageFileOperation(string type, string regionUrn, Guid id, int?width, int?height, Func <Task <FileOperationResult <IImage> > > action, EntityTagHeaderValue etag = null)
        {
            try
            {
                var sw       = Stopwatch.StartNew();
                var sizeHash = (width ?? 0) + (height ?? 0);
                var result   = await CacheManager.GetAsync($"urn:{type}_by_id_operation:{id}:{sizeHash}", action, regionUrn);

                if (!result.IsSuccess)
                {
                    return(new FileOperationResult <IImage>(result.IsNotFoundResult, result.Messages));
                }
                if (result.ETag == etag && etag != null)
                {
                    return(new FileOperationResult <IImage>(OperationMessages.NotModified));
                }
                var force     = width.HasValue || height.HasValue;
                var newWidth  = width ?? Configuration.MaximumImageSize.Width;
                var newHeight = height ?? Configuration.MaximumImageSize.Height;
                if (result?.Data?.Bytes != null)
                {
                    var resized = ImageHelper.ResizeImage(result?.Data?.Bytes, newWidth, newHeight, force);
                    if (resized != null)
                    {
                        result.Data.Bytes   = resized.Item2;
                        result.ETag         = EtagHelper.GenerateETag(HttpEncoder, result.Data.Bytes);
                        result.LastModified = DateTime.UtcNow;
                        if (resized.Item1)
                        {
                            Logger.LogTrace($"{type}: Resized [{id}], Width [{ newWidth}], Height [{ newHeight}], Forced [{ force }]");
                        }
                    }
                    else
                    {
                        Logger.LogTrace($"{type}: Image [{id}] Request returned Null Image, Forced [{ force }]");
                    }
                }

                sw.Stop();
                return(new FileOperationResult <IImage>(result.Messages)
                {
                    Data = result.Data,
                    ETag = result.ETag,
                    LastModified = result.LastModified,
                    ContentType = result.ContentType,
                    Errors = result?.Errors,
                    IsSuccess = result?.IsSuccess ?? false,
                    OperationTime = sw.ElapsedMilliseconds
                });
            }
            catch (Exception ex)
            {
                Logger.LogError(ex, $"GetImageFileOperation Error, Type [{type}], id [{id}]");
            }

            return(new FileOperationResult <IImage>("System Error"));
        }
Exemplo n.º 2
0
        private async Task <FileOperationResult <Image> > GetImageFileOperation(string type, string regionUrn, Guid id,
                                                                                int?width, int?height, Func <Task <FileOperationResult <Image> > > action, EntityTagHeaderValue etag = null)
        {
            try
            {
                var sw     = Stopwatch.StartNew();
                var result = (await CacheManager.GetAsync($"urn:{type}_by_id_operation:{id}", action, regionUrn))
                             .Adapt <FileOperationResult <Image> >();
                if (!result.IsSuccess)
                {
                    return(new FileOperationResult <Image>(result.IsNotFoundResult, result.Messages));
                }
                if (result.ETag == etag)
                {
                    return(new FileOperationResult <Image>(OperationMessages.NotModified));
                }
                if ((width.HasValue || height.HasValue) && result?.Data?.Bytes != null)
                {
                    result.Data.Bytes   = ImageHelper.ResizeImage(result?.Data?.Bytes, width.Value, height.Value);
                    result.ETag         = EtagHelper.GenerateETag(HttpEncoder, result.Data.Bytes);
                    result.LastModified = DateTime.UtcNow;
                    if (width.Value != Configuration.ThumbnailImageSize.Width ||
                        height.Value != Configuration.ThumbnailImageSize.Height)
                    {
                        Logger.LogTrace($"{type}: Resized [{id}], Width [{width.Value}], Height [{height.Value}]");
                    }
                }

                sw.Stop();
                return(new FileOperationResult <Image>(result.Messages)
                {
                    Data = result.Data,
                    ETag = result.ETag,
                    LastModified = result.LastModified,
                    ContentType = result.ContentType,
                    Errors = result?.Errors,
                    IsSuccess = result?.IsSuccess ?? false,
                    OperationTime = sw.ElapsedMilliseconds
                });
            }
            catch (Exception ex)
            {
                Logger.LogError(ex, $"GetImageFileOperation Error, Type [{type}], id [{id}]");
            }

            return(new FileOperationResult <Image>("System Error"));
        }
Exemplo n.º 3
0
        private FileOperationResult <Image> GenerateFileOperationResult(Guid id, data.Image image, EntityTagHeaderValue etag = null, string contentType = "image/jpeg")
        {
            var imageEtag = EtagHelper.GenerateETag(this.HttpEncoder, image.Bytes);

            if (EtagHelper.CompareETag(this.HttpEncoder, etag, imageEtag))
            {
                return(new FileOperationResult <Image>(OperationMessages.NotModified));
            }
            if (!image?.Bytes?.Any() ?? false)
            {
                return(new FileOperationResult <Image>(string.Format("ImageById Not Set [{0}]", id)));
            }
            return(new FileOperationResult <Image>(image?.Bytes?.Any() ?? false ? OperationMessages.OkMessage : OperationMessages.NoImageDataFound)
            {
                IsSuccess = true,
                Data = image.Adapt <Image>(),
                ContentType = contentType,
                LastModified = (image.LastUpdated ?? image.CreatedDate),
                ETag = imageEtag
            });
        }