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

Load file list on the background on the Show page #713

Merged
merged 5 commits into from
Nov 15, 2024
Merged
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
20 changes: 20 additions & 0 deletions app/controllers/catalog_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,15 @@ def show
end
end

# This endpoint is used to feed the AJAX call on the Show page for the file list and
# therefore the return JSON must be something that DataTables can use.
def file_list
document = solr_find(params["id"])
file_list = { data: document.files }

render json: file_list.to_json
end

# Returns the raw BibTex citation information
def bibtex
_unused, @document = search_service.fetch(params[:id])
Expand Down Expand Up @@ -339,4 +348,15 @@ def pppl_reporting_feed
format.json { render json: @documents }
end
end

private

def solr_find(id)
solr_url = Blacklight.default_configuration.connection_config[:url]
solr = RSolr.connect(url: solr_url)
solr_params = { q: "id:#{id}", fl: '*' }
response = solr.get('select', params: solr_params)
solr_doc = response["response"]["docs"][0]
SolrDocument.new(solr_doc)
end
end
4 changes: 3 additions & 1 deletion app/models/dataset_file.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

class DatasetFile
attr_accessor :name, :description, :format, :size, :mime_type, :sequence, :handle, :extension,
attr_accessor :name, :description, :format, :size, :display_size, :mime_type, :sequence, :handle, :extension,
:source, :download_url, :full_path

def self.from_hash(data, data_source)
Expand All @@ -23,6 +23,7 @@ def self.from_hash_dataspace(data)
file.description = hash[:description]
file.mime_type = hash[:mime_type]
file.size = hash[:size]
file.display_size = ActiveSupport::NumberHelper.number_to_human_size(file.size)
file.sequence = (hash[:sequence] || "").to_i
# Technically the handle is a property of the dataset item rather than the file (aka bitstream)
# but we store it at the file level for convenience.
Expand All @@ -40,6 +41,7 @@ def self.from_hash_describe(data)
file.extension = File.extname(file.name)
file.extension = file.extension[1..] if file.extension != "." # drop the leading period
file.size = hash[:size]
file.display_size = ActiveSupport::NumberHelper.number_to_human_size(file.size)
file.download_url = hash[:url]
file
end
Expand Down
78 changes: 42 additions & 36 deletions app/views/catalog/_show_documents.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -13,42 +13,11 @@
<table id="files-table" class="table">
<thead>
<tr>
<th scope="col" nowrap="nowrap"><span>#</span></th>
<th scope="col" nowrap="nowrap"><span>Filename</span></th>
<% if @document.data_source == "dataspace" %>
<th scope="col"><span>Description</span></th>
<% end %>
<th scope="col" nowrap="nowrap"><span>Filesize</span></th>
<th>Filename</th>
<th>Size</th>
</tr>
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We ditched the columns with the sequence number and the description for each file since we don't show those in PDC Describe. (Plus we don't have file description in PDC Describe, this was for DataSpace records but we are moving away from that)

</thead>
<tbody>
<% DatasetFile.sort_file_array(@document.files).each_with_index do |file, ix| %>
<tr class="document-download">
<th scope="row">
<span><span><%= ix + 1 %></span></span>
</th>
<td>
<span>
<i class="bi bi-file-arrow-down"></i>
<% if @render_links%>
<a href="<%= file.download_url %>" class="documents-file-link" target="_blank" title="<%= file.full_path %>"><%= truncate(file.full_path, length: 60) %></a>
<% else %>
<%= truncate(file.full_path, length: 60) %>
<% end %>
</span>
</td>
<% if @document.data_source == "dataspace" %>
<td>
<span><%= file.description %></span>
</td>
<% end %>
<td data-order="<%= file.size %>">
<span><span><%= number_to_human_size(file.size) %></span></span>
</td>
</tr>
<% end %>
</tbody>
<tfoot></tfoot>
<tbody></tbody>
</table>
<% end %>

Expand All @@ -63,8 +32,45 @@
<script type="text/javascript">
$(function() {

// Wire DataTable for the file list.
$('#files-table').DataTable();
$('#files-table').DataTable({
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ajax: "<%= catalog_file_list_path(@document) %>",
columns: [
{ data: 'full_path' },
{ data: 'size' }
],
columnDefs: [
{
// filename
render: function (data, type, row) {
if (type == "display") {
html = `<a href="${row.download_url}">${data}</a>`;
return html;
}

// Force any readme file to sort to the top
var sortValue;
if (data.toLowerCase().includes("readme")) {
sortValue = "A" + data;
} else {
sortValue = "Z" + data;
}
return sortValue;
},
targets: 0,
},
{
// file size
render: function (data, type, row) {
if (type == "display") {
return row.display_size;
}
return parseInt(data, 10);
},
targets: 1,
className: 'dt-right'
}
]
});

// Track file downloads via Plausible.
$(".documents-file-link").click(function() {
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
get 'pppl_reporting_feed' => 'catalog#pppl_reporting_feed', as: :pppl_reporting_feed

get 'catalog/:id/bibtex' => 'catalog#bibtex', as: :catalog_bibtex
get 'catalog/:id/file-list' => 'catalog#file_list', as: :catalog_file_list

resource :catalog, only: [:index], as: 'catalog', path: '/catalog', controller: 'catalog' do
concerns :searchable
Expand Down
9 changes: 6 additions & 3 deletions spec/system/show_pdc_describe_record_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,19 @@
end
it 'has the README files first' do
visit '/catalog/doi-10-34770-bm4s-t361'
sleep(0.1) # wait for the files to load via AJAX
first_filename_spot = find(:css, '#files-table>tbody>tr:first-child>td', match: :first).text
expect(first_filename_spot).to eq("Fig11b_readme.hdf")
end

it "reports sizes using MB and KB" do
visit '/catalog/doi-10-34770-bm4s-t361'
sleep(0.1) # wait for the files to load via AJAX
# These tests are to validate our monkey-patched number_to_human_size (see number_to_human_size_converter.rb)
# is dividing the size by 1000 instead of using the Rails default of 1024.
file_size = find(:css, '#files-table>tbody>tr:first-child>td:nth-child(3)', match: :first).text
file_size = find(:css, '#files-table>tbody>tr:first-child>td:nth-child(2)', match: :first).text
expect(file_size).to eq("22 KB")
file_size = find(:css, '#files-table>tbody>tr:nth-child(6)>td:nth-child(3)', match: :first).text
file_size = find(:css, '#files-table>tbody>tr:nth-child(6)>td:nth-child(2)', match: :first).text
expect(file_size).to eq("32.7 MB")
end

Expand All @@ -51,9 +53,10 @@
end
it 'correctly sorts by file size' do
visit '/catalog/doi-10-34770-bm4s-t361'
sleep(0.1) # wait for the files to load via AJAX
first_filename_spot = find(:css, '#files-table>tbody>tr:first-child>td', match: :first).text
expect(first_filename_spot).to eq("Fig11b_readme.hdf")
find(:xpath, "//thead/tr/th[3]").click
find(:xpath, "//thead/tr/th[2]").click # sort by file size
first_filename_spot = find(:css, '#files-table>tbody>tr:first-child>td', match: :first).text
# "readme.txt" is the smallest file and so now it is first
expect(first_filename_spot).to eq("readme.txt")
Expand Down