| 1 | #!/bin/false | ||
| 2 | # PODNAME: BZ::Client::Bug::Attachment | ||
| 3 | # ABSTRACT: Client side representation of an Attachment to a Bug in Bugzilla | ||
| 4 | # vim: softtabstop=4 tabstop=4 shiftwidth=4 ft=perl expandtab smarttab | ||
| 5 | |||
| 6 | use strict; | ||
| 7 | use warnings 'all'; | ||
| 8 | |||
| 9 | package BZ::Client::Bug::Attachment; | ||
| 10 | $BZ::Client::Bug::Attachment::VERSION = '4.91'; | ||
| 11 | use parent qw( BZ::Client::API ); | ||
| 12 | |||
| 13 | use File::Basename qw( basename ); | ||
| 14 | |||
| 15 | # See https://www.bugzilla.org/docs/tip/en/html/api/Bugzilla/WebService/Bug.html | ||
| 16 | # These are in order as per the above | ||
| 17 | |||
| 18 | ## functions | ||
| 19 | |||
| 20 | sub new { | ||
| 21 | my $class = shift; | ||
| 22 | my $self = $class->SUPER::new(@_); | ||
| 23 | $self->data($self->{data}) # This will make it a ::base64 object | ||
| 24 | if $self->{data}; | ||
| 25 | return $self | ||
| 26 | } | ||
| 27 | |||
| 28 | sub get { | ||
| 29 | my($class, $client, $params) = @_; | ||
| 30 | $client->log('debug', __PACKAGE__ . "::get: Asking for $params"); | ||
| 31 | my $result = $class->api_call($client, 'Bug.attachments', $params); | ||
| 32 | |||
| 33 | if (my $attachments = $result->{attachments}) { | ||
| 34 | $class->error($client, | ||
| 35 | 'Invalid reply by server, expected hash of attachments.') | ||
| 36 | unless ($attachments and 'HASH' eq ref($attachments)); | ||
| 37 | for my $id (keys %{$attachments}) { | ||
| 38 | $attachments->{$id}->{data} = | ||
| 39 | BZ::Client::XMLRPC::base64->new64($attachments->{$id}->{data}); | ||
| 40 | $attachments->{$id} = __PACKAGE__ | ||
| 41 | ->new( %{$attachments->{$id}} ); | ||
| 42 | } | ||
| 43 | } | ||
| 44 | |||
| 45 | if (my $bugs = $result->{bugs}) { | ||
| 46 | $class->error($client, | ||
| 47 | 'Invalid reply by server, expected array of bugs.') | ||
| 48 | unless ($bugs and 'HASH' eq ref($bugs)); | ||
| 49 | for my $id (keys %{$bugs}) { | ||
| 50 | $bugs->{$id} = [ | ||
| 51 | map { __PACKAGE__->new( %{$_} ) } | ||
| 52 | map { $_->{data} = BZ::Client::XMLRPC::base64->new64($_->{data}) if $_->{data}; $_ } | ||
| 53 | @{$bugs->{$id}} ]; | ||
| 54 | } | ||
| 55 | } | ||
| 56 | |||
| 57 | $client->log('debug', __PACKAGE__ . '::get: Got keys: ' . join(', ', sort keys %{$result})); | ||
| 58 | |||
| 59 | return wantarray ? %{$result} : $result | ||
| 60 | } | ||
| 61 | |||
| 62 | sub add { | ||
| 63 | my($class, $client, $params) = @_; | ||
| 64 | # $params = { ids => [], file_name => basename($file), content_type => '?', summary=> $filename, data => \$content | ||
| 65 | $client->log('debug', __PACKAGE__ . '::add: Attaching a file'); | ||
| 66 | if ( ref $params eq 'HASH' ) { | ||
| 67 | my $filename; | ||
| 68 | if ( ! $params->{data} | ||
| 69 | and $filename = $params->{file_name} | ||
| 70 | and -f $filename ) { | ||
| 71 | $params->{data} = do { | ||
| 72 | local $/; | ||
| 73 | open( my $fh, '<', $filename ) | ||
| 74 | or $class->error($client, "Cannot open $filename: $!"); | ||
| 75 | binmode $fh; | ||
| 76 | <$fh> | ||
| 77 | }; | ||
| 78 | $params->{file_name} = basename($params->{file_name}); | ||
| 79 | } | ||
| 80 | $params->{data} = BZ::Client::XMLRPC::base64->new($params->{data}) | ||
| 81 | if (exists $params->{data} and ref $params->{data} eq ''); | ||
| 82 | } | ||
| 83 | my $result = $class->api_call($client, 'Bug.add_attachment', $params); | ||
| 84 | my $ids = $result->{'ids'}; | ||
| 85 | $class->error($client, 'Invalid reply by server, expected attachment ID.') | ||
| 86 | unless $ids; | ||
| 87 | return wantarray ? @$ids : $ids | ||
| 88 | } | ||
| 89 | |||
| 90 | sub update { | ||
| 91 | my($class, $client, $params) = @_; | ||
| 92 | return $class->_returns_array($client, 'Bug.update_attachment', $params, 'attachments'); | ||
| 93 | } | ||
| 94 | |||
| 95 | |||
| 96 | ## rw methods | ||
| 97 | |||
| 98 | sub id { shift->_accessor('id', @_) } | ||
| 99 | |||
| 100 | sub data { | ||
| 101 | my $self = shift; | ||
| 102 | if (@_) { | ||
| 103 | my $data = shift; | ||
| 104 | $self->{'data'} = ref $data ? | ||
| 105 | $data : BZ::Client::XMLRPC::base64->new($data); | ||
| 106 | } | ||
| 107 | else { | ||
| 108 | return $self->{'data'} | ||
| 109 | } | ||
| 110 | } | ||
| 111 | |||
| 112 | sub file_name { shift->_accessor('file_name', @_) } | ||
| 113 | |||
| 114 | sub description { goto &summary } | ||
| 115 | |||
| 116 | sub summary { | ||
| 117 | my $self = shift; | ||
| 118 | if (@_) { | ||
| 119 | $self->{'summary'} = shift; | ||
| 120 | } | ||
| 121 | else { | ||
| 122 | return $self->{'summary'} || $self->{'description'} | ||
| 123 | } | ||
| 124 | } | ||
| 125 | |||
| 126 | sub content_type { shift->_accessor('content_type', @_) } | ||
| 127 | |||
| 128 | sub comment { shift->_accessor('comment', @_) } | ||
| 129 | |||
| 130 | sub is_patch { shift->_accessor('is_patch', @_) } | ||
| 131 | |||
| 132 | sub is_private { shift->_accessor('is_private', @_) } | ||
| 133 | |||
| 134 | sub is_url { shift->_accessor('is_url', @_) } | ||
| 135 | |||
| 136 | ## ro methods | ||
| 137 | |||
| 138 | sub size { my $self = shift; return $self->{'size'} } | ||
| 139 | |||
| 140 | sub creation_time { my $self = shift; return $self->{'creation_time'} } | ||
| 141 | |||
| 142 | sub last_change_time { my $self = shift; return $self->{'last_change_time'} } | ||
| 143 | |||
| 144 | sub bug_id { my $self = shift; return $self->{'bug_id'} } | ||
| 145 | |||
| 146 | sub creator { my $self = shift; return $self->{'creator'} || $self->{'attacher'} } | ||
| 147 | |||
| 148 | sub attacher { goto &creator } | ||
| 149 | |||
| 150 | sub flags { my $self = shift; return $self->{'flags'} } | ||
| 151 | |||
| 152 | 1; | ||
| 153 | |||
| 154 | __END__ |