This repository has been archived by the owner on Jun 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #771 from justarrived/base64-image-upload
Convert API multipart image upload to data URI
- Loading branch information
Showing
14 changed files
with
207 additions
and
77 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
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,31 @@ | ||
# frozen_string_literal: true | ||
class DataUriImage | ||
def initialize(data_uri) | ||
@valid_data_uri = true | ||
@image = Paperclip.io_adapters.for(data_uri) | ||
@content_type = @image.content_type | ||
rescue Paperclip::AdapterRegistry::NoHandlerError => _e | ||
@valid_data_uri = false | ||
end | ||
|
||
def image | ||
return unless valid? | ||
|
||
content_type = @image.content_type | ||
@image.original_filename = ImageContentTypeHelper.original_filename(content_type) | ||
|
||
@image | ||
end | ||
|
||
def valid? | ||
valid_data_uri? && valid_content_type? | ||
end | ||
|
||
def valid_data_uri? | ||
@valid_data_uri | ||
end | ||
|
||
def valid_content_type? | ||
ImageContentTypeHelper.valid?(@content_type) | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
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,30 @@ | ||
# frozen_string_literal: true | ||
class ImageContentTypeHelper | ||
ALLOWED_IMAGE_CONTENT_TYPES = %w(image/jpeg image/png).freeze | ||
ALLOWED_FILE_EXTENSIONS = %w(jpeg png).freeze | ||
|
||
def self.valid?(content_type) | ||
content_type_allowed = ALLOWED_IMAGE_CONTENT_TYPES.include?(content_type) | ||
|
||
type = _file_type(content_type) | ||
extension_allowed = ALLOWED_FILE_EXTENSIONS.include?(type) | ||
|
||
content_type_allowed && extension_allowed | ||
end | ||
|
||
def self.original_filename(content_type) | ||
return unless valid?(content_type) | ||
|
||
SecureGenerator.token(length: 64) + file_extension(content_type) | ||
end | ||
|
||
def self.file_extension(content_type) | ||
return unless valid?(content_type) | ||
|
||
'.' + _file_type(content_type) | ||
end | ||
|
||
def self._file_type(content_type) | ||
content_type.split('/').last | ||
end | ||
end |
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 |
---|---|---|
|
@@ -64,6 +64,7 @@ en: | |
banned: an admin has banned you for not following the Terms of Agreement or the Code of Conduct. Please contact [email protected], to resolve this issue. | ||
wrong_email_or_phone_or_password: wrong email, phone or password | ||
user: | ||
invalid_image_content_type: invalid content type for image | ||
wrong_password: wrong password | ||
password_length: password must have at least %{count} characters | ||
must_be_available_locale: must be a supported language | ||
|
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 was deleted.
Oops, something went wrong.
Oops, something went wrong.