From 97c857836734ae4b86c469a94046f5553117340d Mon Sep 17 00:00:00 2001 From: Dmitry Borisov Date: Thu, 18 Nov 2021 15:22:27 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9F=D1=80=D0=B8=20=D0=B7=D0=B0=D0=B3=D1=80?= =?UTF-8?q?=D1=83=D0=B7=D0=BA=D0=B5=20=D0=B2=20=D0=BE=D0=B1=D0=BB=D0=B0?= =?UTF-8?q?=D0=BA=D0=B0=20=D0=BE=D0=BF=D1=80=D0=B5=D0=B4=D0=B5=D0=BB=D1=8F?= =?UTF-8?q?=D0=B5=D0=BC=20content-type=20=D0=B4=D0=BB=D1=8F=20=D0=BA=D0=B0?= =?UTF-8?q?=D0=B6=D0=B4=D0=BE=D0=B3=D0=BE=20=D1=84=D0=B0=D0=B9=D0=BB=D0=B0?= =?UTF-8?q?=20=D0=BD=D0=B5=D0=B7=D0=B0=D0=B2=D0=B8=D1=81=D0=B8=D0=BC=D0=BE?= =?UTF-8?q?=20=D0=9E=D1=80=D0=B8=D0=B3=D0=B8=D0=BD=D0=B0=D0=BB=D1=8C=D0=BD?= =?UTF-8?q?=D1=8B=D0=B9=20=D1=84=D0=B0=D0=B9=D0=BB=20=D0=B8=20=D0=BF=D0=B5?= =?UTF-8?q?=D1=80=D0=B5=D0=B6=D0=B0=D1=82=D1=8B=D0=B5=20=D0=BC=D0=BE=D0=B3?= =?UTF-8?q?=D1=83=D1=82=20=D0=B8=D0=BC=D0=B5=D1=82=D1=8C=20=D1=80=D0=B0?= =?UTF-8?q?=D0=B7=D0=BD=D1=8B=D0=B5=20=D1=84=D0=BE=D1=80=D0=BC=D0=B0=D1=82?= =?UTF-8?q?=D1=8B.=20=D0=94=D0=B0=D0=B6=D0=B5=20=D1=80=D0=B0=D0=B7=D0=BD?= =?UTF-8?q?=D1=8B=D0=B5=20=D1=81=D1=82=D0=B8=D0=BB=D0=B8=20=D0=BC=D0=BE?= =?UTF-8?q?=D0=B3=D1=83=D1=82=20=D0=B8=D0=BC=D0=B5=D1=82=D1=8C=20=D1=80?= =?UTF-8?q?=D0=B0=D0=B7=D0=BD=D1=8B=D0=B5=20=D1=84=D0=BE=D1=80=D0=BC=D0=B0?= =?UTF-8?q?=D1=82=D1=8B.=20=D0=9F=D0=B5=D1=80=D0=B5=D0=B4=D0=B0=D0=B2?= =?UTF-8?q?=D0=B0=D1=82=D1=8C=20=D0=B4=D0=BB=D1=8F=20=D0=B2=D1=81=D0=B5?= =?UTF-8?q?=D1=85=20content-type=20=D0=BE=D1=80=D0=B8=D0=B3=D0=B8=D0=BD?= =?UTF-8?q?=D0=B0=D0=BB=D1=8C=D0=BD=D0=BE=D0=B3=D0=BE=20=D1=84=D0=B0=D0=B9?= =?UTF-8?q?=D0=BB=D0=B0=20=D0=BD=D0=B5=D0=BF=D1=80=D0=B0=D0=B2=D0=B8=D0=BB?= =?UTF-8?q?=D1=8C=D0=BD=D0=BE.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/paperclip/optimizer.rb | 3 +-- lib/paperclip/storage/delayeds3.rb | 12 ++++++++---- lib/paperclip/upfile.rb | 14 +++++++++++--- 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/lib/paperclip/optimizer.rb b/lib/paperclip/optimizer.rb index 867df04..0af26b9 100644 --- a/lib/paperclip/optimizer.rb +++ b/lib/paperclip/optimizer.rb @@ -12,8 +12,7 @@ module Paperclip end def real_content_type - out = Paperclip.run "file", "--mime-type #{@file.path.shellescape}" - out.split(/:\s+/)[1].gsub("\n", "") + Paperclip::Upfile.content_type_from_file(@file.path) end def optimize(file) diff --git a/lib/paperclip/storage/delayeds3.rb b/lib/paperclip/storage/delayeds3.rb index f90a56a..4e5e6e2 100644 --- a/lib/paperclip/storage/delayeds3.rb +++ b/lib/paperclip/storage/delayeds3.rb @@ -184,6 +184,10 @@ module Paperclip h end + def file_content_type(path) + Paperclip::Upfile.content_type_from_file path + end + def write_to_s3 return true if instance_read(:synced_to_s3) paths = filesystem_paths @@ -195,7 +199,7 @@ module Paperclip s3_object = self.class.aws_bucket.object(s3_path(style)) s3_object.upload_file(file, cache_control: "max-age=#{10.year.to_i}", - content_type: instance_read(:content_type), + content_type: file_content_type(file), expires: 10.year.from_now.httpdate, acl: 'public-read') end @@ -215,7 +219,7 @@ module Paperclip s3_object = self.class.yandex_bucket.object(s3_path(style)) s3_object.upload_file(file, cache_control: "max-age=#{10.year.to_i}", - content_type: instance_read(:content_type), + content_type: file_content_type(file), expires: 10.year.from_now.httpdate, acl: 'public-read') end @@ -235,7 +239,7 @@ module Paperclip s3_object = self.class.sbercloud_bucket.object(s3_path(style)) s3_object.upload_file(file, cache_control: "max-age=#{10.year.to_i}", - content_type: instance_read(:content_type), + content_type: file_content_type(file), expires: 10.year.from_now.httpdate, acl: 'public-read') end @@ -255,7 +259,7 @@ module Paperclip path = s3_path(style) log "Saving to Fog with key #{path}" options = { - "Content-Type" => instance_read(:content_type), + "Content-Type" => file_content_type(file), "Cache-Control" => "max-age=#{10.year.to_i}", "x-goog-acl" => "public-read" } diff --git a/lib/paperclip/upfile.rb b/lib/paperclip/upfile.rb index 9558b64..a1ebfad 100644 --- a/lib/paperclip/upfile.rb +++ b/lib/paperclip/upfile.rb @@ -5,10 +5,11 @@ module Paperclip module Upfile # Infer the MIME-type of the file from the extension. def content_type - Paperclip::Upfile.content_type self.path + Paperclip::Upfile.content_type_from_ext self.path end - def self.content_type path + # TODO: Переписать через MIME::Types + def self.content_type_from_ext(path) type = (path.match(/\.(\w+)$/)[1] rescue "octet-stream").downcase case type when %r"jpe?g" then "image/jpeg" @@ -22,6 +23,13 @@ module Paperclip end end + def self.content_type_from_file(path) + Paperclip + .run("file", "--mime-type #{path.shellescape}") + .split(/:\s+/)[1] + .gsub("\n", "") + end + attr_writer :original_filename # Returns the file's normal name. @@ -45,7 +53,7 @@ if defined? StringIO end def content_type - @content_type ||= Paperclip::Upfile.content_type original_filename + @content_type ||= Paperclip::Upfile.content_type_from_ext original_filename end end end