| 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 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 1 | ▶ | use base qw( App::MatrixTool::Command::client ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | 1 | our $VERSION = '0.08'; | |||||||||
| 13 | |||||||||||
| 14 | 1 | ▶ | use File::Slurper qw( read_binary ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | |||||||||||
| 16 | 1 | ▶ | use constant DESCRIPTION => "Upload a file to the media repository"; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 17 | 1 | ▶ | use constant ARGUMENTS => ( "file", "type?" ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 18 | 1 | ▶ | use constant OPTIONS => (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 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<mxc://> 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 | 0 | ▶ | { | ||||||||
|
Subroutine
run: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||
| 46 | 0 | my $self = shift; | |||||||||
| 47 | 0 | my ( $opts, $file, $type ) = @_; | |||||||||
| 48 | |||||||||||
| 49 | 0 | my $content = read_binary( $file ); | |||||||||
| 50 | |||||||||||
| 51 | 0 | ▶ | unless( defined $type ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 52 | 0 | ▶ | $type = "image/jpeg" if $file =~ m/\.jp[e]?g$/; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 53 | 0 | ▶ | $type = "image/png" if $file =~ m/\.png$/; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 54 | |||||||||||
| 55 | 0 | ▶ | defined $type or | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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 | 0 | ▶ | )->then( sub { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 63 | 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 <leonerd@leonerd.org.uk> | ||||||||||
| 76 | |||||||||||
| 77 | =cut | ||||||||||
| 78 | |||||||||||
| 79 | 1 | 0x55AA; | |||||||||