如何在 Catalyst 中根据文件大小取消文件上传

发布于 2024-09-06 16:41:52 字数 584 浏览 6 评论 0原文

我正在编写一个文件上传处理程序 Catalyst。我正在尝试限制最大文件大小。为此,我制作了一个插件(基于 在这里回答)。这是我检查文件大小的代码:

before 'prepare_body' => sub {
    my $c = shift;

    my $req = $c->request;
    my $length = $req->headers->{"content-length"};
    if ($length > 10000)
    {
        $c->stash->{errors} = "File upload error";
        # how do I abort the upload?
    }
};

这正确地检测到太大的文件,但我无法弄清楚如何中止上传。理想情况下,它还应该到达控制器/操作。谁能给我指点一下吗?多谢。

I'm writing a file upload handler Catalyst. I'm trying to restrict the maximum file size. To do this I've made a Plugin (based on the answer here). Here is the code where I check for the file size:

before 'prepare_body' => sub {
    my $c = shift;

    my $req = $c->request;
    my $length = $req->headers->{"content-length"};
    if ($length > 10000)
    {
        $c->stash->{errors} = "File upload error";
        # how do I abort the upload?
    }
};

This correctly detects files that are too big, but I can't for the life of me figure out how to abort the upload. Ideally, it should also reach the controller/action. Can anyone give me a pointer? Thanks a lot.

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

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

发布评论

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

评论(2

原来分手还会想你 2024-09-13 16:41:52

很简单,你可能不应该这样做。您从插件代码中执行的任何中止处理操作都将削弱用户代码以良好方式处理情况的能力(例如,通过提供验证错误或良好的错误页面,而不是 Catalyst 异常页面) 。

然而,一切并没有失去。为什么不尝试这样的事情呢?

around 'prepare_body' => sub {
  my ($orig, $self) = (shift, shift);
  my ($c) = @_;

  my $max_length = $c->config->{'Plugin::WhateverMyNameIs'}->{max_request_size};
  $max_length = 1_000_000 unless defined $max_length; # default
  my $length = $c->engine->read_length;
  if ($length <= $max_length) { # ok, go ahead
    $self->$orig(@_);
  } else {
    $c->stash->{request_body_aborted} = 1;
  }
};

如果您的请求过大,这将停止读取,但它会让调度正常进行——这意味着您需要在操作中或在 begin 操作中编写一些代码,或者在链根中,检查 $c->stash->{request_body_aborted} 并执行适当的操作 - 无论是设置表单验证错误,还是调用 $c-> ;error("请求太大"); $c->detach 或其他。它也是可配置的,就像任何插件一样。

Very simply, you probably shouldn't. Anything you do from plugin code to abort the handling is going to knock out the ability of user code to deal with the situation in a nice way (for example by giving a validation error or a nice error page, instead of a Catalyst exception page).

However, all is not lost. Why not try something like this?

around 'prepare_body' => sub {
  my ($orig, $self) = (shift, shift);
  my ($c) = @_;

  my $max_length = $c->config->{'Plugin::WhateverMyNameIs'}->{max_request_size};
  $max_length = 1_000_000 unless defined $max_length; # default
  my $length = $c->engine->read_length;
  if ($length <= $max_length) { # ok, go ahead
    $self->$orig(@_);
  } else {
    $c->stash->{request_body_aborted} = 1;
  }
};

This will stop the read if your request is over-size, but it will let dispatch proceed as normal -- which means you will want to write some code in your action, or in a begin action, or in a chain root, that checks for $c->stash->{request_body_aborted} and does something appropriate -- whether that's setting a form validation error, or calling $c->error("Request too large"); $c->detach or whatever. It's also configurable, as any plugin should be.

时光清浅 2024-09-13 16:41:52

我认为这需要在链条的早期发生。如果您有标头,则数据包已创建。

也许你可以尝试: $c->detach();或者可能循环遍历 $c->stack 数组并删除可能已添加的与您的上传相关的操作。

I think this needs to occur earlier in the chain. If you have the headers, then the packet is already created.

Perhaps you could try: $c->detach(); or possibly loop through the $c->stack array and remove actions that might have been added, related to your upload.

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