-
-
Save aried3r/cfe9a32039c17245796c9477587c3866 to your computer and use it in GitHub Desktop.
Shrine plugin cannot call `infer_extension`
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: true | |
require 'bundler/inline' | |
gemfile do | |
source 'https://rubygems.org' | |
gem "activerecord" | |
gem "sqlite3" | |
## Shrine v2 | |
# gem "shrine", "~> 2.19" | |
# gem "shrine-memory" | |
## Shrine v3 | |
gem "shrine", "~> 3.4" | |
gem "mime-types" | |
end | |
require "active_record" | |
require "shrine" | |
require "shrine/storage/memory" | |
require "down" | |
Shrine.storages = { | |
cache: Shrine::Storage::Memory.new, | |
store: Shrine::Storage::Memory.new, | |
} | |
Shrine.plugin :activerecord | |
Shrine.plugin :determine_mime_type, analyzer: :mime_types | |
class Shrine | |
module Plugins | |
module HashLocation | |
def self.load_dependencies(uploader, *) | |
uploader.plugin :signature | |
uploader.plugin :infer_extension | |
end | |
module InstanceMethods | |
# Just an example, don't use in production! | |
def generate_location(io, action:, name:, metadata:, **) | |
if action == :upload | |
super | |
else | |
# Works | |
sha256 = calculate_signature(io, :sha256, format: :hex) | |
p "SHA256: #{sha256}" | |
first_dir = sha256[0] | |
second_dir = sha256[1] | |
# Works in Shrine v2, but fails in v3 with: | |
# shrine-plugin-dependencies.rb:52:in `generate_location': undefined method `infer_extension' for #<MyUploader:0x00007ffab4588f98 @storage_key=:cache> (NoMethodError) | |
extension = infer_extension(metadata['mime_type']) | |
p "Extension: #{extension}" | |
filename = "#{name}-#{sha256}-#{extension}" | |
[first_dir, second_dir, filename].join('/') | |
end | |
end | |
end | |
end | |
register_plugin(:hash_location, HashLocation) | |
end | |
end | |
class MyUploader < Shrine | |
plugin :hash_location | |
end | |
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:") | |
ActiveRecord::Base.connection.create_table(:posts) { |t| t.text :image_data } | |
class Post < ActiveRecord::Base | |
include MyUploader::Attachment(:image) | |
end | |
Post.create(image: Down.download("https://loremflickr.com/cache/resized/65535_50735100323_e42ce59a8f_320_240_nofilter.jpg")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment