From: Tara L Andrews Date: Mon, 20 Jan 2014 22:16:06 +0000 (+0100) Subject: add more robust download functionality; convert to new API in Stexaminer::graphsvg X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=6cf17f047a7cc39d7f0c691bad4df9839cae573c;p=scpubgit%2Fstemmaweb.git add more robust download functionality; convert to new API in Stexaminer::graphsvg --- diff --git a/lib/stemmaweb/Controller/Root.pm b/lib/stemmaweb/Controller/Root.pm index bc86c30..29e1414 100644 --- a/lib/stemmaweb/Controller/Root.pm +++ b/lib/stemmaweb/Controller/Root.pm @@ -513,26 +513,31 @@ sub stemmaroot :Local :Args(2) { =head2 download - GET /download/$textid + GET /download/$textid/$format -Returns the full XML definition of the tradition and its stemmata, if any. +Returns a file for download of the tradition in the requested format. =cut -sub download :Local :Args(1) { - my( $self, $c, $textid ) = @_; +sub download :Local :Args(2) { + my( $self, $c, $textid, $format ) = @_; my $tradition = $c->model('Directory')->tradition( $textid ); unless( $tradition ) { return _json_error( $c, 404, "No tradition with ID $textid" ); } my $ok = _check_permission( $c, $tradition ); return unless $ok; + + my $outmethod = "as_" . lc( $format ); + my $view = "View::$format"; + $c->stash->{'name'} = $tradition->name(); + $c->stash->{'download'} = 1; try { - $c->stash->{'result'} = $tradition->collation->as_graphml(); + $c->stash->{'result'} = $tradition->collation->$outmethod(); } catch( Text::Tradition::Error $e ) { return _json_error( $c, 500, $e->message ); } - $c->forward('View::GraphML'); + $c->forward( $view ); } #################### diff --git a/lib/stemmaweb/Controller/Stexaminer.pm b/lib/stemmaweb/Controller/Stexaminer.pm index ff4f69b..a7b8af8 100644 --- a/lib/stemmaweb/Controller/Stexaminer.pm +++ b/lib/stemmaweb/Controller/Stexaminer.pm @@ -157,9 +157,7 @@ sub graphsvg :Local { my( $self, $c ) = @_; my $dot = $c->request->param('dot'); my @layerwits = $c->request->param('layerwits[]'); - open my $stemma_fh, '<', \$dot; - binmode( $stemma_fh, ':encoding(UTF-8)' ); - my $tempstemma = Text::Tradition::Stemma->new( 'dot' => $stemma_fh ); + my $tempstemma = Text::Tradition::Stemma->new( 'dot' => $dot ); my $svgopts = {}; if( @layerwits ) { $svgopts->{'layerwits'} = \@layerwits; diff --git a/lib/stemmaweb/View/CSV.pm b/lib/stemmaweb/View/CSV.pm new file mode 100644 index 0000000..e39c9f1 --- /dev/null +++ b/lib/stemmaweb/View/CSV.pm @@ -0,0 +1,40 @@ +package stemmaweb::View::CSV; +use Moose; +use namespace::autoclean; + +extends 'Catalyst::View'; + +sub process { + my( $self, $c ) = @_; + $c->res->content_type( 'text/csv' ); + $c->res->content_encoding( 'UTF-8' ); + $c->res->header( 'Content-Disposition', + sprintf( "attachment; filename=\"%s.csv\"", $c->stash->{name} ) ); + $c->res->output( $c->stash->{result} ); +} + +=head1 NAME + +stemmaweb::View::CSV - Catalyst View + +=head1 DESCRIPTION + +Catalyst View. + + +=encoding utf8 + +=head1 AUTHOR + +Tara L Andrews + +=head1 LICENSE + +This library is free software. You can redistribute it and/or modify +it under the same terms as Perl itself. + +=cut + +__PACKAGE__->meta->make_immutable; + +1; diff --git a/lib/stemmaweb/View/GraphML.pm b/lib/stemmaweb/View/GraphML.pm index 3560dfc..5178bcd 100644 --- a/lib/stemmaweb/View/GraphML.pm +++ b/lib/stemmaweb/View/GraphML.pm @@ -7,6 +7,10 @@ sub process { my( $self, $c ) = @_; $c->res->content_type( 'application/graphml+xml' ); $c->res->content_encoding( 'UTF-8' ); + if( $c->stash->{download} ) { + $c->res->header( 'Content-Disposition', + sprintf( "attachment; filename=\"%s.xml\"", $c->stash->{name} ) ); + } $c->res->output( $c->stash->{result} ); } diff --git a/lib/stemmaweb/View/SVG.pm b/lib/stemmaweb/View/SVG.pm index b8e4673..13419e3 100644 --- a/lib/stemmaweb/View/SVG.pm +++ b/lib/stemmaweb/View/SVG.pm @@ -2,13 +2,16 @@ package stemmaweb::View::SVG; use strict; use base 'Catalyst::View'; -use Encode qw( decode_utf8 ); sub process { - my( $self, $c ) = @_; - $c->res->content_type( 'image/svg+xml' ); - $c->res->content_encoding( 'UTF-8' ); - $c->res->output( decode_utf8( $c->stash->{result} ) ); + my( $self, $c ) = @_; + $c->res->content_type( 'image/svg+xml' ); + $c->res->content_encoding( 'UTF-8' ); + if( $c->stash->{download} ) { + $c->res->header( 'Content-Disposition', + sprintf( "attachment; filename=\"%s.svg\"", $c->stash->{name} ) ); + } + $c->res->output( $c->stash->{result} ); } 1; diff --git a/lib/stemmaweb/View/TSV.pm b/lib/stemmaweb/View/TSV.pm new file mode 100644 index 0000000..261a8dd --- /dev/null +++ b/lib/stemmaweb/View/TSV.pm @@ -0,0 +1,40 @@ +package stemmaweb::View::TSV; +use Moose; +use namespace::autoclean; + +extends 'Catalyst::View'; + +sub process { + my( $self, $c ) = @_; + $c->res->content_type( 'text/tab-separated-values' ); + $c->res->content_encoding( 'UTF-8' ); + $c->res->header( 'Content-Disposition', + sprintf( "attachment; filename=\"%s.tsv\"", $c->stash->{name} ) ); + $c->res->output( $c->stash->{result} ); +} + +=head1 NAME + +stemmaweb::View::TSV - Catalyst View + +=head1 DESCRIPTION + +Catalyst View. + + +=encoding utf8 + +=head1 AUTHOR + +Tara L Andrews + +=head1 LICENSE + +This library is free software. You can redistribute it and/or modify +it under the same terms as Perl itself. + +=cut + +__PACKAGE__->meta->make_immutable; + +1; diff --git a/root/js/componentload.js b/root/js/componentload.js index 3f2f264..cd9307f 100644 --- a/root/js/componentload.js +++ b/root/js/componentload.js @@ -636,6 +636,33 @@ $(document).ready( function() { } }); + // Set up the download dialog + $('#download-dialog').dialog({ + autoOpen: false, + height: 150, + width: 300, + modal: true, + buttons: { + Download: function (evt) { + var dlurl = _get_url([ "download", $('#download_tradition').val(), $('#download_format').val() ]); + window.location = dlurl; + $('download-dialog').dialog('close'); + }, + Cancel: function() { + $('#download-dialog').dialog('close'); + } + }, + open: function() { + $('#download_tradition').attr('value', selectedTextID ); + }, + }).ajaxError( function(event, jqXHR, ajaxSettings, thrownError) { + $(event.target).parent().find('.ui-button').button("enable"); + if( ajaxSettings.url.indexOf( 'download' ) > -1 + && ajaxSettings.type == 'POST' ) { + display_error( jqXHR, $("#download_status") ); + } + }); + $('#upload-collation-dialog').dialog({ autoOpen: false, height: 360, diff --git a/root/src/index.tt b/root/src/index.tt index 884a68e..924a82d 100644 --- a/root/src/index.tt +++ b/root/src/index.tt @@ -91,11 +91,12 @@ var textOnLoad = "[% withtradition %]"; View collation and relationships - -
- Download tradition as XML +
+
+ Download tradition
-
+
@@ -176,6 +177,25 @@ var textOnLoad = "[% withtradition %]"; + +
+
+
+
+ + +
+
+
+
+
diff --git a/t/view_CSV.t b/t/view_CSV.t new file mode 100644 index 0000000..75e00bf --- /dev/null +++ b/t/view_CSV.t @@ -0,0 +1,8 @@ +use strict; +use warnings; +use Test::More; + + +BEGIN { use_ok 'stemmaweb::View::CSV' } + +done_testing(); diff --git a/t/view_TSV.t b/t/view_TSV.t new file mode 100644 index 0000000..66c2441 --- /dev/null +++ b/t/view_TSV.t @@ -0,0 +1,8 @@ +use strict; +use warnings; +use Test::More; + + +BEGIN { use_ok 'stemmaweb::View::TSV' } + +done_testing();