Skip to content

Commit

Permalink
Try to support inline and attachment behaviours
Browse files Browse the repository at this point in the history
  • Loading branch information
Half-Shot committed Jul 24, 2023
1 parent 50544bb commit 21989c8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
27 changes: 19 additions & 8 deletions tests/51media/01unicode.pl
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
=head2 upload_test_content
my ( $content_id, $content_uri ) = upload_test_content(
$user, filename => "filename",
$user, filename => "filename", $content_type
) -> get;
Uploads some test content with the given filename.
Expand All @@ -55,13 +55,15 @@ =head2 upload_test_content
=cut
sub upload_test_content
{
my ( $user, %params ) = @_;
my ( $user, %params, $content_type) = @_;

$content_type ||= "application/octet-stream"

$user->http->do_request(
method => "POST",
full_uri => "/_matrix/media/v3/upload",
content => "Test media file",
content_type => "text/plain",
content_type => $content_type,

params => {
access_token => $user->access_token,
Expand All @@ -88,14 +90,16 @@ sub upload_test_content

=head2 get_media
my ( $content_disposition_params, $content ) = get_media( $http, $content_id ) -> get;
my ( $content_disposition_params, $content ) = get_media( $http, $content_id, $allowed_depositions ) -> get;
Fetches a piece of media from the server.
=cut
sub get_media
{
my ( $http, $content_id ) = @_;
my ( $http, $content_id, $allow_inline_disposition ) = @_;

$allow_inline_disposition ||= 0;

$http->do_request(
method => "GET",
Expand All @@ -107,15 +111,15 @@ sub get_media

my $cd_params;
if ( defined $disposition ) {
$cd_params = parse_content_disposition_params( $disposition );
$cd_params = parse_content_disposition_params( $disposition, $allow_inline_disposition );
}
Future->done( $cd_params, $body );
});
}
push @EXPORT, qw( get_media );

sub parse_content_disposition_params {
my ( $disposition ) = @_;
my ( $disposition, $allow_inline_disposition ) = @_;
my @parts = split_header_words( $disposition );

# should be only one list of words
Expand All @@ -125,7 +129,14 @@ sub parse_content_disposition_params {
# the first part must be 'attachment'
my $k = shift @parts;
my $v = shift @parts;
assert_eq( $k, "attachment", "content-disposition" );

if ($allow_inline_disposition eq 1) {
assert_ok( $k eq "attachment" or $k eq "inline", "content-disposition");
}
else {
assert_eq( $k, "attachment", "content-disposition" );
}

die "invalid CD" if defined $v;

my %params;
Expand Down
4 changes: 2 additions & 2 deletions tests/51media/02nofilename.pl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
do => sub {
my ( $user ) = @_;

upload_test_content( $user, )->then( sub {
upload_test_content( $user )->then( sub {
( $content_id ) = @_;
Future->done(1)
});
Expand All @@ -19,7 +19,7 @@ sub test_using_client
{
my ( $client ) = @_;

get_media( $client, $content_id )->then( sub {
get_media( $client, $content_id, 0 )->then( sub {
my ( $disposition ) = @_;

# Require `attachment` `Content-Disposition` without a filename portion.
Expand Down

0 comments on commit 21989c8

Please sign in to comment.