class ImageServer::Uploader
Public Class Methods
new(namespace, source, outputs, configuration: ImageServer.configuration)
click to toggle source
# File lib/image_server/uploader.rb, line 5 def initialize(namespace, source, outputs, configuration: ImageServer.configuration) @namespace = namespace @source = source @outputs = outputs @configuration = configuration end
Public Instance Methods
upload(force: false)
click to toggle source
# File lib/image_server/uploader.rb, line 12 def upload(force: false) return existing_image_property if existing_image_property && !force uploader = Adapters::Http.new(@namespace, @source, configuration: @configuration) properties_json = uploader.upload(uri) find_or_create_image_property(properties_json) rescue PermanentFailure => e raise UploadError.new(e.message) end
Private Instance Methods
existing_image_property()
click to toggle source
# File lib/image_server/uploader.rb, line 49 def existing_image_property return unless url @existing_image_property ||= ImageProperty.where(namespace: @namespace).where('image_url = ?', url.downcase).first end
find_or_create_image_property(properties)
click to toggle source
# File lib/image_server/uploader.rb, line 32 def find_or_create_image_property(properties) attributes = { width: properties['width'], height: properties['height'], image_url: (url.downcase if url), namespace: @namespace } ip = ImageProperty.where(image_hash: properties['hash'], namespace: @namespace).first || ImageProperty.new(image_hash: properties['hash']) ip.assign_attributes(attributes) ip.save! ip end
source_is_url?()
click to toggle source
# File lib/image_server/uploader.rb, line 54 def source_is_url? @source.is_a?(String) && (@source.start_with?('http') || @source.start_with?('//')) end
uri()
click to toggle source
# File lib/image_server/uploader.rb, line 24 def uri uri = URI.parse("#{@configuration.upload_host}/#{@namespace}") params = {outputs: @outputs} params[:source] = url if source_is_url? uri.query = URI.encode_www_form(params) uri end
url()
click to toggle source
# File lib/image_server/uploader.rb, line 45 def url return @source if source_is_url? end