forked from carrierwaveuploader/carrierwave
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove dependency on
ruby-filemagic
in white/blacklist content types
Before this, CarrierWave was relying on mime-types or ruby-filemagic gems to get/set the content type of a file in an inconsistent way. After making mime-types a runtime dependency for CW, `CarrierWave::MimeTypes` has been dropped (carrierwaveuploader#1813 for details), and `CarrierWave::MagicMimeTypes` as well. White/blacklisting content types feature (introduced by carrierwaveuploader#1596) was relying only on ruby-filemagic to read the content type of a file. It was optional and required mixing a module to the uploader. This commit removes this dependency by using the already available content type (thanks to `Mime::Types`). This let us drop any dependency on ruby-filemagic in CarrierWave. It makes JRuby support better as ruby-filemagic is unsupported on JRuby due to the C extension.
- Loading branch information
Mehdi Lahmam
authored and
Kevin Mehlbrech
committed
Aug 9, 2016
1 parent
cbbd208
commit 7ca8e07
Showing
14 changed files
with
143 additions
and
83 deletions.
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,5 +45,3 @@ notifications: | |
|
||
addons: | ||
postgresql: "9.3" | ||
apt_packages: | ||
- libmagic-dev |
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
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
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
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,42 @@ | ||
module CarrierWave | ||
module Uploader | ||
module ContentTypeBlacklist | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
before :cache, :check_content_type_blacklist_pattern! | ||
end | ||
|
||
## | ||
# Override this method in your uploader to provide a black list pattern (regexp) | ||
# of content-types which are prohibited to be uploaded. | ||
# Compares the file's content-type. | ||
# | ||
# === Returns | ||
# | ||
# [Regexp] a black list regexp to match the content_type | ||
# | ||
# === Examples | ||
# | ||
# def content_type_blacklist_pattern | ||
# /(text|application)\/json/ | ||
# end | ||
# | ||
def content_type_blacklist_pattern; end | ||
|
||
private | ||
|
||
def check_content_type_blacklist_pattern!(new_file) | ||
content_type = new_file.content_type | ||
if content_type_blacklist_pattern && content_type.match(content_type_blacklist_pattern) | ||
raise CarrierWave::IntegrityError, I18n.translate(:"errors.messages.content_type_blacklist_error", content_type: content_type) | ||
end | ||
end | ||
|
||
def blacklisted_content_type?(content_type) | ||
content_type.match(content_type_blacklist_pattern) | ||
end | ||
|
||
end # ContentTypeBlacklist | ||
end # Uploader | ||
end # CarrierWave |
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,42 @@ | ||
module CarrierWave | ||
module Uploader | ||
module ContentTypeWhitelist | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
before :cache, :check_content_type_whitelist_pattern! | ||
end | ||
|
||
## | ||
# Override this method in your uploader to provide a white list pattern (regexp) | ||
# of content-types which are allowed to be uploaded. | ||
# Compares the file's content-type. | ||
# | ||
# === Returns | ||
# | ||
# [Regexp] a white list regexp to match the content_type | ||
# | ||
# === Examples | ||
# | ||
# def content_type_whitelist_pattern | ||
# /(text|application)\/json/ | ||
# end | ||
# | ||
def content_type_whitelist_pattern; end | ||
|
||
private | ||
|
||
def check_content_type_whitelist_pattern!(new_file) | ||
content_type = new_file.content_type | ||
if content_type_whitelist_pattern && !whitelisted_content_type?(content_type) | ||
raise CarrierWave::IntegrityError, I18n.translate(:"errors.messages.content_type_whitelist_error", content_type: content_type) | ||
end | ||
end | ||
|
||
def whitelisted_content_type?(content_type) | ||
content_type.match(content_type_whitelist_pattern) | ||
end | ||
|
||
end # ContentTypeWhitelist | ||
end # Uploader | ||
end # CarrierWave |
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
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