Class: PresentationObserver

Inherits:
ActiveRecord::Observer
  • Object
show all
Defined in:
app/models/presentation_observer.rb

Instance Method Summary collapse

Instance Method Details

#after_destroy(presentation) ⇒ Object



42
43
44
45
46
# File 'app/models/presentation_observer.rb', line 42

def after_destroy(presentation)
  Rails.logger.info "Executing PresentationObserver after_destroy hook (#{__FILE__}:#{__LINE__})"
  Paperclip.run("rm", "-rf #{File.dirname presentation.unzipped_location}")
  Rails.logger.info "Cleaned up all unzipped files from #{presentation.unzipped_location}"
end

#after_save(presentation) ⇒ Object



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
# File 'app/models/presentation_observer.rb', line 6

def after_save(presentation)
  Rails.logger.info "Executing PresentationObserver after_save hook (#{__FILE__}:#{__LINE__})"

  # location of existing compressed (zipped) file
  compressed_file = presentation.contents.path

  # Converts /.../path/original/something.zip -> /.../path/original/unpacked/
  destination_path = File.join(File.dirname(presentation.contents.path),
                               Presentation::UNPACKED_DIRNAME)

  if File.exists?(destination_path)
    Paperclip.run("rm", "-rf #{destination_path}")
  end

  Dir.mktmpdir {|tmpdir_path|
    Rails.logger.info "Temporarily unpacking presentation into #{tmpdir_path}"
    Paperclip.run("unzip", "#{compressed_file} -d #{tmpdir_path}")

    index_container = Find.find(tmpdir_path) do |file|
      break File.dirname(file) if file =~ /index\.html/
    end

    if index_container.blank?
      Rails.logger.warn "No index.html file found in uploaded zipfile for presentation with ID of #{presentation.id}! Skipping."
    else
      # move index-containing directory to one named
      # Presentation::UNPACKED_DIRNAME, regardless of original name/structure
      Rails.logger.info "Moving #{index_container} to #{destination_path}"
      FileUtils.move index_container, destination_path
      # Update the #unzipped_location without executing callbacks. A bit hacky
      Presentation.update_all({:unzipped_location => destination_path}, {:id => presentation.id})
    end
    Rails.logger.info "Uncompressing uploaded presentation completed successfully."
  }
end