Skip to content

Commit 4c27cc5

Browse files
authored
Merge pull request #218 from companieshouse/feature/get-content-type-sub-return-fix
Fix temporary implementation of get_content_tye sub
2 parents bd305f1 + dffcdcc commit 4c27cc5

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

lib/ChGovUk/Controllers/Company/FilingHistory.pm

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ sub view {
126126

127127
my $delay_end = $delay->begin(0);
128128
trace "Calling document API to retrieve content_type for possible zip filing [%s]", $doc->{links}->{document_metadata};
129-
$self->_get_content_type( $doc->{links}->{document_metadata}, $doc, $delay_end);
129+
$self->_get_content_type_application_zip( $doc->{links}->{document_metadata}, $doc, $delay_end);
130130
}
131131

132132
if ( $formatted_transaction_date >= $formatted_xhtml_available_date) {
@@ -202,12 +202,10 @@ sub view {
202202

203203
#-------------------------------------------------------------------------------
204204

205-
# _get_content_type calls the document API and retrieves a content_type for the provided filing.
206-
# Please note - this function grabs the first variable returned from the resources array and only uses it
207-
# in the template if it is application/zip (zip filing). Changes would be required if you wanted to use
208-
# other resource types returned in the template. If you want to return multiple changes would also be required to cater for this.
209-
# This future work has been documented in a ticket on JIRA: https://companieshouse.atlassian.net/browse/UK-155.
210-
sub _get_content_type {
205+
# _get_content_type_application_zip calls the document API and retrieves a resources array of content_types for the provided filing.
206+
# If a resources array is found, we will search the resources array for an 'application/zip' content_type and if found,
207+
# set it on the '$doc' element for use in the frontend template.
208+
sub _get_content_type_application_zip {
211209
my ( $self, $document_metadata_uri, $doc, $callback ) = @_;
212210

213211
$self->ch_api->document($document_metadata_uri)->metadata->get->on(
@@ -234,8 +232,13 @@ sub _get_content_type {
234232
my ($api, $tx) = @_;
235233
my $resources = $tx->res->json->{resources};
236234
if ($resources) {
237-
$doc->{content_type} = (keys $resources)[0];
238-
trace "Successfully retrieved content_type %s for %s", $doc->{content_type}, $document_metadata_uri;
235+
foreach my $content_type (keys $resources) {
236+
if ($content_type eq 'application/zip') {
237+
trace "ZIP filing found. Setting content_type on doc to application/zip [%s]", $document_metadata_uri;
238+
$doc->{content_type} = $content_type;
239+
last;
240+
}
241+
}
239242
} else {
240243
trace "No content_type found for %s. Setting content_type to unknown", $document_metadata_uri;
241244
$doc->{content_type} = 'unknown';

0 commit comments

Comments
 (0)