Class: Image

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
Housekeeping::Users, Shared::Identifiable, Shared::Notable, Shared::Taggable, SoftValidation
Defined in:
app/models/image.rb

Overview

Image - class the represents an image stored in the filesystem.

This class relies on the paperclip gem and the ImageMagik app to link, store and manipulate images.

Constant Summary collapse

MISSING_IMAGE_PATH =

constants

'/public/images/missing.jpg'

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from SoftValidation

#clear_soft_validations, #fix_soft_validations, #soft_fixed?, #soft_valid?, #soft_validate, #soft_validated?, #soft_validations

Methods included from Housekeeping::Users

#alive?, #set_created_by_id, #set_updated_by_id

Instance Attribute Details

#heightObject

Image - class the represents an image stored in the filesystem.

This class relies on the paperclip gem and the ImageMagik app to link, store and manipulate images.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'app/models/image.rb', line 20

class Image < ActiveRecord::Base
  include Housekeeping::Users
  include Shared::Identifiable
  include Shared::Notable
  include Shared::Taggable
  include SoftValidation

  #constants
  MISSING_IMAGE_PATH = '/public/images/missing.jpg'

  before_save :extract_tw_attributes

  has_attached_file :image_file,
       :styles => {:medium => '300x300>', :thumb => '100x100>'},
       :default_url => MISSING_IMAGE_PATH,
       :filename_cleaner => CleanseFilename
       #:restricted_characters => /[^A-Za-z0-9\.]/,
  validates_attachment_content_type :image_file, :content_type => /\Aimage\/.*\Z/
  validates_attachment_presence :image_file
  validates_attachment_size :image_file, greater_than: 1.kilobytes

  soft_validate(:sv_duplicate_image?)

  def has_duplicate?
    Image.exists?(:image_file_fingerprint => self.image_file_fingerprint)
  end

  def duplicate_images
    Image.where(:image_file_fingerprint => self.image_file_fingerprint).to_a
  end

  def exif
=begin
    if self.new_record?
      return false
    end
=end
=begin
bash shell extraction notes:
identify -format "%[EXIF:*]" ExifImage.jpg
a=`identify -format "%[EXIF:*]" ExifImage.jpg`

a.split("\n").collect{|b| b.gsub("exif:", "").split("=")}.inject({}){|hsh, c| hsh.merge(c[0]=>c[1])}

    # drop to the shell
=end
    # run an imageMagick command (identify?) pass to that command self.image_file.url

  end

  protected

  def extract_tw_attributes
    # NOTE: assumes content type is an image.
    tempfile = image_file.queued_for_write[:original]
    if tempfile.nil?
      self.width          = 0
      self.height         = 0
      self.user_file_name = ''
      #TODO should an error be thrown here?
    else
      self.user_file_name = tempfile.original_filename
      geometry            = Paperclip::Geometry.from_file(tempfile)
      self.width          = geometry.width.to_i
      self.height         = geometry.height.to_i
    end
  end

  #region soft_validation
  # Check md5 fingerprint against existing fingerprints
  def sv_duplicate_image?
    if has_duplicate?
      soft_validations.add(:image_file_fingerprint, 'This image is a duplicate of an image already stored.')
    end
  end

  #endregion  soft_validation
end

#idObject

The ActiveRecord id, which is used by paperclip to organize the images in the filesystem.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'app/models/image.rb', line 20

class Image < ActiveRecord::Base
  include Housekeeping::Users
  include Shared::Identifiable
  include Shared::Notable
  include Shared::Taggable
  include SoftValidation

  #constants
  MISSING_IMAGE_PATH = '/public/images/missing.jpg'

  before_save :extract_tw_attributes

  has_attached_file :image_file,
       :styles => {:medium => '300x300>', :thumb => '100x100>'},
       :default_url => MISSING_IMAGE_PATH,
       :filename_cleaner => CleanseFilename
       #:restricted_characters => /[^A-Za-z0-9\.]/,
  validates_attachment_content_type :image_file, :content_type => /\Aimage\/.*\Z/
  validates_attachment_presence :image_file
  validates_attachment_size :image_file, greater_than: 1.kilobytes

  soft_validate(:sv_duplicate_image?)

  def has_duplicate?
    Image.exists?(:image_file_fingerprint => self.image_file_fingerprint)
  end

  def duplicate_images
    Image.where(:image_file_fingerprint => self.image_file_fingerprint).to_a
  end

  def exif
=begin
    if self.new_record?
      return false
    end
=end
=begin
bash shell extraction notes:
identify -format "%[EXIF:*]" ExifImage.jpg
a=`identify -format "%[EXIF:*]" ExifImage.jpg`

a.split("\n").collect{|b| b.gsub("exif:", "").split("=")}.inject({}){|hsh, c| hsh.merge(c[0]=>c[1])}

    # drop to the shell
=end
    # run an imageMagick command (identify?) pass to that command self.image_file.url

  end

  protected

  def extract_tw_attributes
    # NOTE: assumes content type is an image.
    tempfile = image_file.queued_for_write[:original]
    if tempfile.nil?
      self.width          = 0
      self.height         = 0
      self.user_file_name = ''
      #TODO should an error be thrown here?
    else
      self.user_file_name = tempfile.original_filename
      geometry            = Paperclip::Geometry.from_file(tempfile)
      self.width          = geometry.width.to_i
      self.height         = geometry.height.to_i
    end
  end

  #region soft_validation
  # Check md5 fingerprint against existing fingerprints
  def sv_duplicate_image?
    if has_duplicate?
      soft_validations.add(:image_file_fingerprint, 'This image is a duplicate of an image already stored.')
    end
  end

  #endregion  soft_validation
end

#image_file_content_type The MIME (must be image) and file type (e.g. image/png).(TheMIME(must be image)) ⇒ Object

Image - class the represents an image stored in the filesystem.

This class relies on the paperclip gem and the ImageMagik app to link, store and manipulate images.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'app/models/image.rb', line 20

class Image < ActiveRecord::Base
  include Housekeeping::Users
  include Shared::Identifiable
  include Shared::Notable
  include Shared::Taggable
  include SoftValidation

  #constants
  MISSING_IMAGE_PATH = '/public/images/missing.jpg'

  before_save :extract_tw_attributes

  has_attached_file :image_file,
       :styles => {:medium => '300x300>', :thumb => '100x100>'},
       :default_url => MISSING_IMAGE_PATH,
       :filename_cleaner => CleanseFilename
       #:restricted_characters => /[^A-Za-z0-9\.]/,
  validates_attachment_content_type :image_file, :content_type => /\Aimage\/.*\Z/
  validates_attachment_presence :image_file
  validates_attachment_size :image_file, greater_than: 1.kilobytes

  soft_validate(:sv_duplicate_image?)

  def has_duplicate?
    Image.exists?(:image_file_fingerprint => self.image_file_fingerprint)
  end

  def duplicate_images
    Image.where(:image_file_fingerprint => self.image_file_fingerprint).to_a
  end

  def exif
=begin
    if self.new_record?
      return false
    end
=end
=begin
bash shell extraction notes:
identify -format "%[EXIF:*]" ExifImage.jpg
a=`identify -format "%[EXIF:*]" ExifImage.jpg`

a.split("\n").collect{|b| b.gsub("exif:", "").split("=")}.inject({}){|hsh, c| hsh.merge(c[0]=>c[1])}

    # drop to the shell
=end
    # run an imageMagick command (identify?) pass to that command self.image_file.url

  end

  protected

  def extract_tw_attributes
    # NOTE: assumes content type is an image.
    tempfile = image_file.queued_for_write[:original]
    if tempfile.nil?
      self.width          = 0
      self.height         = 0
      self.user_file_name = ''
      #TODO should an error be thrown here?
    else
      self.user_file_name = tempfile.original_filename
      geometry            = Paperclip::Geometry.from_file(tempfile)
      self.width          = geometry.width.to_i
      self.height         = geometry.height.to_i
    end
  end

  #region soft_validation
  # Check md5 fingerprint against existing fingerprints
  def sv_duplicate_image?
    if has_duplicate?
      soft_validations.add(:image_file_fingerprint, 'This image is a duplicate of an image already stored.')
    end
  end

  #endregion  soft_validation
end

#image_file_file_name The filename after processing to remove special characters.(Thefilenameafterprocessingtoremovespecialcharacters.) ⇒ Object

Image - class the represents an image stored in the filesystem.

This class relies on the paperclip gem and the ImageMagik app to link, store and manipulate images.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'app/models/image.rb', line 20

class Image < ActiveRecord::Base
  include Housekeeping::Users
  include Shared::Identifiable
  include Shared::Notable
  include Shared::Taggable
  include SoftValidation

  #constants
  MISSING_IMAGE_PATH = '/public/images/missing.jpg'

  before_save :extract_tw_attributes

  has_attached_file :image_file,
       :styles => {:medium => '300x300>', :thumb => '100x100>'},
       :default_url => MISSING_IMAGE_PATH,
       :filename_cleaner => CleanseFilename
       #:restricted_characters => /[^A-Za-z0-9\.]/,
  validates_attachment_content_type :image_file, :content_type => /\Aimage\/.*\Z/
  validates_attachment_presence :image_file
  validates_attachment_size :image_file, greater_than: 1.kilobytes

  soft_validate(:sv_duplicate_image?)

  def has_duplicate?
    Image.exists?(:image_file_fingerprint => self.image_file_fingerprint)
  end

  def duplicate_images
    Image.where(:image_file_fingerprint => self.image_file_fingerprint).to_a
  end

  def exif
=begin
    if self.new_record?
      return false
    end
=end
=begin
bash shell extraction notes:
identify -format "%[EXIF:*]" ExifImage.jpg
a=`identify -format "%[EXIF:*]" ExifImage.jpg`

a.split("\n").collect{|b| b.gsub("exif:", "").split("=")}.inject({}){|hsh, c| hsh.merge(c[0]=>c[1])}

    # drop to the shell
=end
    # run an imageMagick command (identify?) pass to that command self.image_file.url

  end

  protected

  def extract_tw_attributes
    # NOTE: assumes content type is an image.
    tempfile = image_file.queued_for_write[:original]
    if tempfile.nil?
      self.width          = 0
      self.height         = 0
      self.user_file_name = ''
      #TODO should an error be thrown here?
    else
      self.user_file_name = tempfile.original_filename
      geometry            = Paperclip::Geometry.from_file(tempfile)
      self.width          = geometry.width.to_i
      self.height         = geometry.height.to_i
    end
  end

  #region soft_validation
  # Check md5 fingerprint against existing fingerprints
  def sv_duplicate_image?
    if has_duplicate?
      soft_validations.add(:image_file_fingerprint, 'This image is a duplicate of an image already stored.')
    end
  end

  #endregion  soft_validation
end

#image_file_file_sizeObject

Image - class the represents an image stored in the filesystem.

This class relies on the paperclip gem and the ImageMagik app to link, store and manipulate images.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'app/models/image.rb', line 20

class Image < ActiveRecord::Base
  include Housekeeping::Users
  include Shared::Identifiable
  include Shared::Notable
  include Shared::Taggable
  include SoftValidation

  #constants
  MISSING_IMAGE_PATH = '/public/images/missing.jpg'

  before_save :extract_tw_attributes

  has_attached_file :image_file,
       :styles => {:medium => '300x300>', :thumb => '100x100>'},
       :default_url => MISSING_IMAGE_PATH,
       :filename_cleaner => CleanseFilename
       #:restricted_characters => /[^A-Za-z0-9\.]/,
  validates_attachment_content_type :image_file, :content_type => /\Aimage\/.*\Z/
  validates_attachment_presence :image_file
  validates_attachment_size :image_file, greater_than: 1.kilobytes

  soft_validate(:sv_duplicate_image?)

  def has_duplicate?
    Image.exists?(:image_file_fingerprint => self.image_file_fingerprint)
  end

  def duplicate_images
    Image.where(:image_file_fingerprint => self.image_file_fingerprint).to_a
  end

  def exif
=begin
    if self.new_record?
      return false
    end
=end
=begin
bash shell extraction notes:
identify -format "%[EXIF:*]" ExifImage.jpg
a=`identify -format "%[EXIF:*]" ExifImage.jpg`

a.split("\n").collect{|b| b.gsub("exif:", "").split("=")}.inject({}){|hsh, c| hsh.merge(c[0]=>c[1])}

    # drop to the shell
=end
    # run an imageMagick command (identify?) pass to that command self.image_file.url

  end

  protected

  def extract_tw_attributes
    # NOTE: assumes content type is an image.
    tempfile = image_file.queued_for_write[:original]
    if tempfile.nil?
      self.width          = 0
      self.height         = 0
      self.user_file_name = ''
      #TODO should an error be thrown here?
    else
      self.user_file_name = tempfile.original_filename
      geometry            = Paperclip::Geometry.from_file(tempfile)
      self.width          = geometry.width.to_i
      self.height         = geometry.height.to_i
    end
  end

  #region soft_validation
  # Check md5 fingerprint against existing fingerprints
  def sv_duplicate_image?
    if has_duplicate?
      soft_validations.add(:image_file_fingerprint, 'This image is a duplicate of an image already stored.')
    end
  end

  #endregion  soft_validation
end

#image_file_fingerprint MD5 for the image file(MD5) ⇒ Object

Image - class the represents an image stored in the filesystem.

This class relies on the paperclip gem and the ImageMagik app to link, store and manipulate images.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'app/models/image.rb', line 20

class Image < ActiveRecord::Base
  include Housekeeping::Users
  include Shared::Identifiable
  include Shared::Notable
  include Shared::Taggable
  include SoftValidation

  #constants
  MISSING_IMAGE_PATH = '/public/images/missing.jpg'

  before_save :extract_tw_attributes

  has_attached_file :image_file,
       :styles => {:medium => '300x300>', :thumb => '100x100>'},
       :default_url => MISSING_IMAGE_PATH,
       :filename_cleaner => CleanseFilename
       #:restricted_characters => /[^A-Za-z0-9\.]/,
  validates_attachment_content_type :image_file, :content_type => /\Aimage\/.*\Z/
  validates_attachment_presence :image_file
  validates_attachment_size :image_file, greater_than: 1.kilobytes

  soft_validate(:sv_duplicate_image?)

  def has_duplicate?
    Image.exists?(:image_file_fingerprint => self.image_file_fingerprint)
  end

  def duplicate_images
    Image.where(:image_file_fingerprint => self.image_file_fingerprint).to_a
  end

  def exif
=begin
    if self.new_record?
      return false
    end
=end
=begin
bash shell extraction notes:
identify -format "%[EXIF:*]" ExifImage.jpg
a=`identify -format "%[EXIF:*]" ExifImage.jpg`

a.split("\n").collect{|b| b.gsub("exif:", "").split("=")}.inject({}){|hsh, c| hsh.merge(c[0]=>c[1])}

    # drop to the shell
=end
    # run an imageMagick command (identify?) pass to that command self.image_file.url

  end

  protected

  def extract_tw_attributes
    # NOTE: assumes content type is an image.
    tempfile = image_file.queued_for_write[:original]
    if tempfile.nil?
      self.width          = 0
      self.height         = 0
      self.user_file_name = ''
      #TODO should an error be thrown here?
    else
      self.user_file_name = tempfile.original_filename
      geometry            = Paperclip::Geometry.from_file(tempfile)
      self.width          = geometry.width.to_i
      self.height         = geometry.height.to_i
    end
  end

  #region soft_validation
  # Check md5 fingerprint against existing fingerprints
  def sv_duplicate_image?
    if has_duplicate?
      soft_validations.add(:image_file_fingerprint, 'This image is a duplicate of an image already stored.')
    end
  end

  #endregion  soft_validation
end

#image_file_updated_atObject

Image - class the represents an image stored in the filesystem.

This class relies on the paperclip gem and the ImageMagik app to link, store and manipulate images.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'app/models/image.rb', line 20

class Image < ActiveRecord::Base
  include Housekeeping::Users
  include Shared::Identifiable
  include Shared::Notable
  include Shared::Taggable
  include SoftValidation

  #constants
  MISSING_IMAGE_PATH = '/public/images/missing.jpg'

  before_save :extract_tw_attributes

  has_attached_file :image_file,
       :styles => {:medium => '300x300>', :thumb => '100x100>'},
       :default_url => MISSING_IMAGE_PATH,
       :filename_cleaner => CleanseFilename
       #:restricted_characters => /[^A-Za-z0-9\.]/,
  validates_attachment_content_type :image_file, :content_type => /\Aimage\/.*\Z/
  validates_attachment_presence :image_file
  validates_attachment_size :image_file, greater_than: 1.kilobytes

  soft_validate(:sv_duplicate_image?)

  def has_duplicate?
    Image.exists?(:image_file_fingerprint => self.image_file_fingerprint)
  end

  def duplicate_images
    Image.where(:image_file_fingerprint => self.image_file_fingerprint).to_a
  end

  def exif
=begin
    if self.new_record?
      return false
    end
=end
=begin
bash shell extraction notes:
identify -format "%[EXIF:*]" ExifImage.jpg
a=`identify -format "%[EXIF:*]" ExifImage.jpg`

a.split("\n").collect{|b| b.gsub("exif:", "").split("=")}.inject({}){|hsh, c| hsh.merge(c[0]=>c[1])}

    # drop to the shell
=end
    # run an imageMagick command (identify?) pass to that command self.image_file.url

  end

  protected

  def extract_tw_attributes
    # NOTE: assumes content type is an image.
    tempfile = image_file.queued_for_write[:original]
    if tempfile.nil?
      self.width          = 0
      self.height         = 0
      self.user_file_name = ''
      #TODO should an error be thrown here?
    else
      self.user_file_name = tempfile.original_filename
      geometry            = Paperclip::Geometry.from_file(tempfile)
      self.width          = geometry.width.to_i
      self.height         = geometry.height.to_i
    end
  end

  #region soft_validation
  # Check md5 fingerprint against existing fingerprints
  def sv_duplicate_image?
    if has_duplicate?
      soft_validations.add(:image_file_fingerprint, 'This image is a duplicate of an image already stored.')
    end
  end

  #endregion  soft_validation
end

#user_file_name The name of the file as uploaded by the user.(Thenameofthefileasuploadedbytheuser.) ⇒ Object

Image - class the represents an image stored in the filesystem.

This class relies on the paperclip gem and the ImageMagik app to link, store and manipulate images.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'app/models/image.rb', line 20

class Image < ActiveRecord::Base
  include Housekeeping::Users
  include Shared::Identifiable
  include Shared::Notable
  include Shared::Taggable
  include SoftValidation

  #constants
  MISSING_IMAGE_PATH = '/public/images/missing.jpg'

  before_save :extract_tw_attributes

  has_attached_file :image_file,
       :styles => {:medium => '300x300>', :thumb => '100x100>'},
       :default_url => MISSING_IMAGE_PATH,
       :filename_cleaner => CleanseFilename
       #:restricted_characters => /[^A-Za-z0-9\.]/,
  validates_attachment_content_type :image_file, :content_type => /\Aimage\/.*\Z/
  validates_attachment_presence :image_file
  validates_attachment_size :image_file, greater_than: 1.kilobytes

  soft_validate(:sv_duplicate_image?)

  def has_duplicate?
    Image.exists?(:image_file_fingerprint => self.image_file_fingerprint)
  end

  def duplicate_images
    Image.where(:image_file_fingerprint => self.image_file_fingerprint).to_a
  end

  def exif
=begin
    if self.new_record?
      return false
    end
=end
=begin
bash shell extraction notes:
identify -format "%[EXIF:*]" ExifImage.jpg
a=`identify -format "%[EXIF:*]" ExifImage.jpg`

a.split("\n").collect{|b| b.gsub("exif:", "").split("=")}.inject({}){|hsh, c| hsh.merge(c[0]=>c[1])}

    # drop to the shell
=end
    # run an imageMagick command (identify?) pass to that command self.image_file.url

  end

  protected

  def extract_tw_attributes
    # NOTE: assumes content type is an image.
    tempfile = image_file.queued_for_write[:original]
    if tempfile.nil?
      self.width          = 0
      self.height         = 0
      self.user_file_name = ''
      #TODO should an error be thrown here?
    else
      self.user_file_name = tempfile.original_filename
      geometry            = Paperclip::Geometry.from_file(tempfile)
      self.width          = geometry.width.to_i
      self.height         = geometry.height.to_i
    end
  end

  #region soft_validation
  # Check md5 fingerprint against existing fingerprints
  def sv_duplicate_image?
    if has_duplicate?
      soft_validations.add(:image_file_fingerprint, 'This image is a duplicate of an image already stored.')
    end
  end

  #endregion  soft_validation
end

#widthObject

Image - class the represents an image stored in the filesystem.

This class relies on the paperclip gem and the ImageMagik app to link, store and manipulate images.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'app/models/image.rb', line 20

class Image < ActiveRecord::Base
  include Housekeeping::Users
  include Shared::Identifiable
  include Shared::Notable
  include Shared::Taggable
  include SoftValidation

  #constants
  MISSING_IMAGE_PATH = '/public/images/missing.jpg'

  before_save :extract_tw_attributes

  has_attached_file :image_file,
       :styles => {:medium => '300x300>', :thumb => '100x100>'},
       :default_url => MISSING_IMAGE_PATH,
       :filename_cleaner => CleanseFilename
       #:restricted_characters => /[^A-Za-z0-9\.]/,
  validates_attachment_content_type :image_file, :content_type => /\Aimage\/.*\Z/
  validates_attachment_presence :image_file
  validates_attachment_size :image_file, greater_than: 1.kilobytes

  soft_validate(:sv_duplicate_image?)

  def has_duplicate?
    Image.exists?(:image_file_fingerprint => self.image_file_fingerprint)
  end

  def duplicate_images
    Image.where(:image_file_fingerprint => self.image_file_fingerprint).to_a
  end

  def exif
=begin
    if self.new_record?
      return false
    end
=end
=begin
bash shell extraction notes:
identify -format "%[EXIF:*]" ExifImage.jpg
a=`identify -format "%[EXIF:*]" ExifImage.jpg`

a.split("\n").collect{|b| b.gsub("exif:", "").split("=")}.inject({}){|hsh, c| hsh.merge(c[0]=>c[1])}

    # drop to the shell
=end
    # run an imageMagick command (identify?) pass to that command self.image_file.url

  end

  protected

  def extract_tw_attributes
    # NOTE: assumes content type is an image.
    tempfile = image_file.queued_for_write[:original]
    if tempfile.nil?
      self.width          = 0
      self.height         = 0
      self.user_file_name = ''
      #TODO should an error be thrown here?
    else
      self.user_file_name = tempfile.original_filename
      geometry            = Paperclip::Geometry.from_file(tempfile)
      self.width          = geometry.width.to_i
      self.height         = geometry.height.to_i
    end
  end

  #region soft_validation
  # Check md5 fingerprint against existing fingerprints
  def sv_duplicate_image?
    if has_duplicate?
      soft_validations.add(:image_file_fingerprint, 'This image is a duplicate of an image already stored.')
    end
  end

  #endregion  soft_validation
end

Instance Method Details

#duplicate_imagesObject



47
48
49
# File 'app/models/image.rb', line 47

def duplicate_images
  Image.where(:image_file_fingerprint => self.image_file_fingerprint).to_a
end

#exifObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'app/models/image.rb', line 51

def exif
=begin
  if self.new_record?
    return false
  end
=end
=begin
bash shell extraction notes:
identify -format "%[EXIF:*]" ExifImage.jpg
a=`identify -format "%[EXIF:*]" ExifImage.jpg`

a.split("\n").collect{|b| b.gsub("exif:", "").split("=")}.inject({}){|hsh, c| hsh.merge(c[0]=>c[1])}

  # drop to the shell
=end
  # run an imageMagick command (identify?) pass to that command self.image_file.url

end

#has_duplicate?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'app/models/image.rb', line 43

def has_duplicate?
  Image.exists?(:image_file_fingerprint => self.image_file_fingerprint)
end