lib/similar_images.rb
def save_search
    begin
      FileUtils.mkdir_p(SEARCH_CACHE_DIR, :mode => 0775)

      tempfile_path = "#{SEARCH_CACHE_DIR}/#{SecureRandom.random_number(2**32)}.upload"
      File.open(tempfile_path, "wb") { |f| yield f }

      # Use the resizer to validate the file and convert it to a thumbnail-size JPEG.
      imgsize = Moebooru::ImageSizeExif.path(tempfile_path)
      if imgsize[:type].blank?
        raise Moebooru::Resizer::ResizeError, "Unrecognized image format"
      end

      ret = {}
      ret[:original_width] = imgsize[:width]
      ret[:original_height] = imgsize[:height]
      size = Moebooru::Resizer.reduce_to({ :width => ret[:original_width], :height => ret[:original_height] }, :width => 150, :height => 150)
      ext = imgsize[:type].gsub(/jpeg/i, "jpg").downcase

      tempfile_path_resize = "#{tempfile_path}.2"
      Moebooru::Resizer.resize(ext, tempfile_path, tempfile_path_resize, size, 95)
      FileUtils.mv(tempfile_path_resize, tempfile_path)

      md5 = Moebooru::Hasher.compute_one(tempfile_path, :md5)
      id = "#{md5}.#{ext}"
      file_path = "#{SEARCH_CACHE_DIR}/#{id}"

      FileUtils.mv(tempfile_path, file_path)
      FileUtils.chmod(0664, file_path)
    rescue
      FileUtils.rm_f(file_path) if file_path
      raise
    ensure
      FileUtils.rm_f(tempfile_path) if tempfile_path
      FileUtils.rm_f(tempfile_path_resize) if tempfile_path_resize
    end

    ret[:file_path] = file_path
    ret[:search_id] = id
    ret
  end
# Save a file locally to be searched for.  Returns the path to the saved file, and
# the search ID which can be passed to find_saved_search.
Save a file locally to be searched for.  Returns the path to the saved file, and
the search ID which can be passed to find_saved_search.
Save a file locally to be searched for . Returns the path to the saved file and the search ID which can be passed to find_saved_search .
def save_search
    begin
      FileUtils.mkdir_p(SEARCH_CACHE_DIR, :mode => 0775)
      tempfile_path = "#{SEARCH_CACHE_DIR}/#{SecureRandom.random_number(2**32)}.upload"
      File.open(tempfile_path, "wb") { |f| yield f }
      imgsize = Moebooru::ImageSizeExif.path(tempfile_path)
      if imgsize[:type].blank?
        raise Moebooru::Resizer::ResizeError, "Unrecognized image format"
      end
      ret = {}
      ret[:original_width] = imgsize[:width]
      ret[:original_height] = imgsize[:height]
      size = Moebooru::Resizer.reduce_to({ :width => ret[:original_width], :height => ret[:original_height] }, :width => 150, :height => 150)
      ext = imgsize[:type].gsub(/jpeg/i, "jpg").downcase
      tempfile_path_resize = "#{tempfile_path}.2"
      Moebooru::Resizer.resize(ext, tempfile_path, tempfile_path_resize, size, 95)
      FileUtils.mv(tempfile_path_resize, tempfile_path)
      md5 = Moebooru::Hasher.compute_one(tempfile_path, :md5)
      id = "#{md5}.#{ext}"
      file_path = "#{SEARCH_CACHE_DIR}/#{id}"
      FileUtils.mv(tempfile_path, file_path)
      FileUtils.chmod(0664, file_path)
    rescue
      FileUtils.rm_f(file_path) if file_path
      raise
    ensure
      FileUtils.rm_f(tempfile_path) if tempfile_path
      FileUtils.rm_f(tempfile_path_resize) if tempfile_path_resize
    end
    ret[:file_path] = file_path
    ret[:search_id] = id
    ret
  end
def save_search begin FileUtils . mkdir_p ( SEARCH_CACHE_DIR , :mode => 0775 ) tempfile_path = "#{SEARCH_CACHE_DIR}/#{SecureRandom.random_number(2**32)}.upload" File . open ( tempfile_path , "wb" ) { | f | yield f } imgsize = Moebooru :: ImageSizeExif . path ( tempfile_path ) if imgsize [ :type ] . blank? raise Moebooru :: Resizer :: ResizeError , "Unrecognized image format" end ret = { } ret [ :original_width ] = imgsize [ :width ] ret [ :original_height ] = imgsize [ :height ] size = Moebooru :: Resizer . reduce_to ( { :width => ret [ :original_width ] , :height => ret [ :original_height ] } , :width => 150 , :height => 150 ) ext = imgsize [ :type ] . gsub ( / jpeg /i , "jpg" ) . downcase tempfile_path_resize = "#{tempfile_path}.2" Moebooru :: Resizer . resize ( ext , tempfile_path , tempfile_path_resize , size , 95 ) FileUtils . mv ( tempfile_path_resize , tempfile_path ) md5 = Moebooru :: Hasher . compute_one ( tempfile_path , :md5 ) id = "#{md5}.#{ext}" file_path = "#{SEARCH_CACHE_DIR}/#{id}" FileUtils . mv ( tempfile_path , file_path ) FileUtils . chmod ( 0664 , file_path ) rescue FileUtils . rm_f ( file_path ) if file_path raise ensure FileUtils . rm_f ( tempfile_path ) if tempfile_path FileUtils . rm_f ( tempfile_path_resize ) if tempfile_path_resize end ret [ :file_path ] = file_path ret [ :search_id ] = id ret end
Save a file locally to be searched for.
Save a file locally to be searched for .
# Use the resizer to validate the file and convert it to a thumbnail-size JPEG.