<commit_before>module Refinery
  module PhotoGallery
    class Photo < ActiveRecord::Base
      belongs_to :album

      mount_uploader :file, Refinery::PhotoGallery::Admin::FileUploader
      acts_as_indexed :fields => [:title, :description]
      attr_accessible :album_id, :title, :description, :longitude, :latitude
      validates :title, :presence => true
      #TODO validate latitude/longitude - convert from nondecimal to decimal using inspiration from https://github.com/airblade/geo_tools/tree/master/lib/geo_tools

      before_validation :set_title
      before_create :exif_read
      #before_update :exif_write #TODO or use it after update?
      #TODO delete photo path from db? is it used?

      self.per_page = Refinery::PhotoGallery.photos_per_page

      def to_param
        "#{id}-#{title.parameterize}"
      end

      private

      def set_title
        self.title = self.file.file.basename.titleize unless self.title
      end

      def exif_read
        photo = MiniExiftool.new(self.file.file.file, {:numerical=> true})
        self.longitude = photo.GPSLongitude if self.longitude.nil?
        self.latitude = photo.GPSLatitude if self.latitude.nil?
        self.title = photo.DocumentName if photo.DocumentName.present?
        self.description = photo.ImageDescription if self.description.nil? && photo.ImageDescription != 'Exif_JPEG_PICTURE'
        # TODO read keywords from exif
      end

=begin
      #TODO add checkbox to check if it should be writed to exif, globally on/off writing
      def exif_write
        # should only write if tags are changed as images can be large and thus ExifTool will take a while to write to the file
        photo = MiniExiftool.new(self.file.file.file)
        photo.GPSLongitude = self.longitude
        photo.GPSLatitude = self.latitude
        photo.DocumentName = self.title
        photo.ImageDescription = self.description
        photo.Keywords = self.tags
        photo.save
      end
=end

    end
  end
end<commit_msg>prevent Argument error exception invalid byte sequence from MiniMagick<commit_after>module Refinery
  module PhotoGallery
    class Photo < ActiveRecord::Base
      belongs_to :album

      mount_uploader :file, Refinery::PhotoGallery::Admin::FileUploader
      acts_as_indexed :fields => [:title, :description]
      attr_accessible :album_id, :title, :description, :longitude, :latitude
      validates :title, :presence => true
      #TODO validate latitude/longitude - convert from nondecimal to decimal using inspiration from https://github.com/airblade/geo_tools/tree/master/lib/geo_tools

      before_validation :set_title
      before_create :exif_read
      #before_update :exif_write #TODO or use it after update?
      #TODO delete photo path from db? is it used?

      self.per_page = Refinery::PhotoGallery.photos_per_page

      def to_param
        "#{id}-#{title.parameterize}"
      end

      private

      def set_title
        self.title = self.file.file.basename.titleize unless self.title
      end

      def exif_read
      	 begin
          photo = MiniExiftool.new(self.file.file.file, {:numerical=> true})

          self.longitude = photo.GPSLongitude if self.longitude.nil?
          self.latitude = photo.GPSLatitude if self.latitude.nil?
          self.title = photo.DocumentName if photo.DocumentName.present?
          self.description = photo.ImageDescription if self.description.nil? && photo.ImageDescription != 'Exif_JPEG_PICTURE'
          # TODO read keywords from exif
        rescue
          p "ERROR raised exception during MiniExiftool reading"
        end
      end

=begin
      #TODO add checkbox to check if it should be writed to exif, globally on/off writing
      def exif_write
        # should only write if tags are changed as images can be large and thus ExifTool will take a while to write to the file
        photo = MiniExiftool.new(self.file.file.file)
        photo.GPSLongitude = self.longitude
        photo.GPSLatitude = self.latitude
        photo.DocumentName = self.title
        photo.ImageDescription = self.description
        photo.Keywords = self.tags
        photo.save
      end
=end

    end
  end
end
{'fraction_non_alphanumeric': 0.07464114832535886, 'fraction_numerical': 0.0, 'mean_word_length': 2.8078324225865208, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3388575', 'n_tokens_mistral': 1214, 'n_tokens_neox': 1191, 'n_words': 343}