Skip to content

Instantly share code, notes, and snippets.

@katafrakt
Created October 18, 2019 21:41
Show Gist options
  • Save katafrakt/35f1e114cbb485b36a85e9a03d9ed7ac to your computer and use it in GitHub Desktop.
Save katafrakt/35f1e114cbb485b36a85e9a03d9ed7ac to your computer and use it in GitHub Desktop.
Shrine 3 entity problem
require 'shrine'
require 'shrine/storage/file_system'
require 'image_processing/mini_magick'
Dir.mktmpdir do |tmp|
Shrine.storages = {
cache: Shrine::Storage::FileSystem.new(tmp),
store: Shrine::Storage::FileSystem.new(tmp, prefix: 'uploads')
}
class Uploader < Shrine
plugin :entity
plugin :derivatives
Attacher.derivatives_processor do |original|
magick = ImageProcessing::MiniMagick.source(original)
size_100 = magick.resize_to_limit!(100, 100)
size_30 = magick.resize_to_limit!(30, 30)
{original: original, small: size_100, tiny: size_30}
end
end
class SomeEntity
attr_accessor :image_data
include Uploader::Attachment(:image)
def initialize(image_data: nil)
@image_data = image_data
end
end
`curl https://cdn.pixabay.com/photo/2017/02/20/18/03/cat-2083492_960_720.jpg --output /#{tmp}/shrine.jpg`
attacher = Uploader::Attacher.from_entity(SomeEntity.new, :image)
attacher.attach(File.open("/#{tmp}/shrine.jpg"))
p attacher.column_data
ent = SomeEntity.new(image_data: attacher.column_data)
ent.image_attacher.create_derivatives
end
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 100 139k 100 139k 0 0 397k 0 --:--:-- --:--:-- --:--:-- 397k
"{\"id\":\"a93a8da72458e1aaffbd4ba4f697d835.jpg\",\"storage\":\"store\",\"metadata\":{\"filename\":\"shrine.jpg\",\"size\":143288,\"mime_type\":null}}"
Traceback (most recent call last):
16: from test_.rb:5:in `<main>'
15: from /home/katafrakt/.rbenv/versions/2.5.6/lib/ruby/2.5.0/tmpdir.rb:93:in `mktmpdir'
14: from test_.rb:39:in `block in <main>'
13: from /home/katafrakt/.rbenv/versions/2.5.6/lib/ruby/gems/2.5.0/gems/shrine-3.0.1/lib/shrine/plugins/derivatives.rb:184:in `create_derivatives'
12: from /home/katafrakt/.rbenv/versions/2.5.6/lib/ruby/gems/2.5.0/gems/shrine-3.0.1/lib/shrine/plugins/derivatives.rb:201:in `add_derivatives'
11: from /home/katafrakt/.rbenv/versions/2.5.6/lib/ruby/gems/2.5.0/gems/shrine-3.0.1/lib/shrine/plugins/derivatives.rb:228:in `upload_derivatives'
10: from /home/katafrakt/.rbenv/versions/2.5.6/lib/ruby/gems/2.5.0/gems/shrine-3.0.1/lib/shrine/plugins/derivatives.rb:466:in `map_derivative'
9: from /home/katafrakt/.rbenv/versions/2.5.6/lib/ruby/gems/2.5.0/gems/shrine-3.0.1/lib/shrine/plugins/derivatives.rb:557:in `map_derivative'
8: from /home/katafrakt/.rbenv/versions/2.5.6/lib/ruby/gems/2.5.0/gems/shrine-3.0.1/lib/shrine/plugins/derivatives.rb:557:in `inject'
7: from /home/katafrakt/.rbenv/versions/2.5.6/lib/ruby/gems/2.5.0/gems/shrine-3.0.1/lib/shrine/plugins/derivatives.rb:557:in `each'
6: from /home/katafrakt/.rbenv/versions/2.5.6/lib/ruby/gems/2.5.0/gems/shrine-3.0.1/lib/shrine/plugins/derivatives.rb:560:in `block in map_derivative'
5: from /home/katafrakt/.rbenv/versions/2.5.6/lib/ruby/gems/2.5.0/gems/shrine-3.0.1/lib/shrine/plugins/derivatives.rb:575:in `map_derivative'
4: from /home/katafrakt/.rbenv/versions/2.5.6/lib/ruby/gems/2.5.0/gems/shrine-3.0.1/lib/shrine/plugins/derivatives.rb:231:in `block in upload_derivatives'
3: from /home/katafrakt/.rbenv/versions/2.5.6/lib/ruby/gems/2.5.0/gems/shrine-3.0.1/lib/shrine/plugins/derivatives.rb:242:in `upload_derivative'
2: from /home/katafrakt/.rbenv/versions/2.5.6/lib/ruby/2.5.0/tempfile.rb:145:in `open'
1: from /home/katafrakt/.rbenv/versions/2.5.6/lib/ruby/2.5.0/tempfile.rb:145:in `open'
/home/katafrakt/.rbenv/versions/2.5.6/lib/ruby/2.5.0/tempfile.rb:145:in `initialize': No such file or directory @ rb_sysopen - /tmp/shrine20191018-47070-1f2vf8c.jpg (Errno::ENOENT)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment