class Imager::ServerInterface

Public Class Methods

client() click to toggle source
# File lib/imager/server_interface.rb, line 81
def self.client
  unless Imager::ServerClient.base_uri
    Imager::ServerClient.base_uri Imager.base_uri + '/' + Imager.manager_path
  end
  Imager::ServerClient
end
delete(collection, album, file_id) click to toggle source

@param [String] Collection for save the image @param [String] Album for save the image @param [String] File id @raise [ImagerError] if collection or album or file_id is wrong @raise [ArgumentError] when something with server comunication is wrong

# File lib/imager/server_interface.rb, line 66
def self.delete(collection, album, file_id)
  options = {}
  options[:collection] = collection.to_s
  options[:album]      = album.to_s
  options[:file_id]    = file_id.to_s

  options_json = JSON.generate options

  query = {}
  query[:auth]    = auth_token(options_json)
  query[:options] = options_json

  return parse client.post('/delete.php', multipart: true, body: query), true
end
post(collection, album, file, sizes, file_id = nil) click to toggle source

@param [String] Collection for save the image @param [String] Album for save the image @param [UploadIO, File, String] The image file or the path to it @param [Hash] Sizes you want to save. @see github.com/guilherme-otran/Imager#sizes-explain @param [String] File id. if passed file_id will be this instead of file.original_filename or File.basename(file). @return [void] @raise [ImagerError] if some server validation failed. @raise [ArgumentError] when something with server comunication is wrong

# File lib/imager/server_interface.rb, line 19
def self.post(collection, album, file, sizes, file_id = nil)
  raise Imager::ImagerError "File is empty", caller unless file

  if file.is_a?(String)
    raise Imager::ImagerError, "File is not a file", caller unless File.file?(file)
    file = File.new(file)
  end

  options = {}
  options[:collection] = collection.to_s
  options[:album]      = album.to_s
  options[:sizes]      = sizes

  options[:file_id]    = file_id
  options[:file_id]  ||= file.original_filename if file.respond_to?(:original_filename)
  options[:file_id]  ||= File.basename(file)

  # Remove file extension
  options[:file_id].gsub!(/(\..{3,4})\z/i, '')

  begin
    options[:file_md5]   = Digest::MD5.file(file)
    options[:file_sha1]  = Digest::SHA1.file(file)
  rescue
    raise Imager::ImagerError, "Cannot read the file", caller unless file.respond_to?(:read)

    # Fix for rubinius
    options[:file_md5]  = Digest::MD5.hexdigest(file.read)
    options[:file_sha1] = Digest::SHA1.hexdigest(file.read)
  end

  options_json = JSON.generate(options);

  query = {
    file: file,
    options: options_json,
    auth: auth_token(options_json)
  };

  return parse client.post('/post.php', multipart: true, body: query)
end

Private Class Methods

auth_token(options_json) click to toggle source
# File lib/imager/server_interface.rb, line 122
def self.auth_token(options_json)
  OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('md5'), Imager.auth_code, options_json)
end
parse(response, is_delete = false) click to toggle source
# File lib/imager/server_interface.rb, line 90
def self.parse(response, is_delete = false)
  case response.code
  when 204
    return true
  when 200..299
    parsed =  begin
                !!JSON.parse(response.body)
              rescue
                false
              end

    return true if parsed

    # Something is wrong with the server
    raise ArgumentError, "The server send an invalid response: #{response.body}", caller
  when 422
    raise ImagerError, response.body, caller
  when 404
    # We are deleting something that doesn't exist
    if (is_delete && response.body == "Cannot find the file.")
      raise ImagerError, response.body, caller
    else
      raise ArgumentError, "The server return an unexpected 404.", caller
    end
  when 401
    # Authentication with the server failed
    raise ArgumentError, "Authentication failed: " + response.body, caller
  else
    raise ArgumentError, "The server returned an error: " + response.body, caller
  end
end