From 783e47fc2b9ac45b5b0a703094d254dc93d09648 Mon Sep 17 00:00:00 2001 From: Karina Kwiatek Date: Fri, 1 May 2020 23:43:46 +0100 Subject: [PATCH] Replace paperclip with CarrierWave --- Gemfile | 5 +- Gemfile.lock | 64 ++++++++----------- app/models/user.rb | 16 +---- app/uploaders/profile_header_uploader.rb | 43 +++++++++++++ app/uploaders/profile_picture_uploader.rb | 44 +++++++++++++ config/environments/development.rb | 10 +-- config/initializers/25_carrierwave.rb | 12 ++++ config/initializers/25_paperclip.rb | 14 ---- config/justask.yml.example | 2 +- config/late_initializers/costanza.rb | 15 ----- ...add_attachment_profile_picture_to_users.rb | 10 ++- ..._add_attachment_profile_header_to_users.rb | 5 +- db/migrate/20150825073030_create_themes.rb | 5 +- db/schema.rb | 6 +- 14 files changed, 153 insertions(+), 98 deletions(-) create mode 100644 app/uploaders/profile_header_uploader.rb create mode 100644 app/uploaders/profile_picture_uploader.rb create mode 100644 config/initializers/25_carrierwave.rb delete mode 100644 config/initializers/25_paperclip.rb delete mode 100644 config/late_initializers/costanza.rb diff --git a/Gemfile b/Gemfile index fd79772f..4cdc790d 100644 --- a/Gemfile +++ b/Gemfile @@ -28,8 +28,6 @@ gem 'bootstrap_form' gem 'font-kit-rails' gem 'nprogress-rails' gem 'font-awesome-rails', '~> 4.7.0' -gem "paperclip", "~> 5.2" -gem 'delayed_paperclip' gem 'fog-core' gem 'fog-aws' gem 'fog-local' @@ -39,6 +37,8 @@ gem 'bootstrap4-datetime-picker-rails' gem 'tiny-color-rails' gem 'jquery-minicolors-rails' gem 'colorize' +gem 'carrierwave', '~> 2.0' +gem 'mini_magick' gem "rolify", "~> 5.2" @@ -79,6 +79,7 @@ group :development do gem 'spring', '~> 2.0' gem 'byebug' gem 'web-console', '< 4.0.0' + gem 'binding_of_caller' end gem 'puma' diff --git a/Gemfile.lock b/Gemfile.lock index 212bb83d..d97927bc 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -68,7 +68,6 @@ GEM addressable (2.7.0) public_suffix (>= 2.0.2, < 5.0) arel (9.0.0) - ast (2.4.0) autoprefixer-rails (9.7.6) execjs bcrypt (3.1.13) @@ -77,6 +76,8 @@ GEM erubi (>= 1.0.0) rack (>= 0.9.0) bindex (0.8.1) + binding_of_caller (0.8.0) + debug_inspector (>= 0.0.1) bootstrap (4.4.1) autoprefixer-rails (>= 9.1.0) popper_js (>= 1.14.3, < 2) @@ -100,7 +101,13 @@ GEM rack-test (>= 0.6.3) regexp_parser (~> 1.5) xpath (~> 3.2) - climate_control (0.2.0) + carrierwave (2.1.0) + activemodel (>= 5.0.0) + activesupport (>= 5.0.0) + addressable (~> 2.6) + image_processing (~> 1.1) + mimemagic (>= 0.3.0) + mini_mime (>= 0.1.3) cliver (0.3.2) coderay (1.1.2) coffee-rails (4.2.2) @@ -115,9 +122,7 @@ GEM connection_pool (2.2.2) crass (1.0.6) database_cleaner (1.8.4) - delayed_paperclip (3.0.1) - activejob (>= 4.2) - paperclip (>= 3.3) + debug_inspector (0.0.3) devise (4.7.1) bcrypt (~> 3.0) orm_adapter (~> 0.1) @@ -177,6 +182,7 @@ GEM railties (>= 3.2, < 6.1) font-kit-rails (1.2.0) rails + foreman (0.87.1) formatador (0.2.5) globalid (0.4.2) activesupport (>= 4.2.0) @@ -197,11 +203,6 @@ GEM haml (5.1.2) temple (>= 0.8.0) tilt - haml_lint (0.35.0) - haml (>= 4.0, < 5.2) - rainbow - rubocop (>= 0.50.0) - sysexits (~> 1.1) hashie (4.1.0) http (4.4.1) addressable (~> 2.3) @@ -221,8 +222,10 @@ GEM concurrent-ruby (~> 1.0) i18n-js (3.0.0.rc10) i18n (~> 0.6) + image_processing (1.10.3) + mini_magick (>= 4.9.5, < 5) + ruby-vips (>= 2.0.17, < 3) ipaddress (0.8.3) - jaro_winkler (1.5.4) jbuilder (2.10.0) activesupport (>= 5.0.0) jquery-minicolors-rails (2.2.6.2) @@ -250,6 +253,7 @@ GEM activerecord kaminari-core (= 1.2.0) kaminari-core (1.2.0) + kgio (2.11.3) launchy (2.5.0) addressable (~> 2.7) letter_opener (1.7.0) @@ -272,6 +276,7 @@ GEM mime-types-data (~> 3.2015) mime-types-data (3.2019.1009) mimemagic (0.3.4) + mini_magick (4.10.1) mini_mime (1.0.2) mini_portile2 (2.4.0) minitest (5.14.0) @@ -309,15 +314,6 @@ GEM omniauth-oauth (~> 1.1) rack orm_adapter (0.5.0) - paperclip (5.3.0) - activemodel (>= 4.2.0) - activesupport (>= 4.2.0) - mime-types - mimemagic (~> 0.3.0) - terrapin (~> 0.6.0) - parallel (1.19.1) - parser (2.7.1.2) - ast (~> 2.4.0) pg (1.2.3) pghero (2.4.2) activerecord (>= 5) @@ -386,7 +382,7 @@ GEM method_source rake (>= 0.8.7) thor (>= 0.19.0, < 2.0) - rainbow (3.0.0) + raindrops (0.19.1) rake (13.0.1) rb-fsevent (0.10.3) rb-inotify (0.10.1) @@ -398,7 +394,6 @@ GEM responders (3.0.0) actionpack (>= 5.0) railties (>= 5.0) - rexml (3.2.4) rolify (5.2.0) rspec-core (3.9.1) rspec-support (~> 3.9.1) @@ -423,15 +418,9 @@ GEM rspec-core (~> 3.0, >= 3.0.0) sidekiq (>= 2.4.0) rspec-support (3.9.2) - rubocop (0.82.0) - jaro_winkler (~> 1.5.1) - parallel (~> 1.10) - parser (>= 2.7.0.1) - rainbow (>= 2.2.2, < 4.0) - rexml - ruby-progressbar (~> 1.7) - unicode-display_width (>= 1.4.0, < 2.0) ruby-progressbar (1.10.1) + ruby-vips (2.0.17) + ffi (~> 1.9) sanitize (5.1.0) crass (~> 1.0.2) nokogiri (>= 1.8.0) @@ -481,10 +470,7 @@ GEM sprockets (>= 3.0.0) sweetalert-rails (1.1.3) railties (>= 3.1.0) - sysexits (1.2.0) temple (0.8.2) - terrapin (0.6.0) - climate_control (>= 0.0.3, < 1.0) thor (1.0.1) thread_safe (0.3.6) tilt (2.0.10) @@ -511,7 +497,9 @@ GEM unf (0.1.4) unf_ext unf_ext (0.0.7.7) - unicode-display_width (1.7.0) + unicorn (5.5.4) + kgio (~> 2.6) + raindrops (~> 0.7) warden (1.2.8) rack (>= 2.0.6) web-console (3.7.0) @@ -531,16 +519,17 @@ PLATFORMS DEPENDENCIES bcrypt (~> 3.1.7) better_errors + binding_of_caller bootstrap (~> 4.4, >= 4.4.1) bootstrap4-datetime-picker-rails bootstrap_form brakeman byebug capybara + carrierwave (~> 2.0) coffee-rails (~> 4.1) colorize database_cleaner - delayed_paperclip devise (~> 4.0) devise-async devise-i18n @@ -552,9 +541,9 @@ DEPENDENCIES fog-local font-awesome-rails (~> 4.7.0) font-kit-rails + foreman guard-brakeman haml (~> 5.0) - haml_lint httparty i18n-js (= 3.0.0.rc10) jbuilder (~> 2.10) @@ -562,6 +551,7 @@ DEPENDENCIES jquery-rails jquery-turbolinks letter_opener + mini_magick moment-timezone-rails (~> 1.0) momentjs-rails (>= 2.9.0) newrelic_rpm @@ -569,7 +559,6 @@ DEPENDENCIES omniauth omniauth-tumblr omniauth-twitter - paperclip (~> 5.2) pg pghero poltergeist @@ -603,6 +592,7 @@ DEPENDENCIES turbolinks (~> 2.5.3) twitter uglifier (>= 1.3.0) + unicorn web-console (< 4.0.0) BUNDLED WITH diff --git a/app/models/user.rb b/app/models/user.rb index 2e43c3e0..6e154d38 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -54,20 +54,8 @@ class User < ApplicationRecord validates :display_name, length: { maximum: 50 } validates :bio, length: { maximum: 200 } - # validates :website, format: { with: WEBSITE_REGEX } - - has_attached_file :profile_picture, styles: { large: "500x500#", medium: "256x256#", small: "80x80#" }, - default_url: "/images/:style/no_avatar.png", use_timestamp: false, - processors: [:cropper] - validates_attachment_content_type :profile_picture, :content_type => /\Aimage\/(png|jpe?g|gif)\Z/ - - has_attached_file :profile_header, styles: { web: "1500x350#", mobile: "450x105#", retina: "900x210#" }, - default_url: '/images/header/:style/no_header.jpg', use_timestamp: false, - processors: [:cropper] - validates_attachment_content_type :profile_header, :content_type => /\Aimage\/(png|jpe?g)\Z/ - - process_in_background :profile_picture - process_in_background :profile_header + mount_uploader :profile_picture, ProfilePictureUploader, mount_on: :profile_picture_file_name + mount_uploader :profile_header, ProfileHeaderUploader, mount_on: :profile_header_file_name before_save do self.website = if website.match %r{\Ahttps?://} diff --git a/app/uploaders/profile_header_uploader.rb b/app/uploaders/profile_header_uploader.rb new file mode 100644 index 00000000..54429285 --- /dev/null +++ b/app/uploaders/profile_header_uploader.rb @@ -0,0 +1,43 @@ +class ProfileHeaderUploader < CarrierWave::Uploader::Base + 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 + "/system/uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}" + end + + # Provide a default URL as a default if there hasn't been a file uploaded: + 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('/') + end + + version :web do + process resize_to_fit: [1500, 350] + end + version :mobile do + process resize_to_fit: [450, 105] + end + version :retina do + process resize_to_fit: [900, 210] + 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 diff --git a/app/uploaders/profile_picture_uploader.rb b/app/uploaders/profile_picture_uploader.rb new file mode 100644 index 00000000..b994835a --- /dev/null +++ b/app/uploaders/profile_picture_uploader.rb @@ -0,0 +1,44 @@ +class ProfilePictureUploader < CarrierWave::Uploader::Base + 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 + "/system/uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}" + end + + # Provide a default URL as a default if there hasn't been a file uploaded: + 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('/') + end + + version :large do + process resize_to_fit: [500, 500] + end + version :medium do + process resize_to_fit: [256, 256] + end + version :small do + process resize_to_fit: [80, 80] + 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 diff --git a/config/environments/development.rb b/config/environments/development.rb index cd66aa46..7f2cc1de 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -29,14 +29,8 @@ Rails.application.configure do # Action Mailer Setup # if you want to test sending mails locally, uncomment the line below and comment the :sendmail line # config.action_mailer.delivery_method = :letter_opener - if ENV["mailcatcher"] == "yes" - config.action_mailer.delivery_method = :smtp - config.action_mailer.smtp_settings = { :address => "localhost", :port => 1025 } - elsif ENV["letteropener"] == "yes" - config.action_mailer.delivery_method = :letter_opener - else - config.action_mailer.delivery_method = :sendmail - end + config.action_mailer.delivery_method = :smtp + config.action_mailer.smtp_settings = { :address => "localhost", :port => 1025 } # Don't care if the mailer can't send. config.action_mailer.raise_delivery_errors = false diff --git a/config/initializers/25_carrierwave.rb b/config/initializers/25_carrierwave.rb new file mode 100644 index 00000000..422b791a --- /dev/null +++ b/config/initializers/25_carrierwave.rb @@ -0,0 +1,12 @@ +CarrierWave.configure do |config| + config.fog_credentials = { + :provider => "Local", + :local_root => "#{Rails.root}/public", + } + config.fog_directory = "/system" + + unless APP_CONFIG["fog"].nil? + config.fog_credentials = APP_CONFIG["fog"]["credentials"] unless APP_CONFIG["fog"]["credentials"].nil? + config.fog_directory = APP_CONFIG["fog"]["directory"] unless APP_CONFIG["fog"]["directory"].nil? + end +end \ No newline at end of file diff --git a/config/initializers/25_paperclip.rb b/config/initializers/25_paperclip.rb deleted file mode 100644 index dc6db410..00000000 --- a/config/initializers/25_paperclip.rb +++ /dev/null @@ -1,14 +0,0 @@ -Paperclip::Attachment.default_options[:storage] = :fog -Paperclip::Attachment.default_options[:fog_credentials] = {:provider => "Local", :local_root => "#{Rails.root}/public"} -Paperclip::Attachment.default_options[:fog_directory] = "/system" -Paperclip::Attachment.default_options[:fog_host] = "/system" - -unless APP_CONFIG["fog"].nil? - Paperclip::Attachment.default_options[:fog_credentials] = APP_CONFIG["fog"]["credentials"] unless APP_CONFIG["fog"]["credentials"].nil? - Paperclip::Attachment.default_options[:fog_directory] = APP_CONFIG["fog"]["directory"] unless APP_CONFIG["fog"]["directory"].nil? - Paperclip::Attachment.default_options[:fog_host] = APP_CONFIG["fog"]["host"] unless APP_CONFIG["fog"]["host"].nil? - - if not APP_CONFIG["fog"]["credentials"].nil? and APP_CONFIG["fog"]["host"].nil? - Paperclip::Attachment.default_options[:fog_host] = nil - end -end diff --git a/config/justask.yml.example b/config/justask.yml.example index a0b28e63..2727a5e4 100644 --- a/config/justask.yml.example +++ b/config/justask.yml.example @@ -58,7 +58,7 @@ fog: aws_access_key_id: 'ACCESS KEY' aws_secret_access_key: 'SECRET KEY' region: 'space-pluto-1' + host: 's3.wherever.com' # bucket name, required directory: 'retrospring' # URL host, comment out to use default, GENERALLY you don't want to define this - # host: '' diff --git a/config/late_initializers/costanza.rb b/config/late_initializers/costanza.rb deleted file mode 100644 index 48e13832..00000000 --- a/config/late_initializers/costanza.rb +++ /dev/null @@ -1,15 +0,0 @@ -# I seriously hope you guys don't do this. - -class Paperclip::FileCommandContentTypeDetector - alias old_type_from_file_command type_from_file_command - def type_from_file_command - default = old_type_from_file_command - if default.strip == 'text/x-c' or default.strip == 'text/plain' or default.strip == 'text/stylesheet' - 'text/css' - else - default - end - end -end - -# Here be monkey patches. diff --git a/db/migrate/20141229085904_add_attachment_profile_picture_to_users.rb b/db/migrate/20141229085904_add_attachment_profile_picture_to_users.rb index 7c59a2fe..5826b09a 100644 --- a/db/migrate/20141229085904_add_attachment_profile_picture_to_users.rb +++ b/db/migrate/20141229085904_add_attachment_profile_picture_to_users.rb @@ -1,11 +1,17 @@ class AddAttachmentProfilePictureToUsers < ActiveRecord::Migration[4.2] def self.up change_table :users do |t| - t.attachment :profile_picture + t.string :profile_picture_file_name + t.string :profile_picture_content_type + t.bigint :profile_picture_file_size + t.datetime :profile_picture_updated_at end end def self.down - remove_attachment :users, :profile_picture + remove_column :users, :profile_picture_file_name + remove_column :users, :profile_picture_content_type + remove_column :users, :profile_picture_file_size + remove_column :users, :profile_picture_updated_at end end diff --git a/db/migrate/20150508144336_add_attachment_profile_header_to_users.rb b/db/migrate/20150508144336_add_attachment_profile_header_to_users.rb index dff46586..91ac267c 100644 --- a/db/migrate/20150508144336_add_attachment_profile_header_to_users.rb +++ b/db/migrate/20150508144336_add_attachment_profile_header_to_users.rb @@ -1,7 +1,10 @@ class AddAttachmentProfileHeaderToUsers < ActiveRecord::Migration[4.2] def change change_table :users do |t| - t.attachment :profile_header + t.string :profile_header_file_name + t.string :profile_header_content_type + t.bigint :profile_header_file_size + t.datetime :profile_header_updated_at t.boolean :profile_header_processing t.integer :crop_h_x t.integer :crop_h_y diff --git a/db/migrate/20150825073030_create_themes.rb b/db/migrate/20150825073030_create_themes.rb index 9c423709..ac8da0e8 100644 --- a/db/migrate/20150825073030_create_themes.rb +++ b/db/migrate/20150825073030_create_themes.rb @@ -30,7 +30,10 @@ class CreateThemes < ActiveRecord::Migration[4.2] t.integer :background_text, limit: 4, default: 0x222222 t.integer :background_muted, limit: 4, default: 0xBBBBBB - t.attachment :css + t.string :css_file_name + t.string :css_content_type + t.bigint :css_file_size + t.datetime :css_updated_at t.timestamps null: false end diff --git a/db/schema.rb b/db/schema.rb index 499d010e..67d6ad34 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2020_05_09_094402) do +ActiveRecord::Schema.define(version: 2020_05_13_154602) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -250,7 +250,7 @@ ActiveRecord::Schema.define(version: 2020_05_09_094402) do t.text "bio", default: "", null: false t.string "profile_picture_file_name" t.string "profile_picture_content_type" - t.integer "profile_picture_file_size" + t.bigint "profile_picture_file_size" t.datetime "profile_picture_updated_at" t.boolean "profile_picture_processing" t.integer "crop_x" @@ -267,7 +267,7 @@ ActiveRecord::Schema.define(version: 2020_05_09_094402) do t.integer "comment_smiled_count", default: 0, null: false t.string "profile_header_file_name" t.string "profile_header_content_type" - t.integer "profile_header_file_size" + t.bigint "profile_header_file_size" t.datetime "profile_header_updated_at" t.boolean "profile_header_processing" t.integer "crop_h_x"