Permalink
Browse files

Extract PreloadedFile for easy handling of files preloaded by the Clo…

…udinary's jQuery library
  • Loading branch information...
1 parent 7a81033 commit 0413d98c0d090ecfe610e50f097d8a0c8a7fe393 @TalLevAmi TalLevAmi committed Aug 25, 2012
Showing with 36 additions and 19 deletions.
  1. +1 −0 lib/cloudinary.rb
  2. +12 −19 lib/cloudinary/carrier_wave/preloaded.rb
  3. +23 −0 lib/cloudinary/preloaded_file.rb
View
@@ -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)
@@ -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)
@@ -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
@@ -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.