lib/paperclip/storage/cloudinary.rb in paperclip-cloudinary-1.2.0 vs lib/paperclip/storage/cloudinary.rb in paperclip-cloudinary-1.3.0

- old
+ new

@@ -17,10 +17,11 @@ def flush_writes @queued_for_write.each do |style_name, file| defaults = { public_id: public_id(style_name), + resource_type: resource_type, use_filename: true, unique_filename: false, overwrite: true, invalidate: true } @@ -43,24 +44,28 @@ @queued_for_write.clear end def flush_deletes @queued_for_delete.each do |path| - ::Cloudinary::Uploader.destroy path[0..-(File.extname(path).length + 1)] + defaults = { + resource_type: resource_type, + invalidate: true + } + ::Cloudinary::Uploader.destroy public_id_for_path(path), defaults end @queued_for_delete.clear end def copy_to_local_file style, local_dest_path File.open(local_dest_path, 'wb') do |file| - file.write ::Cloudinary::Downloader.download(path(style)) + file.write ::Cloudinary::Downloader.download(url(style)) end end def exists? style = default_style - ::Cloudinary::Uploader.exists? path(style) + ::Cloudinary::Uploader.exists? public_id(style), cloudinary_url_options(style) end def url style_or_options = default_style, options = {} if style_or_options.is_a?(Hash) options.merge! style_or_options @@ -71,14 +76,22 @@ inline_opts = options[:cloudinary] || {} ::Cloudinary::Utils.cloudinary_url path(style), cloudinary_url_options(style, inline_opts) end def public_id style - s = path style + public_id_for_path(path style) + end + + def public_id_for_path s s[0..-(File.extname(s).length + 1)] end + def resource_type + type = @options[:cloudinary_resource_type] || 'image' + %w{image raw video audio}.include?(type.to_s) ? type.to_s : 'image' + end + def cloud_name cloudinary_credentials[:cloud_name] end def api_key @@ -88,11 +101,11 @@ def api_secret cloudinary_credentials[:api_secret] end def cloudinary_credentials - @cloudinary_credentials ||= parse_credentials(@options[:cloudinary_credentials] || Rails.root.join("config/cloudinary.yml")) + @cloudinary_credentials ||= parse_credentials(@options[:cloudinary_credentials] || find_default_config_path) @cloudinary_credentials end def parse_credentials creds creds = creds.respond_to?('call') ? creds.call(self) : creds @@ -106,17 +119,28 @@ def cloudinary_url_options style_name, inline_opts={} url_opts = @options[:cloudinary_url_options] || {} default_opts = url_opts[:default] || {} style_opts = url_opts[:styles].try(:[], style_name) || {} + default_opts[:resource_type] = resource_type + options = {} [default_opts, style_opts, inline_opts].each do |opts| options.deep_merge!(opts) do |key, existing_value, new_value| new_value.try(:call, style_name, self) || new_value end end options.merge! default_opts options + end + + def find_default_config_path + config_path = Rails.root.join("config/cloudinary.yml") + if File.exist? config_path + return config_path + else + return Rails.root.join("config/cloudinary.yaml") + end end def find_credentials creds case creds when File