cirandas.net

ref: master

app/models/concerns/delayed_attachment_fu.rb


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
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
module DelayedAttachmentFu

  module ClassMethods
    def delay_attachment_fu_thumbnails
      include DelayedAttachmentFu::InstanceMethods
      after_create do |file|
        if file.thumbnailable?
          Delayed::Job.enqueue CreateThumbnailsJob.new(file.class.name, file.id)
        end
      end
    end
  end

  module InstanceMethods
    # skip processing with RMagick
    def process_attachment
    end

    def after_process_attachment
      save_to_storage
      @temp_paths.clear
      @saved_attachment = nil
      run_callbacks :after_attachment_saved
    end

    def create_thumbnails
      if thumbnailable?
        self.class.with_image(full_filename) do |img|
          self.width = img.columns
          self.height = img.rows
          self.save!
        end
        self.class.attachment_options[:thumbnails].each do |suffix, size|
          self.create_or_update_thumbnail(self.full_filename, suffix, size)
        end
        self.thumbnails_processed = true
        self.save!
      end
    end

    def public_filename(size=nil)
      force, size = true, nil if size == :uploaded
      if !self.thumbnailable? || self.thumbnails_processed || force
        super size
      else
        size ||= :thumb

        if NOOSFERO_CONF['delayed_attachment_fallback_original_image'] && self.full_filename
          self.full_filename.to_s.gsub %r(^#{Regexp.escape(base_path)}), ''
        else
          '/images/icons-app/image-loading-%s.png' % size
        end
      end
    end


  end
end