Class: MailEngine::HtmlDocumentAssetsReplacer

Inherits:
Object
  • Object
show all
Defined in:
lib/mail_engine/html_document_assets_replacer.rb

Class Method Summary collapse

Class Method Details

.check!(file_paths) ⇒ Object

One requirement: Need one html file in the zip



7
8
9
10
11
12
# File 'lib/mail_engine/html_document_assets_replacer.rb', line 7

def check! file_paths
  html_file_pathes = file_paths.select { |path| path =~ /\/[^_\/]+\.htm(l)?$/i }
  raise "No html was passed in." if html_file_pathes.blank?
  raise "Please only include one html file in the zip file." if html_file_pathes.size > 1
  html_file_pathes.first
end

.process(mail_template, file_paths, hostname) ⇒ Object

MailEngine::HtmlDocumentAssetsReplacer.process(mail_template, file_paths)



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
# File 'lib/mail_engine/html_document_assets_replacer.rb', line 15

def process mail_template, file_paths, hostname
  raise "Host should not include http://" if hostname =~ /^http:\/\//i

  # check if there is a html file
  html_file_path = check! file_paths

  # persist files into databases.
  other_file_paths = file_paths - Array.wrap(html_file_path)
  stored_file_objects = other_file_paths.map do |path|
    f = File.open(path)
    mail_template.mail_template_files.create :file => f, :size => File.size(f)
  end

  ### replace images and csses from the html document.
  # possible will use this in the future to take care of encoding problem:
  # doc = Nokogiri.XML('<foo><bar/><foo>', nil, 'EUC-JP')
  doc = Nokogiri::HTML(File.read(html_file_path))

  doc.css('img, link').each do |element|
    # FIXME seems will have the problem if have duplicated filename in different dir
    case
    when element.name == "link" && substitution_object = stored_file_objects.detect { |file_object| file_object.attributes["file"] == File.basename(element['href']) }
      element['href'] = "http://#{File.join(hostname, substitution_object.file.url)}"
    when element.name == "img" && substitution_object = stored_file_objects.detect { |file_object| file_object.attributes["file"] == File.basename(element['src']) }
      element['src'] = "http://#{File.join(hostname, substitution_object.file.url)}"
    end
  end

  doc.to_html
end

.replace_resource_in_html(html, original_filename, new_filename, resource_type) ⇒ Object

replace the old filename in html by new filename



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/mail_engine/html_document_assets_replacer.rb', line 47

def replace_resource_in_html html, original_filename, new_filename, resource_type
  # FIXME better to raise an error, return an unprocessed file maybe not good.
  return html if original_filename.blank? or new_filename.blank?
  doc = Nokogiri::HTML(html)
  doc.css('img, link').each do |element|
    # FIXME seems will have the problem if have duplicated filename in different dir
    next if element.name == "link" && element['href'].blank?
    next if element.name == "img" && element['src'].blank?

    case
    when element.name == "link" && get_resource_url(element['href'], resource_type) == original_filename
      element['href'] = element['href'].sub(original_filename, new_filename)
    when element.name == "img" && get_resource_url(element['src'], resource_type) == original_filename
      element['src'] = element['src'].sub(original_filename, new_filename)
    end
  end

  doc.to_html
end