Compress image uploads
This commit is contained in:
3
Gemfile
3
Gemfile
@@ -29,7 +29,7 @@ gem "gpx"
|
|||||||
gem "gnuplot"
|
gem "gnuplot"
|
||||||
gem "matrix"
|
gem "matrix"
|
||||||
|
|
||||||
gem "ruby-readability", :require => "readability"
|
gem "ruby-readability", require: "readability"
|
||||||
gem "down"
|
gem "down"
|
||||||
gem "httparty"
|
gem "httparty"
|
||||||
gem "redcarpet"
|
gem "redcarpet"
|
||||||
@@ -47,6 +47,7 @@ gem "mail"
|
|||||||
gem "que"
|
gem "que"
|
||||||
gem "connection_pool"
|
gem "connection_pool"
|
||||||
gem "omdb-api", require: false
|
gem "omdb-api", require: false
|
||||||
|
gem "image_processing", "~> 1.0"
|
||||||
|
|
||||||
group :cli, :development do
|
group :cli, :development do
|
||||||
gem "hanami-reloader"
|
gem "hanami-reloader"
|
||||||
|
@@ -237,6 +237,9 @@ GEM
|
|||||||
i18n (1.13.0)
|
i18n (1.13.0)
|
||||||
concurrent-ruby (~> 1.0)
|
concurrent-ruby (~> 1.0)
|
||||||
ice_nine (0.11.2)
|
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)
|
indieweb-endpoints (8.0.0)
|
||||||
http (~> 5.0)
|
http (~> 5.0)
|
||||||
link-header-parser (~> 5.0)
|
link-header-parser (~> 5.0)
|
||||||
@@ -265,6 +268,7 @@ GEM
|
|||||||
mime-types (3.4.1)
|
mime-types (3.4.1)
|
||||||
mime-types-data (~> 3.2015)
|
mime-types-data (~> 3.2015)
|
||||||
mime-types-data (3.2023.0218.1)
|
mime-types-data (3.2023.0218.1)
|
||||||
|
mini_magick (4.12.0)
|
||||||
mini_mime (1.1.2)
|
mini_mime (1.1.2)
|
||||||
minitest (5.18.0)
|
minitest (5.18.0)
|
||||||
multi_xml (0.6.0)
|
multi_xml (0.6.0)
|
||||||
@@ -395,6 +399,8 @@ GEM
|
|||||||
ruby-readability (0.7.0)
|
ruby-readability (0.7.0)
|
||||||
guess_html_encoding (>= 0.0.4)
|
guess_html_encoding (>= 0.0.4)
|
||||||
nokogiri (>= 1.6.0)
|
nokogiri (>= 1.6.0)
|
||||||
|
ruby-vips (2.1.4)
|
||||||
|
ffi (~> 1.12)
|
||||||
ruby2_keywords (0.0.5)
|
ruby2_keywords (0.0.5)
|
||||||
sanitize (6.0.1)
|
sanitize (6.0.1)
|
||||||
crass (~> 1.0.2)
|
crass (~> 1.0.2)
|
||||||
@@ -473,6 +479,7 @@ DEPENDENCIES
|
|||||||
hanami-validations (~> 2.0.0)
|
hanami-validations (~> 2.0.0)
|
||||||
hanami-view!
|
hanami-view!
|
||||||
httparty
|
httparty
|
||||||
|
image_processing (~> 1.0)
|
||||||
lastfm (~> 1.27)
|
lastfm (~> 1.27)
|
||||||
mail
|
mail
|
||||||
matrix
|
matrix
|
||||||
|
@@ -3,6 +3,7 @@
|
|||||||
require "securerandom"
|
require "securerandom"
|
||||||
require "dry/monads"
|
require "dry/monads"
|
||||||
require "filemagic"
|
require "filemagic"
|
||||||
|
require "image_processing/vips"
|
||||||
|
|
||||||
module Adamantium
|
module Adamantium
|
||||||
module Commands
|
module Commands
|
||||||
@@ -11,7 +12,7 @@ module Adamantium
|
|||||||
include Deps["settings"]
|
include Deps["settings"]
|
||||||
include Dry::Monads[:result]
|
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:)
|
def call(file:)
|
||||||
mime = FileMagic.new
|
mime = FileMagic.new
|
||||||
@@ -21,16 +22,28 @@ module Adamantium
|
|||||||
|
|
||||||
pathname = Time.now.strftime("%m-%Y")
|
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)
|
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)
|
unless File.directory?(dirname)
|
||||||
FileUtils.mkdir_p(dirname)
|
FileUtils.mkdir_p(dirname)
|
||||||
end
|
end
|
||||||
|
|
||||||
begin
|
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
|
rescue Errno::ENOENT, NoMethodError => e
|
||||||
return Failure(e.message)
|
return Failure(e.message)
|
||||||
end
|
end
|
||||||
|
Reference in New Issue
Block a user