Skip to content

Commit

Permalink
Extract PreloadedFile for easy handling of files preloaded by the Clo…
Browse files Browse the repository at this point in the history
…udinary's jQuery library
  • Loading branch information
TalLevAmi committed Aug 25, 2012
1 parent 7a81033 commit 0413d98
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 19 deletions.
1 change: 1 addition & 0 deletions lib/cloudinary.rb
Expand Up @@ -9,6 +9,7 @@
require "cloudinary/api"
require "cloudinary/downloader"
require "cloudinary/blob"
require "cloudinary/preloaded_file"
require "cloudinary/static"
require "cloudinary/missing"
require "cloudinary/carrier_wave" if defined?(::CarrierWave)
Expand Down
31 changes: 12 additions & 19 deletions lib/cloudinary/carrier_wave/preloaded.rb
Expand Up @@ -3,7 +3,7 @@
# Field value must be in the format: "image/upload/v<version>/#<public_id>.<format>#<signature>"
# Where signature is the cloduinary API signature on the public_id and version.
module Cloudinary::CarrierWave
PRELOADED_CLOUDINARY_PATH = /^([^\/]+)\/upload\/v(\d+)\/([^\/]+)#([^\/]+)$/
PRELOADED_CLOUDINARY_PATH = Cloudinary::PreloadedFile::PRELOADED_CLOUDINARY_PATH

def cache!(new_file)
if new_file.is_a?(String) && new_file.match(PRELOADED_CLOUDINARY_PATH)
Expand Down Expand Up @@ -35,28 +35,21 @@ def cache_name
return @file.is_a?(PreloadedCloudinaryFile) ? @file.to_s : super
end

class PreloadedCloudinaryFile
attr_reader :original_filename, :version, :public_id, :signature
class PreloadedCloudinaryFile < Cloudinary::PreloadedFile
def initialize(file_info)
resource_type, @version, @original_filename, @signature = file_info.scan(PRELOADED_CLOUDINARY_PATH).first
raise "Cloudinary CarrierWave integration supports images only" if resource_type != "image"
@public_id = @original_filename[0..(@original_filename.rindex(".")-1)]
expected_signature = Cloudinary::Utils.api_sign_request({:public_id=>public_id, :version=>version}, Cloudinary.config.api_secret)
if @signature != expected_signature
super
raise "Cloudinary CarrierWave integration only supports uploaded images" if resource_type != "image" || type != "upload"
if !valid?
raise CarrierWave::IntegrityError, I18n.translate(:"errors.messages.cloudinary_signature_error", :public_id=>public_id, :default=>"Invalid signature for #{public_id}")
end
end

def identifier
"v#{version}/#{original_filename}"
end

def to_s
"image/upload/v#{version}/#{original_filename}##{signature}"
end
end
end

def delete
# Do nothing. This is a virtual file.
end
end

def original_filename
self.filename
end
end
end
23 changes: 23 additions & 0 deletions lib/cloudinary/preloaded_file.rb
@@ -0,0 +1,23 @@
class Cloudinary::PreloadedFile
PRELOADED_CLOUDINARY_PATH = /^([^\/]+)\/([^\/]+)\/v(\d+)\/([^\/]+)#([^\/]+)$/

attr_reader :filename, :version, :public_id, :signature, :resource_type, :type
def initialize(file_info)
@resource_type, @type, @version, @filename, @signature = file_info.scan(PRELOADED_CLOUDINARY_PATH).first
@public_id = @resource_type == "image" ? @filename[0..(@filename.rindex(".")-1)] : @filename
end

def valid?
expected_signature = Cloudinary::Utils.api_sign_request({:public_id=>public_id, :version=>version}, Cloudinary.config.api_secret)
@signature == expected_signature
end

def identifier
"v#{version}/#{filename}"
end

def to_s
"#{resource_type}/#{type}/v#{version}/#{filename}##{signature}"
end

end

0 comments on commit 0413d98

Please sign in to comment.