Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
conduwuit
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
🥺
conduwuit
Commits
c4317a7a
Commit
c4317a7a
authored
3 years ago
by
Andrej Kacian
Browse files
Options
Downloads
Patches
Plain Diff
Reduce code duplication in media download route handlers
parent
52873c88
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/client_server/media.rs
+51
-48
51 additions, 48 deletions
src/client_server/media.rs
with
51 additions
and
48 deletions
src/client_server/media.rs
+
51
−
48
View file @
c4317a7a
...
@@ -74,6 +74,38 @@ pub async fn create_content_route(
...
@@ -74,6 +74,38 @@ pub async fn create_content_route(
.into
())
.into
())
}
}
pub
async
fn
get_remote_content
(
db
:
&
DatabaseGuard
,
mxc
:
&
str
,
server_name
:
&
ruma
::
ServerName
,
media_id
:
&
str
)
->
ConduitResult
<
get_content
::
Response
>
{
let
content_response
=
db
.sending
.send_federation_request
(
&
db
.globals
,
server_name
,
get_content
::
Request
{
allow_remote
:
false
,
server_name
,
media_id
},
)
.await
?
;
db
.media
.create
(
mxc
.to_string
(),
&
db
.globals
,
&
content_response
.content_disposition
.as_deref
(),
&
content_response
.content_type
.as_deref
(),
&
content_response
.file
,
)
.await
?
;
Ok
(
content_response
.into
())
}
/// # `GET /_matrix/media/r0/download/{serverName}/{mediaId}`
/// # `GET /_matrix/media/r0/download/{serverName}/{mediaId}`
///
///
/// Load media from our server or over federation.
/// Load media from our server or over federation.
...
@@ -103,30 +135,13 @@ pub async fn get_content_route(
...
@@ -103,30 +135,13 @@ pub async fn get_content_route(
}
}
.into
())
.into
())
}
else
if
&*
body
.server_name
!=
db
.globals
.server_name
()
&&
body
.allow_remote
{
}
else
if
&*
body
.server_name
!=
db
.globals
.server_name
()
&&
body
.allow_remote
{
let
get_content_response
=
db
let
remote_content_response
=
get_remote_content
(
.sending
&
db
,
.send_federation_request
(
&
mxc
,
&
db
.globals
,
&
body
.server_name
,
&
body
.server_name
,
&
body
.media_id
get_content
::
Request
{
)
.await
?
;
allow_remote
:
false
,
Ok
(
remote_content_response
)
server_name
:
&
body
.server_name
,
media_id
:
&
body
.media_id
,
},
)
.await
?
;
db
.media
.create
(
mxc
,
&
db
.globals
,
&
get_content_response
.content_disposition
.as_deref
(),
&
get_content_response
.content_type
.as_deref
(),
&
get_content_response
.file
,
)
.await
?
;
Ok
(
get_content_response
.into
())
}
else
{
}
else
{
Err
(
Error
::
BadRequest
(
ErrorKind
::
NotFound
,
"Media not found."
))
Err
(
Error
::
BadRequest
(
ErrorKind
::
NotFound
,
"Media not found."
))
}
}
...
@@ -161,31 +176,19 @@ pub async fn get_content_as_filename_route(
...
@@ -161,31 +176,19 @@ pub async fn get_content_as_filename_route(
}
}
.into
())
.into
())
}
else
if
&*
body
.server_name
!=
db
.globals
.server_name
()
&&
body
.allow_remote
{
}
else
if
&*
body
.server_name
!=
db
.globals
.server_name
()
&&
body
.allow_remote
{
let
get_content_response
=
db
let
remote_content_response
=
get_remote_content
(
.sending
&
db
,
.send_federation_request
(
&
mxc
,
&
db
.globals
,
&
body
.server_name
,
&
body
.server_name
,
&
body
.media_id
get_content_as_filename
::
Request
{
)
.await
?
;
allow_remote
:
false
,
server_name
:
&
body
.server_name
,
media_id
:
&
body
.media_id
,
filename
:
&
body
.filename
,
},
)
.await
?
;
db
.media
.create
(
mxc
,
&
db
.globals
,
&
get_content_response
.content_disposition
.as_deref
(),
&
get_content_response
.content_type
.as_deref
(),
&
get_content_response
.file
,
)
.await
?
;
Ok
(
get_content_response
.into
())
Ok
(
get_content_as_filename
::
Response
{
content_disposition
:
Some
(
format!
(
"inline: filename={}"
,
body
.filename
)),
content_type
:
remote_content_response
.0
.content_type
,
file
:
remote_content_response
.0
.file
}
.into
())
}
else
{
}
else
{
Err
(
Error
::
BadRequest
(
ErrorKind
::
NotFound
,
"Media not found."
))
Err
(
Error
::
BadRequest
(
ErrorKind
::
NotFound
,
"Media not found."
))
}
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment