Move file glob methods into their own module

This commit is contained in:
James Smith committed 2026-03-12 15:06:21 +00:00
1 parent f2c090818d
commit dd5ce8bfa2
5 files changed
+50 -49

No files matched your search

-42
View File
@@ -26,46 +26,4 @@ class ApplicationJob < ActiveJob::Base
step: nil
)
end
def self.extension_glob(extensions)
[
"*.{",
extensions.map { |ext| case_insensitive_glob_string(ext) }.join(","),
"}"
].join
end
def self.case_insensitive_glob_string(str)
str.chars.map { |char|
"[#{char.upcase}#{char.downcase}]"
}.join
end
def self.image_pattern
extension_glob(SupportedMimeTypes.image_extensions)
end
def self.file_pattern
extension_glob(SupportedMimeTypes.indexable_extensions)
end
def self.common_subfolders
{
"3mf" => file_pattern,
"fdm" => file_pattern,
"files" => file_pattern,
"images" => image_pattern,
"lychee" => file_pattern,
"lys" => file_pattern,
"model" => file_pattern,
"obj" => file_pattern,
"parts" => file_pattern,
"presupported" => file_pattern,
"resin" => file_pattern,
"stl" => file_pattern,
"sup" => file_pattern,
"supported" => file_pattern,
"unsupported" => file_pattern
}
end
end
@@ -4,7 +4,7 @@ class Scan::Library::DetectFilesystemChangesJob < ApplicationJob
# Find all files in the library that we might need to look at
def filenames_on_disk(library)
library.list_files(File.join("**", ApplicationJob.file_pattern))
library.list_files(File.join("**", FileMatcher.file_pattern))
end
# Get a list of all the existing filenames
@@ -13,7 +13,7 @@ class Scan::Library::DetectFilesystemChangesJob < ApplicationJob
end
def filter_out_common_subfolders(folders)
matcher = /\/(#{ApplicationJob.common_subfolders.keys.join("|")})$/i
matcher = /\/(#{FileMatcher.common_subfolders.keys.join("|")})$/i
folders.map { |f| f.gsub(matcher, "") }.uniq
end
+4 -4
View File
@@ -6,12 +6,12 @@ class Scan::Model::AddNewFilesJob < ApplicationJob
def file_list(model_path, library, include_all_subfolders: false)
glob = include_all_subfolders ?
[File.join(Shellwords.escape(model_path), "**", ApplicationJob.file_pattern)] :
[File.join(Shellwords.escape(model_path), ApplicationJob.file_pattern)] +
ApplicationJob.common_subfolders.map do |name, pattern|
[File.join(Shellwords.escape(model_path), "**", FileMatcher.file_pattern)] :
[File.join(Shellwords.escape(model_path), FileMatcher.file_pattern)] +
FileMatcher.common_subfolders.map do |name, pattern|
File.join(
Shellwords.escape(model_path),
ApplicationJob.case_insensitive_glob_string(name),
FileMatcher.case_insensitive_glob_string(name),
pattern
)
end
+43
View File
@@ -0,0 +1,43 @@
module FileMatcher
def self.extension_glob(extensions)
[
"*.{",
extensions.map { |ext| case_insensitive_glob_string(ext) }.join(","),
"}"
].join
end
def self.case_insensitive_glob_string(str)
str.chars.map { |char|
"[#{char.upcase}#{char.downcase}]"
}.join
end
def self.image_pattern
extension_glob(SupportedMimeTypes.image_extensions)
end
def self.file_pattern
extension_glob(SupportedMimeTypes.indexable_extensions)
end
def self.common_subfolders
{
"3mf" => file_pattern,
"fdm" => file_pattern,
"files" => file_pattern,
"images" => image_pattern,
"lychee" => file_pattern,
"lys" => file_pattern,
"model" => file_pattern,
"obj" => file_pattern,
"parts" => file_pattern,
"presupported" => file_pattern,
"resin" => file_pattern,
"stl" => file_pattern,
"sup" => file_pattern,
"supported" => file_pattern,
"unsupported" => file_pattern
}
end
end
@@ -1,6 +1,6 @@
require "rails_helper"
RSpec.describe ApplicationJob do
RSpec.describe FileMatcher do
it "generates a case-insensitive pattern for all supported files" do # rubocop:todo RSpec/MultipleExpectations
pattern = described_class.file_pattern
expect(pattern).to include "[Ss][Tt][Ll],"