Convert GIFs to MP4s
This commit is contained in:
@@ -4,6 +4,7 @@ require "securerandom"
|
|||||||
require "dry/monads"
|
require "dry/monads"
|
||||||
require "filemagic"
|
require "filemagic"
|
||||||
require "image_processing/vips"
|
require "image_processing/vips"
|
||||||
|
require "open3"
|
||||||
|
|
||||||
module Adamantium
|
module Adamantium
|
||||||
module Commands
|
module Commands
|
||||||
@@ -12,17 +13,50 @@ module Adamantium
|
|||||||
include Deps["settings"]
|
include Deps["settings"]
|
||||||
include Dry::Monads[:result]
|
include Dry::Monads[:result]
|
||||||
|
|
||||||
VALID_UPLOAD_TYPES = %i[jpeg jpg png gif]
|
IMAGE_TYPES = %i[jpeg jpg png].freeze
|
||||||
|
VIDEO_TYPES = %i[gif].freeze
|
||||||
|
VALID_UPLOAD_TYPES = IMAGE_TYPES + VIDEO_TYPES
|
||||||
|
|
||||||
def call(file:)
|
def call(file:)
|
||||||
mime = FileMagic.new
|
mime = FileMagic.new
|
||||||
type = mime.file(file[:tempfile].path, true)
|
type = mime.file(file[:tempfile].path, true).to_sym
|
||||||
|
|
||||||
return Failure(:invalid_file_type) unless VALID_UPLOAD_TYPES.include? type.to_sym
|
return Failure(:invalid_file_type) unless VALID_UPLOAD_TYPES.include? type
|
||||||
|
|
||||||
pathname = Time.now.strftime("%m-%Y")
|
save_image(file: file) if IMAGE_TYPES.include? type
|
||||||
uuid = SecureRandom.uuid
|
save_video(file: file) if VIDEO_TYPES.include? type
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def pathname
|
||||||
|
Time.now.strftime("%m-%Y")
|
||||||
|
end
|
||||||
|
|
||||||
|
def uuid
|
||||||
|
SecureRandom.uuid
|
||||||
|
end
|
||||||
|
|
||||||
|
def save_video(file:)
|
||||||
|
fullsize_filename = "#{uuid}.mp4"
|
||||||
|
|
||||||
|
dirname = File.join("public", "media", pathname)
|
||||||
|
|
||||||
|
unless File.directory?(dirname)
|
||||||
|
FileUtils.mkdir_p(dirname)
|
||||||
|
end
|
||||||
|
|
||||||
|
begin
|
||||||
|
Open3.popen3("ffmpeg -i #{file[:tempfile].path} -movflags faststart -pix_fmt yuv420p -vf 'scale=trunc(iw/2)*2:trunc(ih/2)*2' #{File.join(dirname, fullsize_filename)}")
|
||||||
|
rescue Errno::ENOENT, NoMethodError => e
|
||||||
|
return Failure(e.message)
|
||||||
|
end
|
||||||
|
|
||||||
|
upload_path = File.join(settings.micropub_site_url, "/media/", "/#{pathname}/", fullsize_filename).to_s
|
||||||
|
Success(upload_path)
|
||||||
|
end
|
||||||
|
|
||||||
|
def save_image(file:)
|
||||||
fullsize_filename = "#{uuid}#{File.extname(file[:filename])}"
|
fullsize_filename = "#{uuid}#{File.extname(file[:filename])}"
|
||||||
thumbnail_filename = "#{uuid}-small#{File.extname(file[:filename])}"
|
thumbnail_filename = "#{uuid}-small#{File.extname(file[:filename])}"
|
||||||
|
|
||||||
|
@@ -28,7 +28,7 @@ html
|
|||||||
script src="/assets/gallery.js"
|
script src="/assets/gallery.js"
|
||||||
script src=asset_from_manifest("index.js")
|
script src=asset_from_manifest("index.js")
|
||||||
|
|
||||||
script src="https://unpkg.com/htmx.org@1.8.4" integrity="sha384-wg5Y/JwF7VxGk4zLsJEcAojRtlVp1FKKdGy1qN+OMtdq72WRvX/EdRdqg/LOhYeV" crossorigin="anonymous"
|
script src="https://unpkg.com/htmx.org@1.9.2/dist/htmx.min.js" integrity="sha384-L6OqL9pRWyyFU3+/bjdSri+iIphTN/bvYyM37tICVyOJkWZLpP2vGn6VUEXgzg6h" crossorigin="anonymous"
|
||||||
script src="https://cdn.jsdelivr.net/npm/alpinejs@3.12.0/dist/cdn.min.js" defer="true"
|
script src="https://cdn.jsdelivr.net/npm/alpinejs@3.12.0/dist/cdn.min.js" defer="true"
|
||||||
|
|
||||||
- if Hanami.app.settings.micropub_pub_key
|
- if Hanami.app.settings.micropub_pub_key
|
||||||
|
@@ -13,7 +13,7 @@ html
|
|||||||
|
|
||||||
script src=asset_from_manifest("index.js")
|
script src=asset_from_manifest("index.js")
|
||||||
|
|
||||||
script src="https://unpkg.com/htmx.org@1.9.2" integrity="sha384-L6OqL9pRWyyFU3+/bjdSri+iIphTN/bvYyM37tICVyOJkWZLpP2vGn6VUEXgzg6h" crossorigin="anonymous"
|
script src="https://unpkg.com/htmx.org@1.9.2/dist/htmx.min.js" integrity="sha384-L6OqL9pRWyyFU3+/bjdSri+iIphTN/bvYyM37tICVyOJkWZLpP2vGn6VUEXgzg6h" crossorigin="anonymous"
|
||||||
script src="https://cdn.jsdelivr.net/npm/alpinejs@3.12.0/dist/cdn.min.js" defer="true"
|
script src="https://cdn.jsdelivr.net/npm/alpinejs@3.12.0/dist/cdn.min.js" defer="true"
|
||||||
|
|
||||||
- if Hanami.app.settings.micropub_pub_key
|
- if Hanami.app.settings.micropub_pub_key
|
||||||
|
Reference in New Issue
Block a user