File Coverage

blib/lib/App/MatrixTool/Command/client/upload.pm
Criterion Covered Total %
statement 18 30 60.0
branch 0 8 0.0
condition n/a
subroutine 6 8 75.0
pod 0 1 0.0
total 24 47 51.0


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