Skip to content

Commit 0ba00a6

Browse files
committed
Add file_size to document details for PDF downloads
This change includes the file_size field in the document details sent to the Publishing API, enabling the frontend to display human-readable file sizes for downloadable PDF maps.
1 parent 7f8a136 commit 0ba00a6

2 files changed

Lines changed: 43 additions & 1 deletion

File tree

app/presenters/edition_presenter.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,12 +147,14 @@ def image
147147

148148
def document
149149
@document ||= if edition.document
150-
{
150+
document_hash = {
151151
"attachment_type" => "file",
152152
"id" => SecureRandom.uuid,
153153
"url" => edition.document["file_url"],
154154
"content_type" => edition.document["content_type"],
155155
}
156+
document_hash["file_size"] = edition.document["size"] if edition.document["size"]
157+
document_hash
156158
end
157159
end
158160
end

spec/presenters/edition_presenter_spec.rb

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,5 +214,45 @@
214214
expect(presented_data["last_edited_by_editor_id"]).to eq(user.uid)
215215
end
216216
end
217+
218+
describe "document field" do
219+
let(:document_details) { presented_data["details"]["document"] }
220+
221+
context "when the edition has a document with size" do
222+
before do
223+
allow(edition).to receive(:document).and_return(
224+
"file_url" => "https://assets.example.com/map.pdf",
225+
"content_type" => "application/pdf",
226+
"size" => 201_672,
227+
)
228+
end
229+
230+
it "includes the file size in the document details" do
231+
expect(document_details).to include(
232+
"file_size" => 201_672,
233+
"url" => "https://assets.example.com/map.pdf",
234+
"content_type" => "application/pdf",
235+
"attachment_type" => "file",
236+
)
237+
end
238+
end
239+
240+
context "when the edition has a document without size" do
241+
before do
242+
allow(edition).to receive(:document).and_return(
243+
"file_url" => "https://assets.example.com/map.pdf",
244+
"content_type" => "application/pdf",
245+
)
246+
end
247+
248+
it "does not include the file size in the document details" do
249+
expect(document_details).to include(
250+
"url" => "https://assets.example.com/map.pdf",
251+
"content_type" => "application/pdf",
252+
)
253+
expect(document_details).not_to have_key("file_size")
254+
end
255+
end
256+
end
217257
end
218258
end

0 commit comments

Comments
 (0)