Class: Cabriolet::Extraction::FileExtractionWorker

Inherits:
Fractor::Worker
  • Object
show all
Defined in:
lib/cabriolet/extraction/file_extraction_worker.rb

Overview

Worker for extracting files using Fractor

Instance Method Summary collapse

Instance Method Details

#process(work) ⇒ Fractor::WorkResult

Process a file extraction work item

Parameters:

Returns:

  • (Fractor::WorkResult)

    Result of extraction



13
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
# File 'lib/cabriolet/extraction/file_extraction_worker.rb', line 13

def process(work)
  output_path = build_output_path(work)

  # Check if file exists and skip if not overwriting
  if ::File.exist?(output_path) && !work.overwrite
    return skipped_result(work, "File already exists")
  end

  # Create parent directory
  dir = ::File.dirname(output_path)
  FileUtils.mkdir_p(dir) unless ::File.directory?(dir)

  # Get file data
  data = work.file.data
  unless data
    return skipped_result(work, "No data available")
  end

  # Write file data
  ::File.binwrite(output_path, data)

  # Preserve file attributes if available
  preserve_file_attributes(output_path, work.file)

  # Return success result
  Fractor::WorkResult.new(
    result: {
      path: output_path,
      size: data.bytesize,
      name: work.file.name,
    },
    work: work,
  )
rescue StandardError => e
  # Return error result
  Fractor::WorkResult.new(
    error: {
      message: e.message,
      class: e.class.name,
      backtrace: e.backtrace.first(5),
    },
    work: work,
  )
end