Module: Paperclip::Storage::Ipfs
- Defined in:
- lib/paperclip/storage/ipfs.rb
Class Method Summary collapse
Instance Method Summary collapse
- #copy_to_local_file(style_name = default_style, destination_path) ⇒ Object
- #directory(style_name = default_style) ⇒ Object
- #exists?(style_name = default_style) ⇒ Boolean
-
#flush_deletes ⇒ Object
:nodoc:.
-
#flush_writes ⇒ Object
:nodoc:.
- #gateway_url(style_name = default_style) ⇒ Object
- #hash ⇒ Object
Class Method Details
.extended(base) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/paperclip/storage/ipfs.rb', line 9 def self.extended(base) begin require 'ipfs-api' rescue LoadError => e e. << '(You may need to install the ipfs-api gem)' raise e end base.instance_eval do @api_url = @options[:ipfs_api_url] || 'http://127.0.0.1:5001' @ipfs = IPFS::Connection.new(@api_url) end Paperclip.interpolates(:item_hash) do |, _| .hash end Paperclip.interpolates(:directory) do |, style_name| .directory style_name end Paperclip.interpolates(:gateway_url) do |, style_name| .gateway_url style_name end end |
Instance Method Details
#copy_to_local_file(style_name = default_style, destination_path) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/paperclip/storage/ipfs.rb', line 50 def copy_to_local_file(style_name = default_style, destination_path) File.open(destination_path, 'wb') do |fd| if @queued_for_write[style_name] IO.copy_stream(@queued_for_write[style_name], fd) else directory = directory(style_name) filename = Paperclip::Interpolations.filename(self, style_name.to_sym) fd.write(@ipfs.cat("#{directory}/#{filename}")) end end end |
#directory(style_name = default_style) ⇒ Object
37 38 39 |
# File 'lib/paperclip/storage/ipfs.rb', line 37 def directory(style_name = default_style) "#{hash}/#{style_name}" end |
#exists?(style_name = default_style) ⇒ Boolean
46 47 48 |
# File 'lib/paperclip/storage/ipfs.rb', line 46 def exists?(style_name = default_style) hash(style_name).present? end |
#flush_deletes ⇒ Object
:nodoc:
86 87 88 89 |
# File 'lib/paperclip/storage/ipfs.rb', line 86 def flush_deletes #:nodoc: log('deleting is not supported on IPFS') @queued_for_delete.clear end |
#flush_writes ⇒ Object
:nodoc:
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/paperclip/storage/ipfs.rb', line 62 def flush_writes #:nodoc: return if @queued_for_write.empty? Dir.mktmpdir do |tempdir| Dir.chdir(tempdir) do @attr = queued_for_write.each do |style_name, file| Dir.mkdir(style_name.to_s) File.open(Pathname.new(style_name.to_s).join(original_filename), 'wb') do |fd| IO.copy_stream(file, fd) end end end @ipfs.add Dir.new(tempdir) do |node| if node.name == File.basename(tempdir) instance_write('ipfs_hash'.to_sym, node.hash) end end end after_flush_writes @queued_for_write.clear instance.save end |
#gateway_url(style_name = default_style) ⇒ Object
41 42 43 44 |
# File 'lib/paperclip/storage/ipfs.rb', line 41 def gateway_url(style_name = default_style) filename = Paperclip::Interpolations.filename(self, style_name.to_sym) "https://gateway.ipfs.io/ipfs/#{directory style_name}/#{filename}" end |
#hash ⇒ Object
33 34 35 |
# File 'lib/paperclip/storage/ipfs.rb', line 33 def hash instance_read('ipfs_hash'.to_sym) end |