Implement cropping; make uploaders DRY

This commit is contained in:
Karina Kwiatek 2020-05-02 17:45:11 +01:00 committed by Dominik M. Kwiatek
parent 9757c56a2b
commit ff9741589d
6 changed files with 48 additions and 85 deletions

View File

@ -30,10 +30,10 @@ if window.URL? or window.webkitURL?
preview = ($ '#profile-picture-preview') preview = ($ '#profile-picture-preview')
updateVars = (data, action) -> updateVars = (data, action) ->
($ '#crop_x').val Math.floor(data.x / data.scale) ($ '#profile_picture_x').val Math.floor(data.x / data.scale)
($ '#crop_y').val Math.floor(data.y / data.scale) ($ '#profile_picture_y').val Math.floor(data.y / data.scale)
($ '#crop_w').val Math.floor(data.w / data.scale) ($ '#profile_picture_w').val Math.floor(data.w / data.scale)
($ '#crop_h').val Math.floor(data.h / data.scale) ($ '#profile_picture_h').val Math.floor(data.h / data.scale)
# rx = 100 / data.w # rx = 100 / data.w
# ry = 100 / data.h # ry = 100 / data.h
# ($ '#profile-picture-preview').css # ($ '#profile-picture-preview').css
@ -76,10 +76,10 @@ if window.URL? or window.webkitURL?
preview = ($ '#profile-header-preview') preview = ($ '#profile-header-preview')
updateVars = (data, action) -> updateVars = (data, action) ->
($ '#crop_h_x').val Math.floor(data.x / data.scale) ($ '#profile_header_x').val Math.floor(data.x / data.scale)
($ '#crop_h_y').val Math.floor(data.y / data.scale) ($ '#profile_header_y').val Math.floor(data.y / data.scale)
($ '#crop_h_w').val Math.floor(data.w / data.scale) ($ '#profile_header_w').val Math.floor(data.w / data.scale)
($ '#crop_h_h').val Math.floor(data.h / data.scale) ($ '#profile_header_h').val Math.floor(data.h / data.scale)
cropper.on 'load', -> cropper.on 'load', ->
if ({}.toString).call(src) == "[object URL]" if ({}.toString).call(src) == "[object URL]"

View File

@ -28,8 +28,9 @@ class UserController < ApplicationController
end end
def update def update
user_attributes = params.require(:user).permit(:display_name, :profile_picture, :profile_header, :motivation_header, :website, user_attributes = params.require(:user).permit(:display_name, :motivation_header, :website, :show_foreign_themes, :location, :bio,
:location, :bio, :crop_x, :crop_y, :crop_w, :crop_h, :crop_h_x, :crop_h_y, :crop_h_w, :crop_h_h, :show_foreign_themes) :profile_picture_x, :profile_picture_y, :profile_picture_w, :profile_picture_h,
:profile_header_x, :profile_header_y, :profile_header_w, :profile_header_h, :profile_picture, :profile_header)
if current_user.update_attributes(user_attributes) if current_user.update_attributes(user_attributes)
text = t('flash.user.update.text') text = t('flash.user.update.text')
text += t('flash.user.update.avatar') if user_attributes[:profile_picture] text += t('flash.user.update.avatar') if user_attributes[:profile_picture]

View File

@ -41,6 +41,9 @@ class User < ApplicationRecord
has_one :theme, dependent: :destroy has_one :theme, dependent: :destroy
attr_accessor :profile_picture_x, :profile_picture_y, :profile_picture_w, :profile_picture_h,
:profile_header_x, :profile_header_y, :profile_header_w, :profile_header_h
SCREEN_NAME_REGEX = /\A[a-zA-Z0-9_]{1,16}\z/ SCREEN_NAME_REGEX = /\A[a-zA-Z0-9_]{1,16}\z/
WEBSITE_REGEX = /https?:\/\/([A-Za-z.\-]+)\/?(?:.*)/i WEBSITE_REGEX = /https?:\/\/([A-Za-z.\-]+)\/?(?:.*)/i
@ -65,7 +68,7 @@ class User < ApplicationRecord
end unless website.blank? end unless website.blank?
end end
# when a user deleted himself, all reports relating to the user are invalid # when a user has been deleted, all reports relating to the user become invalid
before_destroy do before_destroy do
rep = Report.where(target_id: self.id, type: 'Reports::User') rep = Report.where(target_id: self.id, type: 'Reports::User')
rep.each do |r| rep.each do |r|
@ -221,10 +224,6 @@ class User < ApplicationRecord
end end
# endregion # endregion
def cropping?
!crop_x.blank? && !crop_y.blank? && !crop_w.blank? && !crop_h.blank?
end
# forwards fill # forwards fill
def banned? def banned?
self.permanently_banned? or ((not self.banned_until.nil?) and self.banned_until >= DateTime.current) self.permanently_banned? or ((not self.banned_until.nil?) and self.banned_until >= DateTime.current)

View File

@ -0,0 +1,31 @@
class BaseUploader < CarrierWave::Uploader::Base
include CarrierWave::Compatibility::Paperclip
include CarrierWave::MiniMagick
storage :fog
# Store original size
version :original
# Process cropping on upload
process :cropping
def store_dir
"/uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
def paperclip_path
"users/:attachment/:id_partition/:style/:basename.:extension"
end
def cropping
x = model.public_send("#{mounted_as}_x")
y = model.public_send("#{mounted_as}_y")
w = model.public_send("#{mounted_as}_w")
h = model.public_send("#{mounted_as}_h")
manipulate! do |image|
image.crop "#{w}x#{h}+#{x}+#{y}"
end
end
end

View File

@ -1,30 +1,8 @@
class ProfileHeaderUploader < CarrierWave::Uploader::Base class ProfileHeaderUploader < BaseUploader
include CarrierWave::Compatibility::Paperclip
include CarrierWave::MiniMagick
# Choose what kind of storage to use for this uploader:
storage :fog
# Override the directory where uploaded files will be stored.
# This is a sensible default for uploaders that are meant to be mounted:
def store_dir
"/uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
def paperclip_path
"users/:attachment/:id_partition/:style/:basename.:extension"
end
# Provide a default URL as a default if there hasn't been a file uploaded:
def default_url(*args) def default_url(*args)
# For Rails 3.1+ asset pipeline compatibility:
# ActionController::Base.helpers.asset_path("fallback/" + [version_name, "default.png"].compact.join('_'))
"/images/header/" + [version_name, "no_header.jpg"].compact.join('/') "/images/header/" + [version_name, "no_header.jpg"].compact.join('/')
end end
version :original
version :web do version :web do
process resize_to_fit: [1500, 350] process resize_to_fit: [1500, 350]
end end
@ -34,16 +12,4 @@ class ProfileHeaderUploader < CarrierWave::Uploader::Base
version :retina do version :retina do
process resize_to_fit: [900, 210] process resize_to_fit: [900, 210]
end end
# Add a white list of extensions which are allowed to be uploaded.
# For images you might use something like this:
# def extension_whitelist
# %w(jpg jpeg gif png)
# end
# Override the filename of the uploaded files:
# Avoid using model.id or version_name here, see uploader/store.rb for details.
# def filename
# "something.jpg" if original_filename
# end
end end

View File

@ -1,30 +1,8 @@
class ProfilePictureUploader < CarrierWave::Uploader::Base class ProfilePictureUploader < BaseUploader
include CarrierWave::Compatibility::Paperclip
include CarrierWave::MiniMagick
# Choose what kind of storage to use for this uploader:
storage :fog
# Override the directory where uploaded files will be stored.
# This is a sensible default for uploaders that are meant to be mounted:
def store_dir
"/uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
def paperclip_path
"users/:attachment/:id_partition/:style/:basename.:extension"
end
# Provide a default URL as a default if there hasn't been a file uploaded:
def default_url(*args) def default_url(*args)
# For Rails 3.1+ asset pipeline compatibility:
# ActionController::Base.helpers.asset_path("fallback/" + [version_name, "default.png"].compact.join('_'))
"/images/" + [version_name, "no_avatar.png"].compact.join('/') "/images/" + [version_name, "no_avatar.png"].compact.join('/')
end end
version :original
version :large do version :large do
process resize_to_fit: [500, 500] process resize_to_fit: [500, 500]
end end
@ -34,16 +12,4 @@ class ProfilePictureUploader < CarrierWave::Uploader::Base
version :small do version :small do
process resize_to_fit: [80, 80] process resize_to_fit: [80, 80]
end end
# Add a white list of extensions which are allowed to be uploaded.
# For images you might use something like this:
# def extension_whitelist
# %w(jpg jpeg gif png)
# end
# Override the filename of the uploaded files:
# Avoid using model.id or version_name here, see uploader/store.rb for details.
# def filename
# "something.jpg" if original_filename
# end
end end