通过代理到内存数据通过URL提供图像数据(Serve Image Data Via URL Through Proxy To In Memory Data)

编程入门 行业动态 更新时间:2024-10-24 14:19:11
通过代理到内存数据通过URL提供图像数据(Serve Image Data Via URL Through Proxy To In Memory Data)

目标:在IE11和Edge中渲染来自原始数据的图像。

问题:

该图像来自对仅返回原始图像数据的API的调用。 图像数据服务是内部的,不能也不应该直接从外部网络(互联网)访问。 数据可以在其他浏览器中使用发送到图像标签的图像的base64编码来呈现,但是它不会在IE中渲染因为数据总是会超过此类标签的32kb限制。

问题:在我的Rails应用程序中,是否可以设置某种代理,以便可以为我的Rails应用程序服务器构建一个URL并将其用于图像标记中,如<img src='http://my-rails-app-server.com/images/123'> ,然后打到该服务以检索图像数据,然后以某种方式将其返回到浏览器,以便获得图像源。

一些伪代码:

class ImageProxy < ApplicationController def show data = ExternalImageDataRetriever.get_data_from("internal-network-image-service/images/123") render something: data end end

Goal: Render an image from raw data in IE11 and Edge.

Problem:

The image is coming from a call to an API that only returns raw image data. The image data service is internal and cannot and should not be accessible directly from external networks (the internet). The data can be rendered in other browsers using a base64 encoding of the image sent into an image tag, <img src="data://image/jpeg;base64,#{base64encodedDataHere}">, but it will not render in IE as the data will always exceed the 32kb limit for this type of tag.

Question: In my Rails app, would it be possible to set up some kind of proxying such that I could build a URL to my Rails app server and use it in an image tag, such as <img src='http://my-rails-app-server.com/images/123'>, that would then hit the service to retrieve the image data and then return it to the browser in some way that it would expect for an image source.

Some pseudo code:

class ImageProxy < ApplicationController def show data = ExternalImageDataRetriever.get_data_from("internal-network-image-service/images/123") render something: data end end

最满意答案

事实证明这非常简单。

我已经构建了一个工具,向网络中的外部API请求图像。 调用API返回2个重要的东西:原始图像数据和MIME类型。

我所要做的就是通过铁轨传递这些信息。

控制器操作最终看起来像这样

class ImageProxy < ApplicationController def show service_response = ExternalImageDataRetriever.get_data_from("internal-network-image-service/images/123") render text: service_response.data, layout: nil, content_type: service_response.mime_type end end

之后,它只是在视图中的图像标记中使用该控制器操作的路由。

<%= image_tag image_proxy_path(id: 123) %>

Turns out it was exceptionally easy.

I already had built the tool that makes the request to the external API inside my network for the image. Calling the API returns 2 important things: the raw image data and the mime type.

All I had to do was pass these through rails.

The controller action ended up looking like this

class ImageProxy < ApplicationController def show service_response = ExternalImageDataRetriever.get_data_from("internal-network-image-service/images/123") render text: service_response.data, layout: nil, content_type: service_response.mime_type end end

After that it was just using the route to that controller action in an image tag in the view.

<%= image_tag image_proxy_path(id: 123) %>

更多推荐

本文发布于:2023-08-04 11:45:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1415323.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据   图像   内存   URL   Serve

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!