Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
M
mastofe
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
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
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
Pleroma
mastofe
Commits
dc73241b
Commit
dc73241b
authored
6 years ago
by
Yamagishi Kazutoshi
Committed by
Eugen Rochko
6 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Detect extname from Content-Type (#7733)
parent
1637d24a
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
app/models/concerns/remotable.rb
+27
-7
27 additions, 7 deletions
app/models/concerns/remotable.rb
with
27 additions
and
7 deletions
app/models/concerns/remotable.rb
+
27
−
7
View file @
dc73241b
...
...
@@ -24,14 +24,16 @@ module Remotable
Request
.
new
(
:get
,
url
).
perform
do
|
response
|
next
if
response
.
code
!=
200
matches
=
response
.
headers
[
'content-disposition'
]
&
.
match
(
/filename="([^"]*)"/
)
filename
=
matches
.
nil?
?
parsed_url
.
path
.
split
(
'/'
).
last
:
matches
[
1
]
content_type
=
parse_content_type
(
response
.
headers
[
'content-type'
])
extname
=
detect_extname_from_content_type
(
content_type
)
if
extname
.
nil?
matches
=
response
.
headers
[
'content-disposition'
]
&
.
match
(
/filename="([^"]*)"/
)
filename
=
matches
.
nil?
?
parsed_url
.
path
.
split
(
'/'
).
last
:
matches
[
1
]
extname
=
filename
.
nil?
?
''
:
File
.
extname
(
filename
)
end
basename
=
SecureRandom
.
hex
(
8
)
extname
=
if
filename
.
nil?
''
else
File
.
extname
(
filename
)
end
send
(
"
#{
attachment_name
}
="
,
StringIO
.
new
(
response
.
body_with_limit
(
limit
)))
send
(
"
#{
attachment_name
}
_file_name="
,
basename
+
extname
)
...
...
@@ -57,4 +59,22 @@ module Remotable
end
end
end
private
def
detect_extname_from_content_type
(
content_type
)
return
if
content_type
.
nil?
type
=
MIME
::
Types
[
content_type
].
first
return
if
type
.
nil?
type
.
extensions
.
first
end
def
parse_content_type
(
content_type
)
return
if
content_type
.
nil?
content_type
.
split
(
/\s*;\s*/
).
first
end
end
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