module Paperclip::Storage::Multiple

This is a Paperclip storage to work simultaneously with a filesystem and fog backends.

It’s optimized to migrate from filesystem to s3 (with fog), so it’s required that assets always have their filesystem version to begin with.

Public Class Methods

extended(attachment) click to toggle source
# File lib/paperclip/storage/multiple.rb, line 9
def self.extended attachment
  if attachment.options[:multiple_if].call(attachment.instance)
    attachment.instance_eval do
      @filesystem = Attachment.new(attachment.name, attachment.instance, attachment.options.merge(storage: :filesystem))
      @fog        = Attachment.new(attachment.name, attachment.instance, attachment.options.merge(storage: :fog))

      # `after_flush_writes` closes files and unlinks them, but we have to read them twice to save them
      # on both storages, so we blank this one, and let the other attachment close the files.
      def @fog.after_flush_writes; end
    end
  else
    attachment.extend Filesystem
  end
end

Public Instance Methods

copy_to_local_file(style, local_dest_path) click to toggle source

This defaults to the filesystem version, since it’s a lot faster than querying s3.

# File lib/paperclip/storage/multiple.rb, line 75
def copy_to_local_file(style, local_dest_path)
  @filesystem.copy_to_local_file(style, local_dest_path)
end
display_from_s3?() click to toggle source
# File lib/paperclip/storage/multiple.rb, line 32
def display_from_s3?
  @options[:display_from_s3].call(instance) && use_multiple?
end
exists?(style_name = default_style) click to toggle source

This defaults to the filesystem version, since it’s a lot faster than querying s3.

# File lib/paperclip/storage/multiple.rb, line 38
def exists?(style_name = default_style)
  filesystem.exists?
end
expiring_url(time = (Time.now + 3600), style = default_style) click to toggle source
# File lib/paperclip/storage/multiple.rb, line 105
def expiring_url(time = (Time.now + 3600), style = default_style)
  @fog.expiring_url(time, style)
end
filesystem() click to toggle source
# File lib/paperclip/storage/multiple.rb, line 24
def filesystem
  @filesystem
end
flush_deletes() click to toggle source

Delegates to both filesystem and fog storages.

# File lib/paperclip/storage/multiple.rb, line 63
def flush_deletes
  @filesystem.flush_deletes
  begin
    @fog.flush_deletes
  rescue Excon::Errors::Error => e
    log("There was an unexpected error while deleting file from fog: #{e}")
  end
  @queued_for_delete = []
end
flush_writes() click to toggle source

Delegates to both filesystem and fog storages.

# File lib/paperclip/storage/multiple.rb, line 44
def flush_writes
  sync(:@queued_for_write)

  @fog.flush_writes
  @filesystem.flush_writes

  @queued_for_write = {}
end
fog() click to toggle source
# File lib/paperclip/storage/multiple.rb, line 28
def fog
  @fog
end
path(style_name = default_style) click to toggle source
Calls superclass method
# File lib/paperclip/storage/multiple.rb, line 89
def path(style_name = default_style)
  if display_from_s3?
    @fog.path(style_name)
  elsif use_multiple?
    @filesystem.path(style_name)
  else
    super
  end
end
public_url(style = default_style) click to toggle source

These two are needed for general fog working-around

# File lib/paperclip/storage/multiple.rb, line 101
def public_url(style = default_style)
  @fog.public_url(style)
end
queue_all_for_delete() click to toggle source
Calls superclass method
# File lib/paperclip/storage/multiple.rb, line 53
def queue_all_for_delete
  if use_multiple? && file?
    @filesystem.send :queue_some_for_delete, *all_styles
    @fog.send :queue_some_for_delete, *all_styles
  end
  super
end
url(style_name = default_style, options = {}) click to toggle source
Calls superclass method
# File lib/paperclip/storage/multiple.rb, line 79
def url(style_name = default_style, options = {})
  if display_from_s3?
    @fog.url(style_name, options)
  elsif use_multiple?
    @filesystem.url(style_name, options)
  else
    super
  end
end

Private Instance Methods

all_styles() click to toggle source
# File lib/paperclip/storage/multiple.rb, line 111
def all_styles
  [:original, *styles.keys]
end
sync(ivar) click to toggle source
# File lib/paperclip/storage/multiple.rb, line 115
def sync(ivar)
  @fog.instance_variable_set(ivar, instance_variable_get(ivar))
  @filesystem.instance_variable_set(ivar, instance_variable_get(ivar))
end
use_multiple?() click to toggle source
# File lib/paperclip/storage/multiple.rb, line 120
def use_multiple?
  @options[:multiple_if].call(instance)
end