Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Previews check content type #4544

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions app/controllers/csv_preview_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,16 @@ def show
parent_document_uri = @asset["parent_document_url"]
parent_document_path = URI(parent_document_uri).request_uri
@content_item = GdsApi.content_store.content_item(parent_document_path).to_hash
@attachment_metadata = @content_item.dig("details", "attachments").select do |attachment|
@attachment_metadata = @content_item.dig("details", "attachments").find do |attachment|
attachment["filename"] == asset_filename
end

if @attachment_metadata.empty?
if @attachment_metadata.nil?
redirect_to(parent_document_uri, status: :see_other, allow_other_host: true) and return
end

return cacheable_404 if @attachment_metadata["content_type"] != "text/csv"

@csv_rows, @truncated = CsvPreviewService
.new(GdsApi.asset_manager.media(params[:id], params[:filename]).body)
.csv_rows
Expand Down
8 changes: 4 additions & 4 deletions app/views/csv_preview/malformed_csv.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
add_view_stylesheet("csv_preview")
%>

<% content_for :title, "#{@attachment_metadata.first["title"]} - GOV.UK" %>
<% content_for :title, "#{@attachment_metadata["title"]} - GOV.UK" %>

<main id="content" role="main" class="govuk-main-wrapper">
<div class="govuk-grid-row">
Expand Down Expand Up @@ -35,7 +35,7 @@
<%= render 'govuk_publishing_components/components/inverse_header', {} do %>
<%= render "govuk_publishing_components/components/title", {
context: I18n.t("csv_preview.document_type.#{@content_item['document_type']}", count: 1),
title: @attachment_metadata.first["title"],
title: @attachment_metadata["title"],
font_size: "xl",
inverse: true,
margin_bottom: 8,
Expand All @@ -44,7 +44,7 @@
<p class="govuk-body csv-preview__updated">
Updated <%= I18n.l(Time.zone.parse(@content_item["public_updated_at"]), format: "%-d %B %Y") %>
<br>
<%= link_to("<strong>Download CSV</strong> #{number_to_human_size(@attachment_metadata.first['file_size'])}".html_safe, @attachment_metadata.first['url'], class: "csv-preview__download-link") %>
<%= link_to("<strong>Download CSV</strong> #{number_to_human_size(@attachment_metadata['file_size'])}".html_safe, @attachment_metadata['url'], class: "csv-preview__download-link") %>
</p>
<% end %>
</div>
Expand All @@ -57,7 +57,7 @@
<p class="govuk-body">
This CSV cannot be viewed online.
<br>
You can <%= link_to("download the file", @attachment_metadata.first['url'], class: "govuk-link") %> to open it with your own software.
You can <%= link_to("download the file", @attachment_metadata['url'], class: "govuk-link") %> to open it with your own software.
</p>
</div>
</div>
Expand Down
8 changes: 4 additions & 4 deletions app/views/csv_preview/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
add_view_stylesheet("csv_preview")
%>

<% content_for :title, "#{@attachment_metadata.first["title"]} - GOV.UK" %>
<% content_for :title, "#{@attachment_metadata["title"]} - GOV.UK" %>

<main id="content" role="main" class="govuk-main-wrapper">
<div class="govuk-grid-row">
Expand Down Expand Up @@ -35,7 +35,7 @@
<%= render 'govuk_publishing_components/components/inverse_header', {} do %>
<%= render "govuk_publishing_components/components/title", {
context: I18n.t("csv_preview.document_type.#{@content_item['document_type']}", count: 1),
title: @attachment_metadata.first["title"],
title: @attachment_metadata["title"],
font_size: "xl",
inverse: true,
margin_bottom: 8,
Expand All @@ -44,7 +44,7 @@
<p class="govuk-body csv-preview__updated">
Updated <%= I18n.l(Time.zone.parse(@content_item["public_updated_at"]), format: "%-d %B %Y") %>
<br>
<%= link_to("<strong>Download CSV</strong> #{number_to_human_size(@attachment_metadata.first['file_size'])}".html_safe, @attachment_metadata.first['url'], class: "csv-preview__download-link") %>
<%= link_to("<strong>Download CSV</strong> #{number_to_human_size(@attachment_metadata['file_size'])}".html_safe, @attachment_metadata['url'], class: "csv-preview__download-link") %>
</p>
<% end %>
</div>
Expand All @@ -56,7 +56,7 @@
} do %>
<p class="govuk-body">
This preview shows the first 1,000 rows and 50 columns.
<%= link_to("Download CSV #{number_to_human_size(@attachment_metadata.first['file_size'])}", @attachment_metadata.first['url'], class: "govuk-link") %>
<%= link_to("Download CSV #{number_to_human_size(@attachment_metadata['file_size'])}", @attachment_metadata['url'], class: "govuk-link") %>
</p>
<% end %>
<% end %>
Expand Down
13 changes: 13 additions & 0 deletions spec/requests/csv_preview_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,19 @@
end
end

context "when the file type of the attachment is not text/csv" do
before do
setup_asset_manager(parent_document_url, asset_manager_id, asset_manager_filename)
setup_content_item(path_from_filename(asset_manager_filename), parent_document_base_path, content_type: "application/pdf")
end

it "redirects to parent" do
get "/#{path_from_filename(asset_manager_filename)}/preview"

expect(response).to have_http_status(:not_found)
end
end

def path_from_filename(base_name)
"media/#{asset_manager_id}/#{base_name}.csv"
end
Expand Down
6 changes: 3 additions & 3 deletions spec/support/asset_manager_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@ def setup_asset_manager(parent_document_url, asset_manager_id, filename = nil, m
stub_request(:get, "#{Plek.find('asset-manager')}/media/#{asset_manager_id}/#{filename}.csv").to_return(body: csv_file, status: media_code)
end

def setup_content_item(non_legacy_url_path, parent_document_base_path)
def setup_content_item(non_legacy_url_path, parent_document_base_path, content_type: "text/csv")
filename = non_legacy_url_path.split("/").last
content_item = {
base_path: parent_document_base_path,
document_type: "guidance",
public_updated_at: "2023-05-27T08:00:07.000+00:00",
details: {
attachments: [
{ title: "Attachment 1", filename: "file.csv", url: "https://www.gov.uk/media/5678/file.csv", file_size: "1024" },
{ title: "Attachment 2", filename:, url: "https://www.gov.uk/#{non_legacy_url_path}", file_size: "2048" },
{ title: "Attachment 1", content_type: "text/csv", filename: "file.csv", url: "https://www.gov.uk/media/5678/file.csv", file_size: "1024" },
{ title: "Attachment 2", content_type:, filename:, url: "https://www.gov.uk/#{non_legacy_url_path}", file_size: "2048" },
],
},
links: {
Expand Down
Loading