File Coverage

blib/lib/App/MatrixTool/Command/client/upload.pm
Criterion Covered Total %
statement 21 33 63.6
branch 0 8 0.0
condition n/a
subroutine 7 9 77.7
pod 0 1 0.0
total 28 51 54.9


line stmt bran cond sub pod time code
1             # You may distribute under the terms of either the GNU General Public License
2             # or the Artistic License (the same terms as Perl itself)
3             #
4             # (C) Paul Evans, 2016 -- leonerd@leonerd.org.uk
5              
6             package App::MatrixTool::Command::client::upload;
7              
8 1     1   780 use strict;
  1         1  
  1         27  
9 1     1   3 use warnings;
  1         1  
  1         24  
10 1     1   3 use base qw( App::MatrixTool::Command::client );
  1         2  
  1         89  
11              
12             our $VERSION = '0.08';
13              
14 1     1   496 use File::Slurper qw( read_binary );
  1         2907  
  1         93  
15              
16 1     1   6 use constant DESCRIPTION => "Upload a file to the media repository";
  1         1  
  1         59  
17 1     1   4 use constant ARGUMENTS => ( "file", "type?" );
  1         1  
  1         46  
18 1     1   4 use constant OPTIONS => ();
  1         1  
  1         224  
19              
20             =head1 NAME
21              
22             matrixtool client upload - Upload a file to the media repository
23              
24             =head1 SYNOPSIS
25              
26             $ matrixtool client -u @me:example.com upload avatar.png
27              
28             =head1 DESCRIPTION
29              
30             This command uploads a file to the media repository of a Matrix homeserver,
31             printing the returned F URL.
32              
33             Normally the MIME type must be supplied as a second argument, but in the
34             common case of files whose names end in certain recognised file extensions,
35             the MIME type can be automatically inferred for convenience.
36              
37             The recognised extensions are
38              
39             .jpg, .jpeg image/jpeg
40             .png image/png
41              
42             =cut
43              
44             sub run
45             {
46 0     0 0   my $self = shift;
47 0           my ( $opts, $file, $type ) = @_;
48              
49 0           my $content = read_binary( $file );
50              
51 0 0         unless( defined $type ) {
52 0 0         $type = "image/jpeg" if $file =~ m/\.jp[e]?g$/;
53 0 0         $type = "image/png" if $file =~ m/\.png$/;
54              
55 0 0         defined $type or
56             die "Type not specified and could not guess it from the filename\n";
57             }
58              
59             $self->do_json( POST => "/_matrix/media/r0/upload",
60             content => $content,
61             content_type => $type,
62             )->then( sub {
63 0     0     my ( $result ) = @_;
64              
65 0           $self->output_ok( "Uploaded content" );
66              
67 0           print $result->{content_uri} . "\n";
68              
69 0           Future->done();
70 0           });
71             }
72              
73             =head1 AUTHOR
74              
75             Paul Evans
76              
77             =cut
78              
79             0x55AA;