Class: Goldencobra::Upload

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/goldencobra/upload.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#crop_hObject

Returns the value of attribute crop_h.



28
29
30
# File 'app/models/goldencobra/upload.rb', line 28

def crop_h
  @crop_h
end

#crop_imageObject

Returns the value of attribute crop_image.



28
29
30
# File 'app/models/goldencobra/upload.rb', line 28

def crop_image
  @crop_image
end

#crop_wObject

Returns the value of attribute crop_w.



28
29
30
# File 'app/models/goldencobra/upload.rb', line 28

def crop_w
  @crop_w
end

#crop_xObject

Returns the value of attribute crop_x.



28
29
30
# File 'app/models/goldencobra/upload.rb', line 28

def crop_x
  @crop_x
end

#crop_yObject

Returns the value of attribute crop_y.



28
29
30
# File 'app/models/goldencobra/upload.rb', line 28

def crop_y
  @crop_y
end

#image_urlObject

Returns the value of attribute image_url.



28
29
30
# File 'app/models/goldencobra/upload.rb', line 28

def image_url
  @image_url
end

Class Method Details

.default_positionObject



139
140
141
142
# File 'app/models/goldencobra/upload.rb', line 139

def self.default_position
  Goldencobra::Setting.for_key("goldencobra.article.image_positions").
  to_s.split(",").map(&:strip).first
end

Instance Method Details

#complete_list_nameObject



94
95
96
97
98
99
# File 'app/models/goldencobra/upload.rb', line 94

def complete_list_name
  result = ""
  result += "#{self.image_file_name} " if self.image_file_name.present?
  result += "(#{self.source}, #{self.rights}) " if self.source.present? || self.rights.present?
  result + "- #{self.updated_at.strftime("%d.%m.%Y - %H:%M Uhr")}"
end

#crop_image_with_coordsObject



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'app/models/goldencobra/upload.rb', line 70

def crop_image_with_coords
  require "RMagick"
  # Should we crop?
  if self.crop_image.present? && self.crop_image == "1" && self.crop_x.present? &&
    self.crop_y.present? && self.crop_w.present? && self.crop_h.present?

    scaled_img = Magick::ImageList.new(self.image.path(:large))
    orig_img = Magick::ImageList.new(self.image.path(:original))
    scale = orig_img.columns.to_f / scaled_img.columns

    args = [ self.crop_x.to_i, self.crop_y.to_i, self.crop_w.to_i, self.crop_h.to_i ]
    args = args.collect { |a| a.to_i * scale }

    orig_img.crop!(*args)
    orig_img.write(self.image.path(:original))
    self.crop_image = false
    self.image.reprocess!
  end
end

#image_file?Boolean

Internal: Makes sure to only post-process files which are either pdf or image files. Word documents would break file upload.

No params

Returns true for image or pdf files and false for everything else

Returns:

  • (Boolean)


131
132
133
134
135
136
137
# File 'app/models/goldencobra/upload.rb', line 131

def image_file?
  if !(self.image_content_type =~ /^image.*/).nil? || !(self.image_content_type =~ /pdf/).nil?
    true
  else
    false
  end
end

#titleObject



90
91
92
# File 'app/models/goldencobra/upload.rb', line 90

def title
  "#{self.image_file_name} (#{self.image_content_type})"
end

#unzip_filesObject



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'app/models/goldencobra/upload.rb', line 101

def unzip_files
  if self.image_file_name.include?(".zip") && File.exists?(self.image.path)
    require "zip"
    zipped_files = Zip::File.open(self.image.path)
    zipped_files.each do |zipped_file|
      filename = zipped_file.name.split("/").last.gsub(" ", "_").gsub(/[^0-9A-z.\-]/, "")
      file_path = "tmp/#{self.id}_#{filename}"
      next if filename[0] == "."
      if zipped_file.file?
        zipped_file.extract(file_path)
        Goldencobra::Upload.create(
          image: File.open(file_path),
          image_file_name: filename,
          source: self.source,
          rights: self.rights,
          description: self.description,
          tag_list: self.tag_list.join(", ")
        )
        File.delete(file_path)
      end
    end
  end
end