Perl 中的图标图像代理

发布于 2024-09-14 19:33:30 字数 480 浏览 1 评论 0原文

我正在尝试找出为我们的网站列表提供图标文件的正确方法。基本上,列表的图标可以来自少数不同服务(Flickr、Picasa、Google 静态地图、我们自己的内部图像托管服务等)的图像文件。图标的 URL 存储在我们的数据库中,因此我想通过简单地调用以下命令来显示每个列表图标:

http://www.example.com/listing/1234/icon

目前我一直在使用 CGI.pm 重定向到正确的图标 URL,但是,我想要文件直接显示,无需进行 301 重定向。以下是我们一直在使用的代码:

my $url = "http://www.example-service.com/image-123.gif";
print $query->redirect(-url=>$url);

如果有任何关于如何更新它以通过代理提供文件而无需重定向用户的建议和代码示例,我将不胜感激。预先感谢您的帮助!

I'm trying to figure out the right way to serve icon files for our site listings. Basically an icon for a listing can come from an image file from a handful of different services (Flickr, Picasa, Google Static Maps, our own internal image hosting service, etc). The URL of the icon is stored in our database so I'd like to enable each listing icon to be displayed by simply calling:

http://www.example.com/listing/1234/icon

Currently I have been using CGI.pm to do a redirect to the correct icon URL, however, I want the file to be directly displayed without having to do a 301 redirect. Here is the code for what we've been using:

my $url = "http://www.example-service.com/image-123.gif";
print $query->redirect(-url=>$url);

I would appreciate any suggestions and code examples of on how I could update this to serve the file via proxy without having to redirect the user. Thanks in advance for your help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初心 2024-09-21 19:33:30

使用 LWP 获取远程文件并将其打印出来。

#!/usr/local/bin/perl
use LWP::UserAgent;
use CGI;
my $q = CGI->new;
my $ua = LWP::UserAgent->new;
$ua->agent("MyApp/0.1");
my $url = 'http://www.example-service.com/image-123.gif';

# Create a request
my $req = HTTP::Request->new(GET => $url);

my $res = $ua->request($req);

if ($res->is_success) {
        print $q->header( $res->content_type );
        print $res->content;
} else {
        print $q->header( 'text/plain', $res->status_line );
        print $res->status_line, "\n";
}

或者,您可以为数据库编写一个触发器,当您添加新列表时,它会下载列表的图像并将其存储在 Webroot 中的某个位置或数据库本身中。

Use LWP to get the remote file and print it out.

#!/usr/local/bin/perl
use LWP::UserAgent;
use CGI;
my $q = CGI->new;
my $ua = LWP::UserAgent->new;
$ua->agent("MyApp/0.1");
my $url = 'http://www.example-service.com/image-123.gif';

# Create a request
my $req = HTTP::Request->new(GET => $url);

my $res = $ua->request($req);

if ($res->is_success) {
        print $q->header( $res->content_type );
        print $res->content;
} else {
        print $q->header( 'text/plain', $res->status_line );
        print $res->status_line, "\n";
}

Alternatively you could write a trigger for your database which downloads the image for the listing and stores it either in the webroot somewhere or in the database itself when you add a new listing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文