<commit_before>require "open3"
require "ftools"
require "matrix"
require 'erb'
require 'rexml/document'
require 'nokogiri'
require 'open-uri'
include ErrorCalculator


class Map < ActiveRecord::Base
  has_many :my_maps, :dependent => :destroy
  has_many :users, :through => :my_maps
  belongs_to :owner, :class_name => "User"
  belongs_to :import
  has_one :user, :through => :my_maps
  has_many :gcps,  :dependent => :destroy
  has_many :layers_maps,  :dependent => :destroy
  has_many :layers, :through => :layers_maps # ,:after_add, :after_remove
  has_many :groups_maps, :dependent => :destroy
  has_many :groups, :through => :groups_maps
  acts_as_taggable
  has_attached_file :upload, :styles => {:thumb => ["100x100>", :png]} ,
    :url => '/:attachment/:id/:style/:basename.:extension'
  attr_protected :upload_file_name, :upload_content_type, :upload_size

  validates_attachment_size(:upload, :less_than => MAX_ATTACHMENT_SIZE) if defined?(MAX_ATTACHMENT_SIZE)

  acts_as_audited :except => [:filename]

  acts_as_commentable
  acts_as_enum :status, [:unloaded, :loading, :available, :warping, :warped, :published]
  acts_as_enum :mask_status, [:unmasked, :masking, :masked]
  acts_as_enum :map_type, [:index, :is_map, :not_map ]
  acts_as_enum :rough_state, [:step_1, :step_2, :step_3, :step_4]
  default_values :status => :unloaded, :mask_status => :unmasked, :map_type => :is_map, :rough_state => :step_1

  named_scope :public, :conditions => ['public = ?', true]
  named_scope :warped, :conditions => {:status => [Map.status(:warped), Map.status(:published)], :map_type => Map.map_type(:is_map) }
  named_scope :published, :conditions => {:status => Map.status(:published), :map_type => Map.map_type(:is_map)}

  named_scope :real_maps, :conditions => {:map_type => Map.map_type(:is_map)}
  attr_accessor :error
  attr_accessor :upload_url

  validates_presence_of :title
  validates_numericality_of :rough_lat, :rough_lon, :rough_zoom, :allow_nil => true
  validates_numericality_of :metadata_lat, :metadata_lon, :allow_nil => true
  
  before_create :download_remote_image, :if => :upload_url_provided?
  before_create :save_dimensions
  after_create :setup_image
  after_destroy :delete_images
  after_destroy :delete_map, :update_counter_cache, :update_layers
  after_save :update_counter_cache
  
  #############################################
  #CUSTOM VALIDATIONS
  #############################################

  def validate_on_create
    errors.add(:filename, "is already being used") if Map.find_by_upload_file_name(upload.original_filename)
  end


  #############################################
  #FILTERS
  #############################################

  def upload_url_provided?
    !self.upload_url.blank?
  end

  def download_remote_image
    img_upload = do_download_remote_image
    unless img_upload
      errors.add(:upload_url, "is invalid or inaccessible")
      return false
    end
    self.upload = img_upload
    self.source_uri = upload_url
  end

  def do_download_remote_image<commit_msg>fix for validation of existing maps for map by upload<commit_after>require "open3"
require "ftools"
require "matrix"
require 'erb'
require 'rexml/document'
require 'nokogiri'
require 'open-uri'
include ErrorCalculator


class Map < ActiveRecord::Base
  has_many :my_maps, :dependent => :destroy
  has_many :users, :through => :my_maps
  belongs_to :owner, :class_name => "User"
  belongs_to :import
  has_one :user, :through => :my_maps
  has_many :gcps,  :dependent => :destroy
  has_many :layers_maps,  :dependent => :destroy
  has_many :layers, :through => :layers_maps # ,:after_add, :after_remove
  has_many :groups_maps, :dependent => :destroy
  has_many :groups, :through => :groups_maps
  acts_as_taggable
  has_attached_file :upload, :styles => {:thumb => ["100x100>", :png]} ,
    :url => '/:attachment/:id/:style/:basename.:extension'
  attr_protected :upload_file_name, :upload_content_type, :upload_size

  validates_attachment_size(:upload, :less_than => MAX_ATTACHMENT_SIZE) if defined?(MAX_ATTACHMENT_SIZE)

  acts_as_audited :except => [:filename]

  acts_as_commentable
  acts_as_enum :status, [:unloaded, :loading, :available, :warping, :warped, :published]
  acts_as_enum :mask_status, [:unmasked, :masking, :masked]
  acts_as_enum :map_type, [:index, :is_map, :not_map ]
  acts_as_enum :rough_state, [:step_1, :step_2, :step_3, :step_4]
  default_values :status => :unloaded, :mask_status => :unmasked, :map_type => :is_map, :rough_state => :step_1

  named_scope :public, :conditions => ['public = ?', true]
  named_scope :warped, :conditions => {:status => [Map.status(:warped), Map.status(:published)], :map_type => Map.map_type(:is_map) }
  named_scope :published, :conditions => {:status => Map.status(:published), :map_type => Map.map_type(:is_map)}

  named_scope :real_maps, :conditions => {:map_type => Map.map_type(:is_map)}
  attr_accessor :error
  attr_accessor :upload_url

  validates_presence_of :title
  validates_numericality_of :rough_lat, :rough_lon, :rough_zoom, :allow_nil => true
  validates_numericality_of :metadata_lat, :metadata_lon, :allow_nil => true
  
  before_create :download_remote_image, :if => :upload_url_provided?
  before_create :save_dimensions
  after_create :setup_image
  after_destroy :delete_images
  after_destroy :delete_map, :update_counter_cache, :update_layers
  after_save :update_counter_cache
  
  #############################################
  #CUSTOM VALIDATIONS
  #############################################

  def validate_on_create
    errors.add(:filename, "is already being used") if Map.find_by_upload_file_name(upload.original_filename)
  end


  #############################################
  #FILTERS
  #############################################

  def upload_url_provided?
    !self.upload_url.blank?
  end

  def download_remote_image
    img_upload = do_download_remote_image
    unless img_upload
      errors.add(:upload_url, "is invalid or inaccessible")
      return false
    end
    self.upload = img_upload
    self.source_uri = upload_url

    if Map.find_by_upload_file_name(upload.original_filename)
      errors.add(:filename, "is already being used")
      return false
    end
    
  end

  def do_download_remote_image
{'fraction_non_alphanumeric': 0.16842273307790548, 'fraction_numerical': 0.0038314176245210726, 'mean_word_length': 5.772972972972973, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 65, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5240423', 'n_tokens_mistral': 2119, 'n_tokens_neox': 2065, 'n_words': 438}