コード例 #1
0
        public async Task InvokeAsync(HttpContext context)
        {
            if (!IsImageRequested(context))
            {
                await _next(context);

                return;
            }

            var imagePath     = GetRequestedImagePath(context);
            var fullImagePath = Path.Combine(_storageConfig.Value.RootPath, imagePath);

            if (!File.Exists(fullImagePath))
            {
                context.Response.StatusCode = 404;
                return;
            }

            if (IsThumbnailRequested(context))
            {
                var size = CreateSizeFromQueryString(context);
                fullImagePath = ThumbnailHelper.GenerateThumbnail(fullImagePath, size);
            }

            await using var fs = new FileStream(fullImagePath, FileMode.Open);
            await fs.CopyToAsync(context.Response.Body);
        }
コード例 #2
0
        public virtual ActionResult Thumbnail(string path, int width, int height, string rotate)
        {
            try
            {
                path = StringHelper.TrimWhitespace(HttpUtility.UrlDecode(path));

                //NOTE: encode/decode lose double slash
                if (path.StartsWith("http:/") && !path.StartsWith("http://"))
                {
                    path = path.Replace("http:/", "http://");
                }

                if (path.StartsWith("https:/") && !path.StartsWith("https://"))
                {
                    path = path.Replace("https:/", "https://");
                }

                if (path.StartsWith("http") || path.StartsWith("https"))
                {
                    var fileExt       = Path.GetExtension(Models.UrlHelper.RemoveUrlParams(path));
                    var hash          = MD5Utils.GetMD5HashAsString(path) + fileExt;
                    var thumbnailFile = Server.MapPath("~/Cache/" + "W" + width + "xH" + height + "xR" + rotate + "x" + hash);
                    if (System.IO.File.Exists(thumbnailFile))
                    {
                        return(File(thumbnailFile, "image/png"));
                    }

                    LogI("Generate thumbnail from downloading image: " + path);

                    using (var srcImage = Image.FromStream(ImageHelper.DownloadRemoteImageFileAsStream(path)))
                    {
                        ThumbnailHelper.GenerateThumbnail(srcImage,
                                                          thumbnailFile,
                                                          width,
                                                          height,
                                                          rotate);

                        return(File(thumbnailFile,
                                    FileHelper.GetMimeTypeByExt(Path.GetExtension(thumbnailFile))));
                    }
                }
                else
                {
                    var imageFile = Models.UrlHelper.GetPathFromRelativeImageUrl(path);
                    //var imageFile = Server.MapPath(path); // Path.Combine(Server.MapPath("~/App_Data"), path);

                    //NOTE: for eBay
                    var fileExt  = Path.GetExtension(path);
                    var filename = FileHelper.PrepareFileName(Path.GetFileNameWithoutExtension(path));

                    var thumbnailFile = Server.MapPath("~/Cache/" + "W" + width + "xH" + height + "xR" + rotate + "x" + filename + fileExt);
                    if (System.IO.File.Exists(thumbnailFile))
                    {
                        return(File(thumbnailFile, "image/png"));
                    }

                    //WebImage img = new WebImage(path).Save();
                    if (System.IO.File.Exists(imageFile))
                    {
                        LogI("Generate thumbnail from local image: " + path);
                        using (var srcImage = Image.FromFile(imageFile))
                        {
                            ThumbnailHelper.GenerateThumbnail(srcImage,
                                                              thumbnailFile,
                                                              width,
                                                              height,
                                                              rotate);
                        }
                        return(File(thumbnailFile,
                                    FileHelper.GetMimeTypeByExt(Path.GetExtension(thumbnailFile))));
                    }
                }
            }
            catch (Exception ex)
            {
                LogE("Unable to display thumbnails", ex);
            }

            LogI("Display NO_IMAGE_URL for path=" + path);
            return(File(Server.MapPath(ImageHelper.NO_IMAGE_URL),
                        FileHelper.GetMimeTypeByExt(Path.GetExtension(ImageHelper.NO_IMAGE_URL))));
        }