[Catalyst] Out of Memory - File delivery issue

Craig Chant craig at homeloanpartnership.com
Thu May 2 12:07:23 GMT 2013


Hi Neil,

My model is already 'Catalyst::Model::Factory::PerRequest'

So I have added an attribute...

sub prepare_arguments {

    my ($self,$c) = @_;
    return {,
        res         =>  $c->res
    };

}

So I now have my model bound to  $c->res

How do I assign the scalar CSV data to it?

Surely if I use $self->res->body($xls) in the model it's no different than doing it in the  Controller and is still going to fall over with 'Out of memory'?

I seem to be getting no-where trying to print direct to the browser...

I've tried..

        $c->response->header(
            Content_Type =>  'application/vnd.ms-excel',
            Content_Disposition => 'attachment;filename=NBCS_Export.csv'
            );
         $c->response->body(\*STDOUT);
         print $xls;

but the devel console shows an error...

Filehandle STDOUT opened only for output at C:/Perl/site/lib/Catalyst/Engine.pm line 68.

and all I end up with is an empty XSL file?

How do you output content direct to the browser bypassing Catalyst, I can't seem to get control of STDOUT?

Craig.

-----Original Message-----
From: neil.lunn [mailto:neil at mylunn.id.au]
Sent: 02 May 2013 12:52
To: The elegant MVC web framework
Subject: Re: [Catalyst] Out of Memory - File delivery issue

On 2/05/2013 8:47 PM, Craig Chant wrote:
> Is it possible to have the controller print direct to browser...
Which is exactly what was in my reply and also mentioned in the suggested thread.
>
> So instead of what I currently have...
>
> ----------------------------------
> # run export
> my ($result,$xls) = $c->model('NBCSXLSExport')->csv_export;
So what you can do in the model is rather than have the method return the scalars that it currently is, have them output to a supplied filehandle instead. Either as an argument to instantiating the model or as an argument to the method called. Whichever suits.

$c->model('NBCSXLSExport', $c->res)->csv_export;

or
  $c->model('NBCSXLSExport',)->csv_export( $c->res );

For more on the first approach look at the docs for Catalyst::Model::Factory.

In either case it's your underlying model code that needs the injected filehandle (or IO::Handle type object to be exact ) to be implemented as the output source.

Neil

>
> # check for ok status
>      if($result->ok)
>      {
>          $c->response->header(
>              Content_Type =>  'application/vnd.ms-excel',
>              Content_Disposition => 'attachment;filename=NBCS_Export.csv'
>              );
>          $c->response->body($xls);
>      }
>      else
>      {
>          $c->response->body( $result->message );
>      }
>
>      $c->response->status(200);
>
> ----------------------------------------------
> I replaced it with...
> ----------------------------------
>
>      # run export
>      my ($result,$xls) = $c->model('NBCSXLSExport')->csv_export;
>
>      # check for ok status
>      if($result->ok)
>      {
>          print "Content-Disposition: attachment;filename=NBCS_Export.csv\n";
>          print "Content-Type: application/vnd.ms-excel\n";
>          print "\n";
>          print $xls;
>      }
>      else
>      {
>          $c->response->body( $result->message );
>          $c->response->status(200);
>      }
> ----------------------------------------------
>
> However, the browser doesn't get delivered the file, the XLS content is displayed in the devel console, but nothing is outputted to the browser?
>
> Plus as I have overridden the default 'RenderView' , how do I call it for those methods in the controller that  want to use the 'RenderView' templating mechanism?
>
> All help in understanding how I get this CSV string outputted to the browser as a streaming XLS compatible file is appreciated.
>
> Regards,
>
> Craig.
>
>
> -----Original Message-----
> From: Lukas Thiemeier [mailto:spamcatcher at thiemeier.net]
> Sent: 02 May 2013 10:33
> To: catalyst at lists.scsys.co.uk
> Subject: Re: [Catalyst] Out of Memory - File delivery issue
>
> On 05/02/2013 09:54 AM, Craig Chant wrote:
>> Hi,
>>
>>
>>
>> I understand that Catalyst has a known issue with delivering
>> authenticated files via the response mechanism.
>>
>>
>>
>> What does the Catalyst community do to work around this problem?
>>
>>
>>
>> If I want to read a large file from disk or collate a large CSV / XML
>> file and deliver it direct to the browser, how do I do this?
>>
> Hi Craig,
>
> I never had problems delivering large files, but I don't know what you consider "large".
>
> About delivering large static files: I would recommend to use the web server to serve static files, no matter if they are large or not. You can set up authentication in your web server and use Catalyst::Authentication::Credential::Remote to use the webservers authentication in your Catalyst app.
>
> Another possibility is to use Static::Simple to deliver static files. I successfully delivered files > 4gb using Static::Simple. Is this large enough for you?
>
> AFAIK, Static::Simple has no build in support for authentication, but you could add a "around" modifier in YourApp.pm and do the authentication stuff before calling $c->$orig. You will have to take a look at the code in Static::Simple to find out what method to modify. I am aware that this is NOT a optimal solution. In fact, I think that this approach is very hacky and dirty. But without trying it: I am (almost) sure that it will work.
>
> The topic "creating large XML Files" has come up on this mailing list before. This thread might be helpful:
>
> http://www.gossamer-threads.com/lists/catalyst/users/30931
>
> If you are trying to create a XML File in Memory which is larger than your memory, you do have a problem. But that is not Catalyst specific at all. Try writing the XML Data to your hard disk, and deliver the resulting file.
>
> I guess someone with more knowledge about the Cat internals or more experience with processing large files can give you a better advice. But I still hope this info is useful.
>
> Cheers, Lukas
>
> _______________________________________________
> List: Catalyst at lists.scsys.co.uk
> Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
> Searchable archive:
> http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
> Dev site: http://dev.catalyst.perl.org/ This Email and any attachments
> contain confidential information and is intended solely for the individual to whom it is addressed. If this Email has been misdirected, please notify the author as soon as possible. If you are not the intended recipient you must not disclose, distribute, copy, print or rely on any of the information contained, and all copies must be deleted immediately. Whilst we take reasonable steps to try to identify any software viruses, any attachments to this e-mail may nevertheless contain viruses, which our anti-virus software has failed to identify. You should therefore carry out your own anti-virus checks before opening any documents. HomeLoan Partnership will not accept any liability for damage caused by computer viruses emanating from any attachment or other document supplied with this e-mail. HomeLoan Partnership reserves the right to monitor and archive all e-mail communications through its network. No representative or employee of HomeLoan Partnership has the authority to enter into any contract on behalf of HomeLoan Partnership by email. HomeLoan Partnership is a trading name of H L Partnership Limited, registered in England and Wales with Registration Number 5011722. Registered office: 26-34 Old Street, London, EC1V 9QQ. H L Partnership Limited is authorised and regulated by the Financial Conduct Authority.
>
> _______________________________________________
> List: Catalyst at lists.scsys.co.uk
> Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
> Searchable archive:
> http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
> Dev site: http://dev.catalyst.perl.org/



_______________________________________________
List: Catalyst at lists.scsys.co.uk
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
Dev site: http://dev.catalyst.perl.org/
This Email and any attachments contain confidential information and is intended solely for the individual to whom it is addressed. If this Email has been misdirected, please notify the author as soon as possible. If you are not the intended recipient you must not disclose, distribute, copy, print or rely on any of the information contained, and all copies must be deleted immediately. Whilst we take reasonable steps to try to identify any software viruses, any attachments to this e-mail may nevertheless contain viruses, which our anti-virus software has failed to identify. You should therefore carry out your own anti-virus checks before opening any documents. HomeLoan Partnership will not accept any liability for damage caused by computer viruses emanating from any attachment or other document supplied with this e-mail. HomeLoan Partnership reserves the right to monitor and archive all e-mail communications through its network. No representative or employee of HomeLoan Partnership has the authority to enter into any contract on behalf of HomeLoan Partnership by email. HomeLoan Partnership is a trading name of H L Partnership Limited, registered in England and Wales with Registration Number 5011722. Registered office: 26-34 Old Street, London, EC1V 9QQ. H L Partnership Limited is authorised and regulated by the Financial Conduct Authority.



More information about the Catalyst mailing list