module Paperclip::Storage::Ipfs

Public Class Methods

extended(base) click to toggle source
# File lib/paperclip/storage/ipfs.rb, line 9
def self.extended(base)
  begin
    require 'ipfs-api'
  rescue LoadError => e
    e.message << '(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 |attachment, _|
    attachment.hash
  end
  Paperclip.interpolates(:directory) do |attachment, style_name|
    attachment.directory style_name
  end
  Paperclip.interpolates(:gateway_url) do |attachment, style_name|
    attachment.gateway_url style_name
  end
end

Public Instance Methods

copy_to_local_file(style_name = default_style, destination_path) click to toggle source
# 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) click to toggle source
# File lib/paperclip/storage/ipfs.rb, line 37
def directory(style_name = default_style)
  "#{hash}/#{style_name}"
end
exists?(style_name = default_style) click to toggle source
# File lib/paperclip/storage/ipfs.rb, line 46
def exists?(style_name = default_style)
  hash(style_name).present?
end
gateway_url(style_name = default_style) click to toggle source
# 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() click to toggle source
# File lib/paperclip/storage/ipfs.rb, line 33
def hash
  instance_read('ipfs_hash'.to_sym)
end