Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
M
Majic
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
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
Pleroma
Elixir libraries
Majic
Commits
90b7b240
Commit
90b7b240
authored
4 months ago
by
feld
Browse files
Options
Downloads
Plain Diff
Merge remote-tracking branch 'origin/develop' into release-1.1.0
parents
99dd901c
715a1dbb
No related branches found
No related tags found
1 merge request
!6
Release 1.1.0
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
lib/majic/extension.ex
+39
-21
39 additions, 21 deletions
lib/majic/extension.ex
test/majic/extension_test.exs
+8
-0
8 additions, 0 deletions
test/majic/extension_test.exs
with
47 additions
and
21 deletions
lib/majic/extension.ex
+
39
−
21
View file @
90b7b240
...
@@ -59,29 +59,47 @@ defmodule Majic.Extension do
...
@@ -59,29 +59,47 @@ defmodule Majic.Extension do
defp
do_fix
(
name
,
mime_type
,
options
)
do
defp
do_fix
(
name
,
mime_type
,
options
)
do
append?
=
Keyword
.
get
(
options
,
:append
,
false
)
append?
=
Keyword
.
get
(
options
,
:append
,
false
)
subtype?
=
Keyword
.
get
(
options
,
:subtype_as_extension
,
false
)
subtype?
=
Keyword
.
get
(
options
,
:subtype_as_extension
,
false
)
exts
=
MIME
.
extensions
(
mime_type
)
++
subtype_extension
(
subtype?
,
mime_type
)
ext
_candidate
s
=
MIME
.
extensions
(
mime_type
)
old_ext
=
String
.
downcase
(
Path
.
extname
(
name
))
old_ext
=
String
.
downcase
(
Path
.
extname
(
name
))
old_ext_bare
=
String
.
trim_leading
(
old_ext
,
"."
)
basename
=
Path
.
basename
(
name
,
old_ext
)
unless
old_ext
==
""
do
cond
do
basename
=
Path
.
basename
(
name
,
old_ext
)
# extension already in candidate list, so no-op
"."
<>
old
=
old_ext
old_ext_bare
in
ext_candidates
->
name
if
old
in
exts
do
Enum
.
join
([
basename
,
"."
,
old
])
# has extension, append the subtype
else
not
match?
(
""
,
old_ext
)
&&
append?
&&
subtype?
->
ext
=
List
.
first
(
exts
)
Enum
.
join
([
name
,
subtype_extension
(
subtype?
,
mime_type
)],
"."
)
ext_list
=
# has extension, change to subtype
cond
do
not
match?
(
""
,
old_ext
)
&&
subtype?
->
ext
&&
append?
->
[
old
,
ext
]
Enum
.
join
([
basename
,
subtype_extension
(
subtype?
,
mime_type
)],
"."
)
!ext
->
[]
ext
->
[
ext
]
# no extension, append
end
match?
(
""
,
old_ext
)
&&
append?
->
Enum
.
join
([
basename
,
List
.
first
(
ext_candidates
)],
"."
)
Enum
.
join
([
basename
]
++
ext_list
,
"."
)
end
# no candidates, so strip extension
else
match?
([],
ext_candidates
)
->
name
basename
# no extension but no appending, so no-op
match?
(
""
,
old_ext
)
->
name
# append first candidate
not
Enum
.
empty?
(
ext_candidates
)
&&
append?
->
Enum
.
join
([
name
,
List
.
first
(
ext_candidates
)],
"."
)
# change extension to first candidate
not
Enum
.
empty?
(
ext_candidates
)
->
Enum
.
join
([
basename
,
List
.
first
(
ext_candidates
)],
"."
)
# do nothing
true
->
name
end
end
end
end
...
...
This diff is collapsed.
Click to expand it.
test/majic/extension_test.exs
+
8
−
0
View file @
90b7b240
...
@@ -13,6 +13,14 @@ defmodule Majic.ExtensionTest do
...
@@ -13,6 +13,14 @@ defmodule Majic.ExtensionTest do
assert
"cat.jpeg.webp"
==
Extension
.
fix
(
"cat.jpeg"
,
"image/webp"
,
append:
true
)
assert
"cat.jpeg.webp"
==
Extension
.
fix
(
"cat.jpeg"
,
"image/webp"
,
append:
true
)
end
end
test
"it appends extensions if none exist"
do
assert
"webpage.html"
==
Extension
.
fix
(
"webpage"
,
"text/html"
,
append:
true
)
end
test
"it does not append extension if none exist when appending not requested"
do
assert
"webpage"
==
Extension
.
fix
(
"webpage"
,
"text/html"
)
end
test
"it uses subtype as extension"
do
test
"it uses subtype as extension"
do
assert
"Makefile.x-makefile"
==
assert
"Makefile.x-makefile"
==
Extension
.
fix
(
"Makefile.txt"
,
"text/x-makefile"
,
subtype_as_extension:
true
)
Extension
.
fix
(
"Makefile.txt"
,
"text/x-makefile"
,
subtype_as_extension:
true
)
...
...
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