Skip to content

Commit bfaf101

Browse files
committed
Make file conversion job support more formats
It can now handle all assimp-loadable and exportable formats!
1 parent a711711 commit bfaf101

File tree

2 files changed

+34
-34
lines changed

2 files changed

+34
-34
lines changed

app/jobs/analysis/file_conversion_job.rb

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -14,43 +14,43 @@ class Analysis::FileConversionJob < ApplicationJob
1414
def perform(file_id, output_format)
1515
# Get model
1616
file = ModelFile.find(file_id)
17-
# Can we output this format?
18-
raise UnsupportedFormatError unless SupportedMimeTypes.can_export?(output_format) || !file.loadable?
19-
extension = nil
17+
18+
# Can we convert this format?
19+
raise UnsupportedFormatError if !SupportedMimeTypes.can_export?(output_format) || !file.loadable?
20+
extension = Mime::EXTENSION_LOOKUP.select { |k, v| v.symbol == output_format }.keys.last
21+
2022
status[:step] = "jobs.analysis.file_conversion.loading_mesh" # i18n-tasks-use t('jobs.analysis.file_conversion.loading_mesh')
21-
case output_format
22-
when :threemf
23-
# raise NonManifoldError.new if !file.manifold?
24-
extension = "3mf"
23+
scene = file.scene
24+
25+
# Manifold check for 3MF files
26+
# raise NonManifoldError.new if output_format == :threemf && !file.manifold?
27+
28+
status[:step] = "jobs.analysis.file_conversion.exporting" # i18n-tasks-use t('jobs.analysis.file_conversion.exporting')
29+
new_file = ModelFile.new(
30+
model: file.model,
31+
filename: file.filename.gsub(".#{file.extension}", ".#{extension}")
32+
)
33+
dedup = 0
34+
while new_file.exists_on_storage?
35+
dedup += 1
36+
new_file.filename = file.filename.gsub(".#{file.extension}", "-#{dedup}.#{extension}")
2537
end
26-
if extension
27-
status[:step] = "jobs.analysis.file_conversion.exporting" # i18n-tasks-use t('jobs.analysis.file_conversion.exporting')
28-
new_file = ModelFile.new(
29-
model: file.model,
30-
filename: file.filename.gsub(".#{file.extension}", ".#{extension}")
38+
# Save the new file into the Shrine cache, and attach
39+
Tempfile.create("", ModelFileUploader.find_storage(:cache).directory) do |outfile|
40+
scene.export(extension, outfile.path)
41+
new_file.attachment = ModelFileUploader.uploaded_file(
42+
storage: :cache,
43+
id: File.basename(outfile.path),
44+
metadata: {
45+
filename: new_file.filename,
46+
size: File.size(outfile.path)
47+
}
3148
)
32-
dedup = 0
33-
while new_file.exists_on_storage?
34-
dedup += 1
35-
new_file.filename = file.filename.gsub(".#{file.extension}", "-#{dedup}.#{extension}")
36-
end
37-
# Save the new file into the Shrine cache, and attach
38-
Tempfile.create("", ModelFileUploader.find_storage(:cache).directory) do |outfile|
39-
file.scene.export(extension, outfile.path)
40-
new_file.attachment = ModelFileUploader.uploaded_file(
41-
storage: :cache,
42-
id: File.basename(outfile.path),
43-
metadata: {
44-
filename: new_file.filename,
45-
size: File.size(outfile.path)
46-
}
47-
)
48-
end
49-
# Store record in database
50-
new_file.save
51-
# Queue up file scan
52-
new_file.analyse_later
5349
end
50+
# Store record in database
51+
new_file.save
52+
# Queue up file scan
53+
new_file.analyse_later
5454
rescue NonManifoldError
5555
# Log non-manifold error as a problem, and absorb error so we don't retry
5656
Problem.create_or_clear(

spec/jobs/analysis/file_conversion_job_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,6 @@
6161
end
6262

6363
it "does nothing with an invalid output format" do
64-
expect { described_class.perform_now(file.id, :ply) }.to raise_error(UnsupportedFormatError)
64+
expect { described_class.perform_now(file.id, :blend) }.to raise_error(UnsupportedFormatError)
6565
end
6666
end

0 commit comments

Comments
 (0)