| 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.06'; | |||||||||
| 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 | |||||||||||
| 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<mxc://> 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 | ▶ | my $self = shift; | ||||||||
|
Subroutine
run: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||
| 46 | 0 | my ( $opts, $file, $type ) = @_; | |||||||||
| 47 | |||||||||||
| 48 | 0 | my $content = read_binary( $file ); | |||||||||
| 49 | |||||||||||
| 50 | 0 | ▶ | unless( defined $type ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 51 | 0 | ▶ | $type = "image/jpeg" if $file =~ m/\.jp[e]?g$/; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 52 | 0 | ▶ | $type = "image/png" if $file =~ m/\.png$/; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 53 | |||||||||||
| 54 | 0 | ▶ | defined $type or | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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 | ▶ | my ( $result ) = @_; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 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 <leonerd@leonerd.org.uk> | ||||||||||
| 75 | |||||||||||
| 76 | =cut | ||||||||||
| 77 | |||||||||||
| 78 | 1 | 0x55AA; | |||||||||