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

Ignore Synology-generated files #2553

Merged
merged 6 commits into from
Aug 21, 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/jobs/model_scan_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def file_list(model_path, library, include_all_subfolders: false)
pattern
)
end
library.list_files(glob).uniq
library.list_files(glob)
end

def perform(model_id, include_all_subfolders: false)
Expand Down
6 changes: 1 addition & 5 deletions app/jobs/process_uploaded_file_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def unzip(model, uploaded_file)
Archive::Reader.open_filename(archive.path, strip_components: strip) do |reader|
reader.each_entry do |entry|
next if !entry.file? || entry.size > SiteSettings.max_file_extract_size
next if hidden?(entry.pathname)
next if SiteSettings.ignored_file?(entry.pathname)
Dir.chdir(tmpdir) do
reader.extract(entry, Archive::EXTRACT_SECURE)
model.model_files.create(filename: entry.pathname, attachment: File.open(entry.pathname))
Expand All @@ -54,10 +54,6 @@ def unzip(model, uploaded_file)
end
end

def hidden?(pathname)
(File.split(pathname) - ["."]).any? { |x| x.starts_with? "." }
end

def count_common_path_components(archive)
# Generate full list of directories in the archive
paths = []
Expand Down
4 changes: 3 additions & 1 deletion app/models/library.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def self.register_all_storage
end

def list_files(pattern, flags = 0)
case storage_service
files = case storage_service
when "filesystem"
Dir.glob(pattern, flags, base: Shellwords.escape(path)).filter { |x| File.file?(File.join(path, x)) }
when "s3"
Expand All @@ -129,6 +129,8 @@ def list_files(pattern, flags = 0)
else
raise "Invalid storage service: #{storage_service}"
end
# Filter out files that should be ignored
files.uniq.reject { |str| SiteSettings.ignored_file?(str) }
end

def has_file?(path)
Expand Down
11 changes: 11 additions & 0 deletions app/models/site_settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,17 @@ def self.federation_enabled?
Rails.application.config.manyfold_features[:federation]
end

def self.ignored_file?(pathname)
@@patterns ||= [
/^\.[^\.]+/, # Hidden files starting with .
/.*\/@eaDir\/.*/, # Synology temp files
/__MACOSX/ # MACOS resource forks
]
(File.split(pathname) - ["."]).any? do |path_component|
@@patterns.any? { |pattern| path_component =~ pattern }
end
end

module UserDefaults
RENDERER = {
grid_width: 200,
Expand Down
22 changes: 0 additions & 22 deletions spec/jobs/process_uploaded_file_job_spec.rb
Original file line number Diff line number Diff line change
@@ -1,28 +1,6 @@
require "rails_helper"

RSpec.describe ProcessUploadedFileJob do
context "when detecting hidden files" do
it "detects normal files" do
expect(described_class.new.send(:hidden?, "test.stl")).to be false
end

it "detects normal files in subfolders" do
expect(described_class.new.send(:hidden?, "test/test.stl")).to be false
end

it "detects hidden files" do
expect(described_class.new.send(:hidden?, ".test.stl")).to be true
end

it "detects hidden files in subfolders" do
expect(described_class.new.send(:hidden?, "test/.test.stl")).to be true
end

it "detects normal files in hidden subfolders" do
expect(described_class.new.send(:hidden?, ".test/test.stl")).to be true
end
end

context "when counting common path prefixes" do
it "returns zero if there are no directories at all" do
expect(described_class.new.send(:count_common_elements, [])).to eq 0
Expand Down
26 changes: 26 additions & 0 deletions spec/models/site_settings_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require "rails_helper"

RSpec.describe SiteSettings do
context "when detecting ignored files" do
%w[
test.stl
test/test.stl
].each do |pathname|
it "accepts `#{pathname}`" do
expect(described_class.send(:ignored_file?, pathname)).to be false
end
end

%w[
.test.stl
test/.test.stl
.test/test.stl
model/@eaDir/test.png/SYNOPHOTO_THUMB_S.png
model/__MACOSX
].each do |pathname|
it "ignores `#{pathname}`" do
expect(described_class.send(:ignored_file?, pathname)).to be true
end
end
end
end
Loading