diff --git a/Gemfile b/Gemfile index 3ec1685..c274cc2 100644 --- a/Gemfile +++ b/Gemfile @@ -29,7 +29,7 @@ gem "gpx" gem "gnuplot" gem "matrix" -gem "ruby-readability", :require => "readability" +gem "ruby-readability", require: "readability" gem "down" gem "httparty" gem "redcarpet" @@ -47,6 +47,7 @@ gem "mail" gem "que" gem "connection_pool" gem "omdb-api", require: false +gem "image_processing", "~> 1.0" group :cli, :development do gem "hanami-reloader" diff --git a/Gemfile.lock b/Gemfile.lock index 9378f88..b5c9802 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -237,6 +237,9 @@ GEM i18n (1.13.0) concurrent-ruby (~> 1.0) ice_nine (0.11.2) + image_processing (1.12.2) + mini_magick (>= 4.9.5, < 5) + ruby-vips (>= 2.0.17, < 3) indieweb-endpoints (8.0.0) http (~> 5.0) link-header-parser (~> 5.0) @@ -265,6 +268,7 @@ GEM mime-types (3.4.1) mime-types-data (~> 3.2015) mime-types-data (3.2023.0218.1) + mini_magick (4.12.0) mini_mime (1.1.2) minitest (5.18.0) multi_xml (0.6.0) @@ -395,6 +399,8 @@ GEM ruby-readability (0.7.0) guess_html_encoding (>= 0.0.4) nokogiri (>= 1.6.0) + ruby-vips (2.1.4) + ffi (~> 1.12) ruby2_keywords (0.0.5) sanitize (6.0.1) crass (~> 1.0.2) @@ -473,6 +479,7 @@ DEPENDENCIES hanami-validations (~> 2.0.0) hanami-view! httparty + image_processing (~> 1.0) lastfm (~> 1.27) mail matrix diff --git a/app/commands/media/upload.rb b/app/commands/media/upload.rb index 5c901f5..dc55cdb 100644 --- a/app/commands/media/upload.rb +++ b/app/commands/media/upload.rb @@ -3,6 +3,7 @@ require "securerandom" require "dry/monads" require "filemagic" +require "image_processing/vips" module Adamantium module Commands @@ -11,7 +12,7 @@ module Adamantium include Deps["settings"] include Dry::Monads[:result] - VALID_UPLOAD_TYPES = %i[jpeg jpg png gif mp4 iso] + VALID_UPLOAD_TYPES = %i[jpeg jpg png gif] def call(file:) mime = FileMagic.new @@ -21,16 +22,28 @@ module Adamantium pathname = Time.now.strftime("%m-%Y") - filename = "#{SecureRandom.uuid}#{File.extname(file[:filename])}" + fullsize_filename = "#{SecureRandom.uuid}#{File.extname(file[:filename])}" + thumbnail_filename = "#{SecureRandom.uuid}-small#{File.extname(file[:filename])}" dirname = File.join("public", "media", pathname) + fullsize_pipeline = ImageProcessing::Vips.source(file[:tempfile]) + .resize_to_limit(1024, nil) + .saver(quality: 100) + .call(save: false) + + thumbnail_pipeline = ImageProcessing::Vips.source(file[:tempfile]) + .resize_to_limit(300, 300, crop: :attention) + .saver(quality: 100) + .call(save: false) + unless File.directory?(dirname) FileUtils.mkdir_p(dirname) end begin - File.write(File.join(dirname, filename), file[:tempfile].read) + fullsize_pipeline.write_to_file(File.join(dirname, fullsize_filename)) + thumbnail_pipeline.write_to_file(File.join(dirname, thumbnail_filename)) rescue Errno::ENOENT, NoMethodError => e return Failure(e.message) end