BEGIN {
$EntityModel::UploadInstance::VERSION
=
'0.001'
;
}
};
sub
onstart { logInfo(
"Starting upload"
); }
sub
onfilename { logInfo(
"Filename is [%s]"
,
$_
[1]); }
sub
oncomplete { logInfo(
"Complete"
); }
sub
onheader { logInfo(
"Header [%s] is [%s]"
,
$_
[1],
$_
[2]); }
sub
ondata { logInfo(
"Data acquired"
); }
sub
oncancel { logInfo(
"Cancelled"
); }
BEGIN {
$EntityModel::Apache2::UploadHandler::VERSION
=
'0.001'
;
}
_logMask
=> {
type
=>
'debug'
}
};
sub
handler : method {
my
(
$class
,
$r
) =
@_
;
return
Apache2::Const::DECLINED
unless
$r
->method_number == Apache2::Const::M_POST;
logDebug(
"Attach handler for [%s]"
,
$r
->uri);
EntityModel::Web::PageHandler->reloadSiteDef()
unless
$::SITE;
my
(
$page
) = $::SITE->pageFromURI(
$r
->uri);
return
Apache2::Const::DECLINED
unless
$page
&&
$page
->upload;
$page
->upload->
require
;
logError(
"Have upload %s, attaching filter"
,
$page
->upload);
$r
->add_input_filter(
$class
.
'->updateStatus'
);
return
Apache2::Const::OK;
}
sub
updateStatus {
my
(
$class
,
$f
,
$bucket
,
$mode
,
$block
,
$bytesRead
) =
@_
;
logError(
"Have %d bytes"
,
$bytesRead
);
unless
(
$f
->ctx) {
logError(
"New request, pid %d, URI %s"
, $$,
$f
->r->uri);
my
(
$page
) = $::SITE->pageFromURI(
$f
->r->uri);
$f
->ctx({
page
=>
$page
,
total
=>
$f
->r->headers_in->get(
'Content-Length'
) || 0,
upload
=>
$page
->upload->new(
r
=> EntityModel::Web::Request::Apache2->new(
$f
->r)),
});
$f
->ctx->{upload}->onstart
if
$f
->ctx->{upload}->can(
'onstart'
);
if
(
$f
->ctx->{upload}->can(
'onheader'
)) {
$f
->r->headers_in->
do
(
sub
{
my
(
$k
,
$v
) =
@_
;
$f
->ctx->{upload}->onheader(
$k
,
$v
);
1;
});
}
}
logError(
"CTX has been set"
);
my
$upload
=
$f
->ctx->{upload};
logError(
"Upload is %s"
,
$upload
);
my
$rslt
=
$f
->
next
->get_brigade(
$bucket
,
$mode
,
$block
,
$bytesRead
);
unless
(
$rslt
== APR::Const::SUCCESS) {
$upload
->oncancel
if
$upload
->can(
'oncancel'
);
return
$rslt
;
}
my
$content
=
''
;
my
$b
=
$bucket
->first;
while
(
$b
) {
if
(
$b
->
read
(
my
$data
)) {
logDebug(
"Read "
.
length
(
$data
) .
" bytes"
);
$content
.=
$data
;
$upload
->ondata(
$data
)
if
$upload
->can(
'ondata'
);
}
$b
=
$bucket
->
next
(
$b
);
}
unless
(
$f
->ctx->{filename}) {
if
(
$content
=~ m/^Content-Disposition:\s+(.*?)$/msi) {
my
$disposition
= $1;
logDebug(
"Disposition: $disposition"
);
my
(
$filename
) = (
$disposition
=~ /filename=
"([^"
]*)"/i);
logDebug(
"Filename is [%s]"
,
$filename
);
$f
->ctx->{filename} =
$filename
;
$upload
->onfilename(
$filename
)
if
$upload
->can(
'onfilename'
);
}
}
$f
->ctx->{completed} +=
$bucket
->
length
;
if
(
$f
->ctx->{completed} ==
$f
->ctx->{total}) {
$upload
->oncomplete
if
$upload
->can(
'oncomplete'
);
}
return
Apache2::Const::OK;
}
1;