-
Notifications
You must be signed in to change notification settings - Fork 69
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 #645 from coopdevs/validate_image_size
Add avatar file size validation (max 5 MB)
- Loading branch information
Showing
13 changed files
with
100 additions
and
57 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,51 @@ | ||
class AvatarGenerator | ||
MAX_SIZE = 5 | ||
CONTENT_TYPES = %w[image/jpeg image/pjpeg image/png image/x-png] | ||
|
||
attr_accessor :errors | ||
|
||
def initialize(user, params) | ||
@user = user | ||
@params = params | ||
@avatar = params[:avatar] | ||
@errors = [] | ||
|
||
validate_avatar | ||
end | ||
|
||
def call | ||
@user.avatar.purge if @user.avatar.attached? | ||
|
||
@user.avatar.attach( | ||
io: process_image, | ||
filename: @user.username, | ||
content_type: @avatar.content_type | ||
) | ||
end | ||
|
||
private | ||
|
||
def process_image | ||
orig_width = @params[:original_width].to_i | ||
width = @params[:height_width].to_i | ||
left = @params[:width_offset].to_i | ||
top = @params[:height_offset].to_i | ||
|
||
ImageProcessing::MiniMagick. | ||
source(@avatar.tempfile). | ||
resize_to_fit(orig_width, nil). | ||
crop("#{width}x#{width}+#{left}+#{top}!"). | ||
convert("png"). | ||
call | ||
end | ||
|
||
def validate_avatar | ||
if CONTENT_TYPES.exclude?(@avatar.content_type) | ||
@errors << I18n.t("users.show.invalid_format") | ||
end | ||
|
||
if @avatar.size.to_f > MAX_SIZE.megabytes | ||
@errors << I18n.t("users.avatar.max_size_warning", size: MAX_SIZE) | ||
end | ||
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
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