Skip to content

Commit

Permalink
AO3-6625 Replace whitespace in download filenames with underscores (#…
Browse files Browse the repository at this point in the history
…4645)

* AO3-6625 Don't include whitespace in download filenames

* AO3-6625 Remove redundant assignment and prefer zero? to == 0

* AO3-6625 Test more spacing and use more succinct code
  • Loading branch information
sarken authored Oct 29, 2023
1 parent 06a0fc4 commit 86b4fed
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 20 deletions.
9 changes: 6 additions & 3 deletions app/models/download.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,12 @@ def file_type_from_mime(mime)
end
end

# The base name of the file (eg, "War and Peace")
# The base name of the file (e.g., "War_and_Peace")
def file_name
name = clean(work.title)
name += " Work #{work.id}" if name.length < 3
# If the file name is 1-2 characters, append "_Work_#{work.id}".
# If the file name is blank, name the file "Work_#{work.id}".
name = [name, "Work_#{work.id}"].compact_blank.join("_") if name.length < 3
name.strip
end

Expand Down Expand Up @@ -125,6 +127,7 @@ def chapters
# squash spaces
# strip all non-alphanumeric
# truncate to 24 chars at a word boundary
# replace whitespace with underscore for bug with epub table of contents on Kindle (AO3-6625)
def clean(string)
# get rid of any HTML entities to avoid things like "amp" showing up in titles
string = string.gsub(/\&(\w+)\;/, '')
Expand All @@ -133,6 +136,6 @@ def clean(string)
string = string.gsub(/ +/, " ")
string = string.strip
string = string.truncate(24, separator: ' ', omission: '')
string
string.gsub(/\s/, "_")
end
end
10 changes: 6 additions & 4 deletions spec/mailers/user_mailer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1110,10 +1110,11 @@
end

it "has the correct attachments" do
filename = work.title.gsub(/\s/, "_")
expect(email.attachments.length).to eq(2)
expect(email.attachments).to contain_exactly(
an_object_having_attributes(filename: "#{work.title}.html"),
an_object_having_attributes(filename: "#{work.title}.txt")
an_object_having_attributes(filename: "#{filename}.html"),
an_object_having_attributes(filename: "#{filename}.txt")
)
end

Expand Down Expand Up @@ -1162,10 +1163,11 @@
end

it "has the correct attachments" do
filename = work.title.gsub(/\s/, "_")
expect(email.attachments.length).to eq(2)
expect(email.attachments).to contain_exactly(
an_object_having_attributes(filename: "#{work.title}.html"),
an_object_having_attributes(filename: "#{work.title}.txt")
an_object_having_attributes(filename: "#{filename}.html"),
an_object_having_attributes(filename: "#{filename}.txt")
)
end

Expand Down
74 changes: 65 additions & 9 deletions spec/models/download_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,36 +13,92 @@

# Arabic
work.title = "هذا عمل جديد"
expect(Download.new(work).file_name).to eq("hdh ml jdyd")
expect(Download.new(work).file_name).to eq("hdh_ml_jdyd")

# Chinese
work.title = "我哥好像被奇怪的人盯上了怎么破"
expect(Download.new(work).file_name).to eq("Wo Ge Hao Xiang Bei Qi")
expect(Download.new(work).file_name).to eq("Wo_Ge_Hao_Xiang_Bei_Qi")

# Japanese
work.title = "二重スパイは接点を持つ"
expect(Download.new(work).file_name).to eq("Er Zhong supaihaJie Dian")
expect(Download.new(work).file_name).to eq("Er_Zhong_supaihaJie_Dian")

# Hebrew
work.title = "לחזור הביתה"
expect(Download.new(work).file_name).to eq("lkhzvr hbyth")
expect(Download.new(work).file_name).to eq("lkhzvr_hbyth")
end

it "removes HTML entities and emojis" do
work.title = "Two of Hearts <3 &amp; >.< &"
expect(Download.new(work).file_name).to eq("Two of Hearts 3")
expect(Download.new(work).file_name).to eq("Two_of_Hearts_3")

work.title = "Emjoi 🤩 Yay 🥳"
expect(Download.new(work).file_name).to eq("Emjoi Yay")
expect(Download.new(work).file_name).to eq("Emjoi_Yay")
end

it "strips leading space" do
work.title = " Blank Space Baby"
expect(Download.new(work).file_name).to eq("Blank_Space_Baby")
end

it "strips trailing space" do
work.title = "Write your name: "
expect(Download.new(work).file_name).to eq("Write_your_name")
end

it "replaces multiple spaces with single underscore" do
work.title = "Space Opera"
expect(Download.new(work).file_name).to eq("Space_Opera")
end

it "replaces unicode space with underscores" do
work.title = "No-break Space"
expect(Download.new(work).file_name).to eq("No-break_Space")

work.title = "En Quad Space"
expect(Download.new(work).file_name).to eq("En_Quad_Space")

work.title = "Em Quad Space"
expect(Download.new(work).file_name).to eq("Em_Quad_Space")

work.title = "En Space"
expect(Download.new(work).file_name).to eq("En_Space")

work.title = "Em Space"
expect(Download.new(work).file_name).to eq("Em_Space")

work.title = "3 Per Em Space"
expect(Download.new(work).file_name).to eq("3_Per_Em_Space")

work.title = "4 Per Em Space"
expect(Download.new(work).file_name).to eq("4_Per_Em_Space")

work.title = "6 Per Em Space"
expect(Download.new(work).file_name).to eq("6_Per_Em_Space")

work.title = "Figure Space"
expect(Download.new(work).file_name).to eq("Figure_Space")

work.title = "Punctuation Space"
expect(Download.new(work).file_name).to eq("Punctuation_Space")

work.title = "Thin Space"
expect(Download.new(work).file_name).to eq("Thin_Space")

work.title = "Hair Space"
expect(Download.new(work).file_name).to eq("Hair_Space")

work.title = "Narrow No-Break Space"
expect(Download.new(work).file_name).to eq("Narrow_No-Break_Space")
end

it "appends work ID if too short" do
work.id = 999_999
work.title = "Uh"
expect(Download.new(work).file_name).to eq("Uh Work 999999")
expect(Download.new(work).file_name).to eq("Uh_Work_999999")

work.title = ""
expect(Download.new(work).file_name).to eq("Work 999999")
expect(Download.new(work).file_name).to eq("Work_999999")

work.title = "wat"
expect(Download.new(work).file_name).to eq("wat")
Expand All @@ -53,7 +109,7 @@
expect(Download.new(work).file_name).to eq("123456789-123456789-1234")

work.title = "123456789 123456789 123456789"
expect(Download.new(work).file_name).to eq("123456789 123456789")
expect(Download.new(work).file_name).to eq("123456789_123456789")
end
end

Expand Down
10 changes: 6 additions & 4 deletions spec/support/shared_examples/mailer_shared_examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,18 @@

shared_examples "an email with a deleted work with draft chapters attached" do
it "has html and txt attachments" do
filename = work.title.gsub(/\s/, "_")
expect(email.attachments.length).to eq(2)
expect(email.attachments).to contain_exactly(
an_object_having_attributes(filename: "#{work.title}.html"),
an_object_having_attributes(filename: "#{work.title}.txt")
an_object_having_attributes(filename: "#{filename}.html"),
an_object_having_attributes(filename: "#{filename}.txt")
)
end

it "includes draft chapters in attachments" do
html_attachment = email.attachments["#{work.title}.html"].body.raw_source
txt_attachment = email.attachments["#{work.title}.txt"].body.raw_source
filename = work.title.gsub(/\s/, "_")
html_attachment = email.attachments["#{filename}.html"].body.raw_source
txt_attachment = email.attachments["#{filename}.txt"].body.raw_source
decoded_html_content = Base64.decode64(html_attachment)
decoded_txt_content = Base64.decode64(txt_attachment)

Expand Down

0 comments on commit 86b4fed

Please sign in to comment.