markevans / dragonfly-s3_data_store

S3 data store for the Dragonfly ruby gem
MIT License
62 stars 58 forks source link

Make :path per-storage option as a global configuration option #9

Closed alain-sound-oasis closed 9 years ago

alain-sound-oasis commented 10 years ago

This is an excellent gem and is used in RefineryCMS https://github.com/refinery/refinerycms . As a user of Refinery, I would like to be able to set the path on S3, as indicated in the docs here https://github.com/markevans/dragonfly-s3_data_store like so:

    Dragonfly.app.store(some_file, path: 'some/path.txt', headers: {'x-amz-acl' => 'public-read-write'})

The problem is that Refinery does not call Dragonfly.app.store(...) but installs Dragonfly as an extension to ActiveRecord:

# refinerycms/images/lib/refinery/images/dragonfly.rb

          ActiveRecord::Base.extend ::Dragonfly::Model
          ActiveRecord::Base.extend ::Dragonfly::Model::Validations
          ...    
          app_images = ::Dragonfly.app(:refinery_images)
          ...
          if ::Refinery::Images.s3_backend
            require 'dragonfly/s3_data_store'
            options = {
              bucket_name: Refinery::Images.s3_bucket_name,
              access_key_id: Refinery::Images.s3_access_key_id,
              secret_access_key: Refinery::Images.s3_secret_access_key
            }
            # S3 Region otherwise defaults to 'us-east-1'
            options.update(region: Refinery::Images.s3_region) if Refinery::Images.s3_region
            app_images.use_datastore :s3, options
          end

I propose to add a :global_path configuration option (similar to :root_path) that will have the following behaviour. If :global_path is specified, it is used instead of generate_uid

# dragonfly-s3_data_store/lib/dragonfly/s3_data_store.rb
...
      configurable_attr :url_host
      configurable_attr :global_path
...

def store(temp_object, opts={})
    ...
    headers['Content-Type'] = mime_type if mime_type
    unless global_path.nil?
        uid = global_path
    else
        uid = opts[:path] || generate_uid(temp_object.name || 'file')
    end
    ...
end

I understand that it will be my responsibility to make sure the paths are unique. Thanks!

markevans commented 9 years ago

that path option that can be used when using activemodel with storage_options (see README and http://markevans.github.io/dragonfly/models/#storage-options) - you can still use that with refinery thanks