-
Notifications
You must be signed in to change notification settings - Fork 56
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support rendering some media downloads as inline #1360
Closed
Half-Shot
wants to merge
7
commits into
matrix-org:develop
from
Half-Shot:hs/add-media-inline-support
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
21989c8
Try to support inline and attachment behaviours
Half-Shot 231d43d
Add a test for inline behaviour
Half-Shot b4ef274
Remove unused sub
Half-Shot 4866459
Fix docstring
Half-Shot 79d93f3
Merge branch 'develop' into hs/add-media-inline-support
Half-Shot c0e7b46
Fix tests
Half-Shot a7823d5
Remove eq
Half-Shot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
|
@@ -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, | ||
|
@@ -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, $allow_inline_disposition ) -> 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", | ||
|
@@ -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 ) = @_; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd consider making this a named argument to make it more readable on the caller side (more than a plain |
||
my @parts = split_header_words( $disposition ); | ||
|
||
# should be only one list of words | ||
|
@@ -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) { | ||
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; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
my $content_id; | ||
|
||
# This test ensures an uploaded file may optionally be rendered inline | ||
# in the browser. This is checked in get_media | ||
|
||
test "Can download a file that optionally inlines", | ||
requires => [ $main::API_CLIENTS[0], local_user_fixture() ], | ||
|
||
check => sub { | ||
my ( $http, $user ) = @_; | ||
upload_test_content( $user, {} , "text/plain")->then( sub { | ||
( $content_id ) = @_; | ||
get_media( $http, $content_id, 1 )->then( sub { | ||
Future->done(1); | ||
}) | ||
}); | ||
}; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not a fan of mixing positionals and named arguments like that.
We could make it another param, and skip it when using
%params
in its entirety, like this:This is using the defined-or construct introduced in Perl 5.10 – which is over 15 years old, though it is tradition that if the version is not declared anywhere, we should assume 5.8 featureset. I see a bunch of
use 5.010;
elsewhere in the codebase though, so we should be in the clear here.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can't modify delete in defined or assignment (//=) at tests/51media/01unicode.pl line 61, at EOF
:(There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh yes, I got ahead of myself there. That should be just
//
, not//=
. We already have the assignment in that line :)