Class: Analysis::FileConversionJob

Inherits:
ApplicationJob
  • Object
show all
Defined in:
app/jobs/analysis/file_conversion_job.rb

Instance Method Summary collapse

Instance Method Details

#perform(file_id, output_format) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'app/jobs/analysis/file_conversion_job.rb', line 14

def perform(file_id, output_format)
  # Get model
  file = ModelFile.find(file_id)

  # Can we convert this format?
  raise UnsupportedFormatError unless file.convertable?(to: output_format)
  extension = Mime::EXTENSION_LOOKUP.select { |k, v| v.symbol == output_format }.keys.last

  status[:step] = "jobs.analysis.file_conversion.loading_mesh" # i18n-tasks-use t('jobs.analysis.file_conversion.loading_mesh')
  scene = file.scene

  # Manifold check for 3MF files
  # raise NonManifoldError.new if output_format == :threemf && !file.manifold?

  status[:step] = "jobs.analysis.file_conversion.exporting" # i18n-tasks-use t('jobs.analysis.file_conversion.exporting')
  new_file = ModelFile.new(
    model: file.model,
    filename: Pathname.new(file.filename).sub_ext(".#{extension}"),
    presupported: file.presupported,
    y_up: file.y_up,
    previewable: file.previewable,
    caption: file.caption,
    notes: file.notes
  )
  dedup = 0
  while new_file.exists_on_storage?
    dedup += 1
    new_file.filename = Pathname.new(file.filename).sub_ext("-#{dedup}.#{extension}")
  end
  # Save the new file into the Shrine cache, and attach
  Tempfile.create("", ModelFileUploader.find_storage(:cache).directory) do |outfile|
    scene.export(extension, outfile.path)
    new_file.attachment = ModelFileUploader.uploaded_file(
      storage: :cache,
      id: File.basename(outfile.path),
      metadata: {
        filename: new_file.filename,
        size: File.size(outfile.path)
      }
    )
  end
  # Set relationship between new file and old
  new_file.relationships << Relationship.new(subject: new_file, objekt: file, predicate: "alternative_format_of")
  # Store record in database
  new_file.save
  # Queue up file scan
  new_file.analyse_later
  # Update the UI
  file.broadcast_refresh
rescue NonManifoldError
  # Log non-manifold error as a problem, and absorb error so we don't retry
  Problem.create_or_clear(
    file,
    :non_manifold,
    true
  )
ensure
  if file
    # Mark inefficient problem resolution as no longer in progress, if it's set
    file.problems.where(category: :inefficient, in_progress: true).find_each do
      it.update(in_progress: false)
    end
  end
end