my
$model
=
'$c->model("DBIC::Page")'
;
sub
generate_export_filename {
my
(
$c
,
$export_type
) =
@_
;
my
$now
= DateTime->now();
my
$prefix
=
sprintf
(
"%s-%s-$export_type-%s"
,
$c
->fixw(
$c
->pref(
'name'
) ),
substr
(
$c
->stash->{page}->path, 1),
$now
->ymd .
'T'
.
$now
->hms(
'-'
)
);
$prefix
=~ s|/|_|g;
return
$prefix
;
}
sub
export_raw : Global {
my
(
$self
,
$c
) =
@_
;
my
$prefix
= generate_export_filename(
$c
,
'markup'
);
unless
(
$c
->res->{body} =
$c
->cache->get(
$prefix
) ) {
my
@pages
=
$c
->stash->{page}->descendants;
my
$archive
= Archive::Zip->new();
$archive
->addDirectory(
"$prefix/"
);
foreach
my
$page
(
@pages
) {
next
if
not
$page
->content;
$archive
->addString(
Encode::encode_utf8(
$page
->content->body),
$prefix
.
$page
->path . (
$page
->path eq
'/'
?
''
:
'/'
) .
'index'
);
}
my
$fh
= IO::Scalar->new( \
$c
->res->{body} );
$archive
->writeToFileHandle(
$fh
);
$c
->cache->set(
$prefix
,
$c
->res->body );
}
$c
->res->headers->header(
"Content-Type"
=>
'archive/zip'
);
$c
->res->headers->header(
"Content-Disposition"
=>
"attachment; filename=$prefix.zip"
);
}
sub
export_html : Global {
my
(
$self
,
$c
) =
@_
;
my
$prefix
= generate_export_filename(
$c
,
'html'
);
unless
(
$c
->res->{body} =
$c
->cache->get(
$prefix
) ) {
my
@pages
=
$c
->stash->{page}->descendants;
my
$archive
= Archive::Zip->new();
$archive
->addDirectory(
"$prefix/"
);
foreach
my
$page
(
@pages
) {
$c
->
log
->debug(
'Rendering '
.
$page
->path );
$archive
->addString(
Encode::encode_utf8(
$c
->subreq(
'/print'
, {
path
=>
$page
->path } )),
$prefix
.
$page
->path .
"/index.html"
);
}
my
$fh
= IO::Scalar->new( \
$c
->res->{body} );
$archive
->writeToFileHandle(
$fh
);
$c
->cache->set(
$prefix
,
$c
->res->body );
}
$c
->res->headers->header(
"Content-Type"
=>
'archive/zip'
);
$c
->res->headers->header(
"Content-Disposition"
=>
"attachment; filename=$prefix.zip"
);
}
1;