Skip to content

Commit 0233197

Browse files
committed
Marcel breakage not recognising XML mime type correctly, forced workaround: metanorma/metanorma#473
1 parent 541b6aa commit 0233197

2 files changed

Lines changed: 13 additions & 5 deletions

File tree

lib/html2doc/mime.rb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,16 @@ def mime_attachment(boundary, _filename, item, dir)
4141
end
4242

4343
def mime_type(item)
44-
type = Marcel::MimeType.for Pathname.new(item) ||
45-
'text/plain; charset="utf-8"'
44+
abs_path = File.absolute_path(item)
45+
type = Marcel::MimeType.for(Pathname.new(abs_path))
46+
47+
# Marcel sometimes fails to detect XML files and returns application/octet-stream
48+
# Override for .xml files when detection fails
49+
if type == "application/octet-stream" && File.extname(abs_path).downcase == ".xml"
50+
type = "application/xml"
51+
end
52+
53+
type ||= 'text/plain; charset="utf-8"'
4654
type = %(#{type} charset="utf-8") if /^text/.match?(type)
4755
type
4856
end

spec/html2doc_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def word_body(xml, footnote)
8282
Content-ID: <filelist.xml>
8383
Content-Disposition: inline; filename="filelist.xml"
8484
Content-Transfer-Encoding: base64
85-
Content-Type: #{Html2Doc.new({}).mime_type('filelist.xml')}
85+
Content-Type: application/xml
8686
8787
PHhtbCB4bWxuczpvPSJ1cm46c2NoZW1hcy1taWNyb3NvZnQtY29tOm9mZmljZTpvZmZpY2UiPgog
8888
ICAgICAgIDxvOk1haW5GaWxlIEhSZWY9Ii4uL3Rlc3QuaHRtIi8+ICA8bzpGaWxlIEhSZWY9ImZp
@@ -96,7 +96,7 @@ def word_body(xml, footnote)
9696
Content-ID: <filelist.xml>
9797
Content-Disposition: inline; filename="filelist.xml"
9898
Content-Transfer-Encoding: base64
99-
Content-Type: #{Html2Doc.new({}).mime_type('filelist.xml')}
99+
Content-Type: application/xml
100100
PHhtbCB4bWxuczpvPSJ1cm46c2NoZW1hcy1taWNyb3NvZnQtY29tOm9mZmljZTpvZmZpY2UiPgog
101101
ICAgICAgIDxvOk1haW5GaWxlIEhSZWY9Ii4uL3Rlc3QuaHRtIi8+ICA8bzpGaWxlIEhSZWY9ImZp
102102
bGVsaXN0LnhtbCIvPgogIDxvOkZpbGUgSFJlZj0iaGVhZGVyLmh0bWwiLz4KPC94bWw+Cg==
@@ -108,7 +108,7 @@ def word_body(xml, footnote)
108108
Content-ID: <filelist.xml>
109109
Content-Disposition: inline; filename="filelist.xml"
110110
Content-Transfer-Encoding: base64
111-
Content-Type: #{Html2Doc.new({}).mime_type('filelist.xml')}
111+
Content-Type: application/xml
112112
113113
PHhtbCB4bWxuczpvPSJ1cm46c2NoZW1hcy1taWNyb3NvZnQtY29tOm9mZmljZTpvZmZpY2UiPgog
114114
ICAgICAgIDxvOk1haW5GaWxlIEhSZWY9Ii4uL3Rlc3QuaHRtIi8+ICA8bzpGaWxlIEhSZWY9IjFh

0 commit comments

Comments
 (0)