diff --git a/app/javascript/gabsocial/actions/group_editor.js b/app/javascript/gabsocial/actions/group_editor.js index 23231b34..eda9dc52 100644 --- a/app/javascript/gabsocial/actions/group_editor.js +++ b/app/javascript/gabsocial/actions/group_editor.js @@ -35,7 +35,7 @@ export const create = (title, description, coverImage, routerHistory) => (dispat const formData = new FormData(); formData.append('title', title); formData.append('description', description); - debugger; + if (coverImage !== null) { formData.append('cover_image', coverImage); } diff --git a/app/models/concerns/group_cover_image.rb b/app/models/concerns/group_cover_image.rb new file mode 100644 index 00000000..b36e80ce --- /dev/null +++ b/app/models/concerns/group_cover_image.rb @@ -0,0 +1,33 @@ +# frozen_string_literal: true + +module GroupCoverImage + extend ActiveSupport::Concern + + LIMIT = 4.megabytes + IMAGE_MIME_TYPES = ['image/jpeg', 'image/png', 'image/gif', 'image/webp'].freeze + + class_methods do + def cover_image_styles(file) + styles = { original: { geometry: '1200x475#', file_geometry_parser: FastGeometryParser } } + styles[:static] = { geometry: '1200x475#', format: 'png', convert_options: '-coalesce', file_geometry_parser: FastGeometryParser } if file.content_type == 'image/gif' + styles + end + + private :cover_image_styles + end + + included do + has_attached_file :cover_image, styles: ->(f) { cover_image_styles(f) }, convert_options: { all: '-strip' }, processors: [:lazy_thumbnail] + validates_attachment_content_type :cover_image, content_type: IMAGE_MIME_TYPES + validates_attachment_size :cover_image, less_than: LIMIT + remotable_attachment :cover_image, LIMIT + end + + def cover_image_original_url + cover_image.url(:original) + end + + def cover_image_static_url + cover_image_content_type == 'image/gif' ? cover_image.url(:static) : cover_image_original_url + end +end diff --git a/app/models/group.rb b/app/models/group.rb index 5b62a208..1e3edbfb 100644 --- a/app/models/group.rb +++ b/app/models/group.rb @@ -20,6 +20,7 @@ class Group < ApplicationRecord include Paginable include GroupInteractions + include GroupCoverImage PER_ACCOUNT_LIMIT = 50 @@ -34,14 +35,6 @@ class Group < ApplicationRecord validates :title, presence: true validates :description, presence: true - LIMIT = 4.megabytes - IMAGE_MIME_TYPES = ['image/jpeg', 'image/png', 'image/gif', 'image/webp'].freeze - - has_attached_file :cover_image - validates_attachment_content_type :cover_image, content_type: IMAGE_MIME_TYPES - validates_attachment_size :cover_image, less_than: LIMIT - remotable_attachment :cover_image, LIMIT - validates_each :account_id, on: :create do |record, _attr, value| record.errors.add(:base, I18n.t('groups.errors.limit')) if Group.where(account_id: value).count >= PER_ACCOUNT_LIMIT end