| 1 | use strict; | ||
| 2 | use warnings FATAL => 'all'; | ||
| 3 | |||
| 4 | package HTML::Tested::ClassDBI::Upload; | ||
| 5 | use Carp; | ||
| 6 | use File::MMagic; | ||
| 7 | |||
| 8 | sub new { return bless([ $_[1]->CDBI_Class, $_[2], $_[3] ], $_[0]); } | ||
| 9 | |||
| 10 | sub setup_type_info {} | ||
| 11 | |||
| 12 | sub strip_mime_header { | ||
| 13 | my ($class, $buf) = @_; | ||
| 14 | $buf =~ s/^MIME: ([^\n]+)\n//; | ||
| 15 | return ($1, $buf); | ||
| 16 | } | ||
| 17 | |||
| 18 | sub _get_mime { | ||
| 19 | my ($class, $fh) = @_; | ||
| 20 | # Invoking file(2) command on $fh through IPC::Run3 doesn't work in | ||
| 21 | # Apache. | ||
| 22 | my $mm = File::MMagic->new; | ||
| 23 | bless $fh, 'FileHandle'; | ||
| 24 | my $res = $mm->checktype_filehandle($fh); | ||
| 25 | seek($fh, 0, 0) or confess "Unable to seek"; | ||
| 26 | return $res; | ||
| 27 | } | ||
| 28 | |||
| 29 | sub _dbh_write { | ||
| 30 | my ($dbh, $lo_fd, $buf, $rlen) = @_; | ||
| 31 | my $wlen = $dbh->func($lo_fd, $buf, $rlen, 'lo_write'); | ||
| 32 | defined($wlen) or confess "# Unable to lo_write $rlen"; | ||
| 33 | confess "# short write $rlen > $wlen" if $rlen != $wlen; | ||
| 34 | } | ||
| 35 | |||
| 36 | sub _open_lo { | ||
| 37 | my ($class, $dbh, $lo) = @_; | ||
| 38 | confess "We should be in transaction" if $dbh->{AutoCommit}; | ||
| 39 | if ($lo) { | ||
| 40 | $dbh->func($lo, 'lo_unlink') or confess "error: lo_unlink $lo"; | ||
| 41 | $dbh->do("select lo_create(?)", undef, $lo); | ||
| 42 | } else { | ||
| 43 | $lo = $dbh->func($dbh->{pg_INV_WRITE}, 'lo_creat') | ||
| 44 | or confess "# Unable to lo_creat"; | ||
| 45 | } | ||
| 46 | my $lo_fd = $dbh->func($lo, $dbh->{'pg_INV_WRITE'}, 'lo_open'); | ||
| 47 | defined($lo_fd) or confess "# Unable to lo_open $lo"; | ||
| 48 | return ($lo, $lo_fd); | ||
| 49 | } | ||
| 50 | |||
| 51 | sub _parse_arg { | ||
| 52 | my ($class, $arg, $msg) = @_; | ||
| 53 | my ($a, $loid) = ref($arg) && ref($arg) eq 'ARRAY' ? @$arg : ($arg); | ||
| 54 | confess "No $msg is given" unless $a; | ||
| 55 | return ($a, $loid); | ||
| 56 | } | ||
| 57 | |||
| 58 | sub import_lo_from_string { | ||
| 59 | my ($class, $dbh, $stra, $with_mime) = @_; | ||
| 60 | my ($str, $loid) = $class->_parse_arg($stra, "string"); | ||
| 61 | if ($with_mime) { | ||
| 62 | my $mime = File::MMagic->new->checktype_contents($str) | ||
| 63 | or confess "No mime"; | ||
| 64 | $str = "MIME: $mime\n$str"; | ||
| 65 | } | ||
| 66 | my ($lo, $lo_fd) = $class->_open_lo($dbh, $loid); | ||
| 67 | _dbh_write($dbh, $lo_fd, $str, length $str); | ||
| 68 | $dbh->func($lo_fd, 'lo_close') or confess "Unable to close $lo"; | ||
| 69 | return $lo; | ||
| 70 | } | ||
| 71 | |||
| 72 | sub import_lo_object { | ||
| 73 | my ($class, $dbh, $fha, $with_mime) = @_; | ||
| 74 | my ($fh, $loid) = $class->_parse_arg($fha, "filehandle"); | ||
| 75 | my $mime = $class->_get_mime($fh) if ($with_mime); | ||
| 76 | my ($buf, $rlen, $wlen); | ||
| 77 | my ($lo, $lo_fd) = $class->_open_lo($dbh, $loid); | ||
| 78 | if ($mime) { | ||
| 79 | $buf = "MIME: $mime\n"; | ||
| 80 | _dbh_write($dbh, $lo_fd, $buf, length $buf); | ||
| 81 | } | ||
| 82 | while (($rlen = sysread($fh, $buf, 4096 * 16))) { | ||
| 83 | _dbh_write($dbh, $lo_fd, $buf, $rlen); | ||
| 84 | } | ||
| 85 | $dbh->func($lo_fd, 'lo_close') or confess "Unable to close $lo"; | ||
| 86 | return $lo; | ||
| 87 | } | ||
| 88 | |||
| 89 | sub export_lo_to_string { | ||
| 90 | my ($class, $dbh, $loid) = @_;; | ||
| 91 | my $lo_fd = $dbh->func($loid, $dbh->{'pg_INV_READ'}, 'lo_open'); | ||
| 92 | defined($lo_fd) or confess "# Unable to lo_open $loid"; | ||
| 93 | my ($buf, $ct) = ('', ''); | ||
| 94 | $dbh->func($lo_fd, $buf, 4096, 'lo_read'); | ||
| 95 | ($ct, $buf) = HTML::Tested::ClassDBI::Upload->strip_mime_header($buf); | ||
| 96 | my $res = $buf; | ||
| 97 | while ($dbh->func($lo_fd, $buf, 4096, 'lo_read')) { | ||
| 98 | $res .= $buf; | ||
| 99 | } | ||
| 100 | $dbh->func($lo_fd, 'lo_close') or confess "Unable to close $loid"; | ||
| 101 | return ($res, $ct); | ||
| 102 | } | ||
| 103 | |||
| 104 | sub update_column { | ||
| 105 | my ($self, $setter, $root, $name) = @_; | ||
| 106 | my $val = $root->$name or return; | ||
| 107 | my $lo = $self->import_lo_object($self->[0]->db_Main, $val, $self->[2]); | ||
| 108 | $setter->($self->[1], $lo); | ||
| 109 | } | ||
| 110 | |||
| 111 | sub get_column_value {} | ||
| 112 | |||
| 113 | 1; |