Skip to content

Commit b4c2769

Browse files
committed
Add memoization to SupportedMimeTypes to avoid repeated checks
1 parent 390a097 commit b4c2769

File tree

3 files changed

+9
-3
lines changed

3 files changed

+9
-3
lines changed

app/jobs/upgrade/backfill_image_derivatives.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def build_enumerator(cursor:)
1717
end
1818

1919
def each_iteration(modelfile)
20-
return unless modelfile.is_image? || modelfile.mime_type&.to_sym&.in?(SupportedMimeTypes.f3d_formats)
20+
return unless modelfile.is_image? || SupportedMimeTypes.can_render?(modelfile.mime_type)
2121
return if modelfile.extension.downcase == "svg"
2222
Rails.logger.info("Creating image derivatives for: #{modelfile.path_within_library}")
2323
modelfile.attachment_derivatives!

app/lib/supported_mime_types.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,17 @@ def can_export?(type)
7676
end
7777
memoize :can_export?
7878

79-
def f3d_formats
79+
def renderable
8080
readers = `f3d --list-readers`.lines
8181
types = readers.filter_map { |it| it.match(/\w[a-z]*\/[0-9a-z.+-]*\w/)&.to_s }
8282
types.filter_map { |it| Mime::Type.lookup(it).to_sym }.uniq
8383
end
84+
memoize :renderable
85+
86+
def can_render?(type)
87+
renderable.include? type&.to_sym
88+
end
89+
memoize :can_render?
8490

8591
def indexable_types
8692
image_types + model_types + video_types + document_types + archive_types

app/uploaders/application_uploader.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ def generate_location(io, record: nil, derivative: nil, metadata: {}, **)
100100
carousel: magick.resize_to_limit!(1024, 768)
101101
}
102102
end
103-
elsif context[:record].mime_type.to_sym.in? SupportedMimeTypes.f3d_formats
103+
elsif SupportedMimeTypes.can_render?(context[:record].mime_type)
104104
f3d_opts = {
105105
output: "-",
106106
resolution: "512,512",

0 commit comments

Comments
 (0)