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

Index and display video and document files #2890

Merged
merged 9 commits into from
Oct 8, 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
2 changes: 1 addition & 1 deletion app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def configure_content_security_policy
content_security_policy.default_src :self
content_security_policy.connect_src :self
content_security_policy.frame_ancestors :self
content_security_policy.frame_src :none
content_security_policy.frame_src :self
content_security_policy.font_src :self, "https://cdn.jsdelivr.net"
content_security_policy.img_src :self, :data
content_security_policy.object_src :none
Expand Down
5 changes: 1 addition & 4 deletions app/controllers/model_files_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ def show
respond_to do |format|
format.html
format.js
format.any(*SupportedMimeTypes.model_types.map(&:to_sym)) do
send_file_content disposition: (params[:download] == "true") ? :attachment : :inline
end
format.any(*SupportedMimeTypes.image_types.map(&:to_sym)) do
format.any(*SupportedMimeTypes.indexable_types.map(&:to_sym)) do
send_file_content disposition: (params[:download] == "true") ? :attachment : :inline
end
end
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/models_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def show
files = files.where.not(id: hidden_ids)
end
files = files.includes(:presupported_version, :problems)
files = files.select(&:is_3d_model?)
files = files.reject(&:is_image?)
@groups = helpers.group(files)
render layout: "card_list_page"
end
Expand Down
2 changes: 1 addition & 1 deletion app/helpers/uploads_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def library_select_title(l)
end

def uploadable_file_extensions
SupportedMimeTypes.archive_extensions + SupportedMimeTypes.model_extensions + SupportedMimeTypes.image_extensions
SupportedMimeTypes.archive_extensions + SupportedMimeTypes.indexable_extensions
end

def input_accept_string
Expand Down
2 changes: 1 addition & 1 deletion app/jobs/application_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def self.image_pattern
end

def self.file_pattern
extension_glob(SupportedMimeTypes.image_extensions + SupportedMimeTypes.model_extensions)
extension_glob(SupportedMimeTypes.indexable_extensions)
end

def self.common_subfolders
Expand Down
2 changes: 1 addition & 1 deletion app/jobs/process_uploaded_file_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def perform(library_id, uploaded_file, owner: nil, creator_id: nil, collection_i
case File.extname(file.original_filename).delete(".").downcase
when *SupportedMimeTypes.archive_extensions
unzip(model, file)
when *(SupportedMimeTypes.model_extensions + SupportedMimeTypes.image_extensions)
when *SupportedMimeTypes.indexable_extensions
new_file = model.model_files.create(filename: file.original_filename, attachment: file)
else
Rails.logger.warn("Ignoring #{file.inspect}")
Expand Down
36 changes: 36 additions & 0 deletions app/lib/supported_mime_types.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,22 @@ def self.image_extensions
Mime::EXTENSION_LOOKUP.filter { |k, v| is_image_mime_type?(v) }.keys
end

def self.video_types
Mime::LOOKUP.filter { |k, v| is_video_mime_type?(v) }.values
end

def self.video_extensions
Mime::EXTENSION_LOOKUP.filter { |k, v| is_video_mime_type?(v) }.keys
end

def self.document_types
Mime::LOOKUP.filter { |k, v| is_document_mime_type?(v) }.values
end

def self.document_extensions
Mime::EXTENSION_LOOKUP.filter { |k, v| is_document_mime_type?(v) }.keys
end

def self.archive_types
Mime::LOOKUP.filter { |k, v| is_archive_mime_type?(v) }.values
end
Expand All @@ -27,13 +43,33 @@ def self.can_export?(type)
[:threemf].include? type
end

def self.indexable_types
image_types + model_types + video_types + document_types
end

def self.indexable_extensions
image_extensions + model_extensions + video_extensions + document_extensions
end

class << self
private

def is_image_mime_type?(type)
type.to_s.start_with?("image/")
end

def is_video_mime_type?(type)
type.to_s.start_with?("video/")
end

def is_document_mime_type?(type)
[
"application/pdf",
"text/markdown",
"text/plain"
].include?(type.to_s)
end

def is_model_mime_type?(type)
extras = [
"text/x-gcode",
Expand Down
8 changes: 8 additions & 0 deletions app/models/model_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ def is_image?
SupportedMimeTypes.image_extensions.include? extension
end

def is_video?
SupportedMimeTypes.video_extensions.include? extension
end

def is_document?
SupportedMimeTypes.document_extensions.include? extension
end

def is_3d_model?
SupportedMimeTypes.model_extensions.include? extension
end
Expand Down
2 changes: 1 addition & 1 deletion app/models/site_settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class SiteSettings < RailsSettings::Base
field :model_tags_filter_stop_words, type: :boolean, default: true
field :model_tags_tag_model_directory_name, type: :boolean, default: false
field :model_tags_stop_words_locale, type: :string, default: "en"
field :model_tags_custom_stop_words, type: :array, default: (SupportedMimeTypes.image_extensions + SupportedMimeTypes.model_extensions)
field :model_tags_custom_stop_words, type: :array, default: SupportedMimeTypes.indexable_extensions
field :model_tags_auto_tag_new, type: :string, default: "!new"
field :model_path_template, type: :string, default: "{tags}/{modelName}{modelId}"
field :parse_metadata_from_path, type: :boolean, default: true
Expand Down
4 changes: 4 additions & 0 deletions app/views/model_files/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
<div class="col-9">
<% if @file.is_image? %>
<%= image_tag model_model_file_path(@model, @file, format: @file.extension), alt: @file.name, style: "width: 100%" %>
<% elsif @file.is_video? %>
<%= video_tag model_model_file_path(@model, @file, format: @file.extension), alt: @file.name, style: "width: 100%", controls: true %>
<% elsif @file.is_document? %>
<%= tag.iframe src: model_model_file_path(@model, @file, format: @file.extension), alt: @file.name, style: "width: 100%; aspect-ratio: 0.707" %>
<% elsif renderable?(@file.extension) %>
<% if @file.presupported_version || @file.unsupported_version %>
<ul class="nav nav-tabs">
Expand Down
6 changes: 3 additions & 3 deletions spec/jobs/model_scan_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@
let(:thing) { create(:model, path: "thingiverse_model", library: library) }

it "scans files" do # rubocop:todo RSpec/MultipleExpectations
expect { described_class.perform_now(thing.id) }.to change { thing.model_files.count }.to(2)
expect(thing.model_files.map(&:filename)).to eq ["files/part_one.stl", "images/card_preview_DISPLAY.png"]
expect { described_class.perform_now(thing.id) }.to change { thing.model_files.count }.to(4)
expect(thing.model_files.map(&:filename)).to eq ["LICENSE.txt", "README.txt", "files/part_one.stl", "images/card_preview_DISPLAY.png"]
end

it "ignores model-type files in image directory" do # rubocop:todo RSpec/MultipleExpectations
expect { described_class.perform_now(thing.id) }.to change { thing.model_files.count }.to(2)
expect { described_class.perform_now(thing.id) }.to change { thing.model_files.count }.to(4)
expect(thing.model_files.map(&:filename)).not_to include "images/ignore.stl"
end
end
Expand Down
3 changes: 1 addition & 2 deletions spec/jobs/scan/detect_filesystem_changes_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@
around do |ex|
MockDirectory.create([
"thingiverse_model/files/part_one.stl",
"thingiverse_model/images/preview.stl",
"thingiverse_model/README.txt"
"thingiverse_model/images/preview.stl"
]) do |path|
@library_path = path
ex.run
Expand Down
15 changes: 15 additions & 0 deletions spec/lib/supported_mime_types_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,21 @@
expect(described_class.model_extensions).to include("lyt")
end

it "includes PDF files in document list" do # rubocop:todo RSpec/MultipleExpectations
expect(described_class.document_types.map(&:to_s)).to include("application/pdf")
expect(described_class.document_extensions).to include("pdf")
end

it "includes TXT files in document list" do # rubocop:todo RSpec/MultipleExpectations
expect(described_class.document_types.map(&:to_s)).to include("text/plain")
expect(described_class.document_extensions).to include("txt")
end

it "includes video files in video list" do # rubocop:todo RSpec/MultipleExpectations
expect(described_class.video_types.map(&:to_s)).to include("video/mp4")
expect(described_class.video_extensions).to include("mp4")
end

context "when listing non-standard model files" do
it "includes OpenSCAD" do # rubocop:todo RSpec/MultipleExpectations
expect(described_class.model_types).to include("application/x-openscad")
Expand Down
Loading