diff --git a/Gemfile b/Gemfile index d1c4057aeb2..a0784a635ea 100644 --- a/Gemfile +++ b/Gemfile @@ -60,6 +60,10 @@ gem "whenever", "~> 0.10.0", require: false gem "wicked_pdf", "~> 1.1.0" gem "wkhtmltopdf-binary", "0.12.4" gem 'starrr' +gem 'paperclip-ffmpeg' +gem 'paperclip-av-transcoder' +gem "cocaine" +gem 'jw_player_helper' source "https://rails-assets.org" do gem "rails-assets-leaflet" diff --git a/Gemfile.lock b/Gemfile.lock index 22b278fb90c..2e583920a16 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -78,6 +78,8 @@ GEM ast (2.4.1) autoprefixer-rails (8.2.0) execjs + av (0.9.0) + cocaine (~> 0.5.3) babel-source (5.8.35) babel-transpiler (0.7.0) babel-source (>= 4.0, < 6) @@ -248,6 +250,7 @@ GEM jquery-ui-rails (6.0.1) railties (>= 3.2.16) json (2.3.1) + jw_player_helper (0.0.1) jwt (1.5.6) kaminari (1.1.1) activesupport (>= 4.1.0) @@ -337,6 +340,12 @@ GEM cocaine (~> 0.5.5) mime-types mimemagic (~> 0.3.0) + paperclip-av-transcoder (0.6.4) + av (~> 0.9.0) + paperclip (>= 2.5.2) + paperclip-ffmpeg (1.2.0) + paperclip (>= 2.5.2) + rails parallel (1.19.2) paranoia (2.4.2) activerecord (>= 4.0, < 6.1) @@ -558,6 +567,7 @@ DEPENDENCIES capistrano3-delayed-job (~> 1.7.3) capybara (~> 2.17.0) ckeditor (~> 4.2.3) + cocaine cocoon (~> 1.2.9) coffee-rails (~> 4.2.2) coveralls (~> 0.8.22) @@ -585,6 +595,7 @@ DEPENDENCIES jquery-fileupload-rails jquery-rails (~> 4.3.3) jquery-ui-rails (~> 6.0.1) + jw_player_helper kaminari (~> 1.1.1) knapsack_pro (~> 1.1.0) launchy (~> 2.4.3) @@ -596,6 +607,8 @@ DEPENDENCIES omniauth-google-oauth2 (~> 0.4.0) omniauth-twitter (~> 1.4.0) paperclip (~> 5.2.1) + paperclip-av-transcoder + paperclip-ffmpeg paranoia (~> 2.4.1) pg (~> 0.21.0) pg_search (~> 2.0.1) diff --git a/app/assets/javascripts/imageable.js.coffee b/app/assets/javascripts/imageable.js.coffee index bdb71f4f97a..c3de68e9216 100644 --- a/app/assets/javascripts/imageable.js.coffee +++ b/app/assets/javascripts/imageable.js.coffee @@ -121,11 +121,19 @@ App.Imageable = $(data.errorContainer).append(errors) setPreview: (data) -> + + video_preview = "" image_preview = "
" if $(data.preview).length > 0 - $(data.preview).replaceWith(image_preview) + if "mp4".indexOf(data.result.filename) + $(data.preview).replaceWith(data.result.filename) + else + $(data.preview).replaceWith(image_preview) else - $(image_preview).insertBefore($(data.wrapper).find(".attachment-actions")) + if "mp4".indexOf(data.result.filename) + $(video_preview).insertBefore($(data.wrapper).find(".attachment-actions")) + else + $(image_preview).insertBefore($(data.wrapper).find(".attachment-actions")) data.preview = $(data.wrapper).find(".image-preview") doDeleteCachedAttachmentRequest: (url, data) -> diff --git a/app/controllers/admin/settings_controller.rb b/app/controllers/admin/settings_controller.rb index 2e4f1ad8a3f..9e159df7df0 100644 --- a/app/controllers/admin/settings_controller.rb +++ b/app/controllers/admin/settings_controller.rb @@ -49,7 +49,7 @@ def settings_params end def content_type_params - params.permit(:jpg, :png, :gif, :pdf, :doc, :docx, :xls, :xlsx, :csv, :zip) + params.permit(:jpg, :png, :gif, :pdf, :doc, :docx, :xls, :xlsx, :csv, :zip, :mp4) end def hidden_settings diff --git a/app/helpers/imageables_helper.rb b/app/helpers/imageables_helper.rb index 7c8d872981d..4093040df82 100644 --- a/app/helpers/imageables_helper.rb +++ b/app/helpers/imageables_helper.rb @@ -17,7 +17,7 @@ def bytes_to_megabytes(bytes) end def imageable_accepted_content_types - Setting["uploads.images.content_types"]&.split(" ") || [ "image/jpeg" ] + Setting["uploads.images.content_types"]&.split(" ") || [ "image/jpeg", "image/gif", "video/mp4" ] end def imageable_accepted_content_types_extensions diff --git a/app/models/image.rb b/app/models/image.rb index 6929f5f96aa..b884f414b7c 100644 --- a/app/models/image.rb +++ b/app/models/image.rb @@ -2,15 +2,23 @@ class Image < ApplicationRecord include ImagesHelper include ImageablesHelper - has_attached_file :attachment, styles: { - large: "x#{Setting["uploads.images.min_height"]}", - medium: "300x300#", - thumb: "140x245#" - }, - url: "/system/:class/:prefix/:style/:hash.:extension", - hash_data: ":class/:style", - use_timestamp: false, - hash_secret: Rails.application.secrets.secret_key_base + has_attached_file :attachment, + styles: lambda{ |a| + return {} unless a.content_type.in? %w(image/jpeg image/jpg image/png image/gif) + { large: "x#{Setting["uploads.images.min_height"]}", medium: "300x300#", thumb: "140x245#" } + }, + url: "/system/:class/:prefix/:style/:hash.:extension", + hash_data: ":class/:style", + use_timestamp: false, + processors: lambda { |a| + begin + a.is_video? ? [ :ffmpeg ] : [ :thumbnail ] + rescue => exception + [ :thumbnail ] + end + }, + hash_secret: Rails.application.secrets.secret_key_base + attr_accessor :cached_attachment belongs_to :user @@ -27,7 +35,7 @@ class Image < ApplicationRecord validates :user_id, presence: true validates :imageable_id, presence: true, if: -> { persisted? } validates :imageable_type, presence: true, if: -> { persisted? } - validate :validate_image_dimensions, if: -> { attachment.present? && attachment.dirty? } + validate :validate_image_dimensions, if: -> { attachment.present? && attachment.dirty? && attachment.content_type != 'video/mp4' } before_save :set_attachment_from_cached_attachment, if: -> { cached_attachment.present? } after_save :remove_cached_attachment, if: -> { cached_attachment.present? } diff --git a/app/models/setting.rb b/app/models/setting.rb index 91aa42595ae..cc091c34149 100644 --- a/app/models/setting.rb +++ b/app/models/setting.rb @@ -62,7 +62,8 @@ def mime_types "images" => { "jpg" => "image/jpeg", "png" => "image/png", - "gif" => "image/gif" + "gif" => "image/gif", + "mp4" => "video/mp4" }, "documents" => { "pdf" => "application/pdf", @@ -72,6 +73,9 @@ def mime_types "xlsx" => "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "csv" => "text/plain", "zip" => "application/zip" + }, + "videos" => { + "mp4" => "video/mp4" } } end @@ -164,13 +168,14 @@ def defaults "transparency_url": "http://transparencia.madrid.es/", "twitter_handle": "abriendomadrid", "twitter_hashtag": "#decidemadrid", - # Images and Documents + # Images, videos and Documents "uploads.images.title.min_length": 4, "uploads.images.title.max_length": 80, "uploads.images.min_width": 0, "uploads.images.min_height": 475, "uploads.images.max_size": 1, "uploads.images.content_types": "image/jpeg", + "uploads.videos.content_types": "video/mp4", "uploads.documents.max_amount": 3, "uploads.documents.max_size": 3, "uploads.documents.content_types": "application/pdf", diff --git a/app/models/site_customization/image.rb b/app/models/site_customization/image.rb index a98ba62acbe..5a9ef3d4198 100644 --- a/app/models/site_customization/image.rb +++ b/app/models/site_customization/image.rb @@ -10,10 +10,22 @@ class SiteCustomization::Image < ApplicationRecord "logo_email" => [400, 80] } - has_attached_file :image + has_attached_file :image, styles: lambda{ |a| + return {} unless a.content_type.in? %w(image/jpeg image/jpg image/png image/gif) + { medium: "640x480", thumb: "100x100#" } + }, processors: lambda { |a| + begin + a.is_video? ? [ :ffmpeg ] : [ :thumbnail ] + rescue => exception + [ :thumbnail ] + end + } + + validates_attachment_content_type :image, content_type: [ 'image/jpg', 'image/jpeg', 'image/png', 'video/mp4', 'image/gif'] + # has_attached_file :image validates :name, presence: true, uniqueness: true, inclusion: { in: VALID_IMAGES.keys } - validates_attachment_content_type :image, content_type: ["image/png", "image/jpeg"] + # validates_attachment_content_type :image, content_type: ["image/png", "image/jpeg", "image/gif", "video/mp4"] validate :check_image def self.all_images diff --git a/app/views/admin/banners/_form.html.erb b/app/views/admin/banners/_form.html.erb index 8e8d423e814..9e144363db4 100644 --- a/app/views/admin/banners/_form.html.erb +++ b/app/views/admin/banners/_form.html.erb @@ -1,3 +1,4 @@ +<%= javascript_include_tag 'swfobject' %> <%= render "admin/shared/globalize_locales", resource: @banner %> <%= translatable_form_for [:admin, @banner] do |f| %> diff --git a/app/views/custom/welcome/index.html.erb b/app/views/custom/welcome/index.html.erb index 6d7a4a56fe8..e5842ec28a3 100644 --- a/app/views/custom/welcome/index.html.erb +++ b/app/views/custom/welcome/index.html.erb @@ -29,7 +29,9 @@