Class: OxTenderAbstract::ArchiveProcessor

Inherits:
Object
  • Object
show all
Includes:
ContextualLogger
Defined in:
lib/oxtenderabstract/archive_processor.rb

Overview

Archive processor for downloading and extracting archive files

Constant Summary collapse

MAX_FILE_SIZE_BYTES =

100 MB in bytes

100 * 1024 * 1024
MAX_RETRY_ATTEMPTS =
3
RETRY_DELAY_SECONDS =
2

Instance Method Summary collapse

Methods included from ContextualLogger

included, #log_debug, #log_error, #log_fatal, #log_info, #log_warn, #logger

Constructor Details

#initializeArchiveProcessor



19
20
21
# File 'lib/oxtenderabstract/archive_processor.rb', line 19

def initialize
  # Archive processor initialization
end

Instance Method Details

#download_and_extract(archive_url) ⇒ Object

Download and extract archive data



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
# File 'lib/oxtenderabstract/archive_processor.rb', line 24

def download_and_extract(archive_url)
  return Result.failure('Empty archive URL') if archive_url.nil? || archive_url.empty?

  begin
    # Download archive to memory with retry logic
    download_result = download_with_retry(archive_url)
    return download_result if download_result.failure?

    content = download_result.data[:content]

    # Determine archive format by first bytes
    first_bytes = content[0..1].unpack1('H*')

    if first_bytes == '1f8b'
      # This is GZIP archive - decompress GZIP, then ZIP
      gunzip_result = decompress_gzip(content)
      return gunzip_result if gunzip_result.failure?

      zip_result = extract_zip_from_memory(gunzip_result.data[:content])

      Result.success({
                       files: zip_result,
                       total_size: download_result.data[:size],
                       compressed_size: gunzip_result.data[:compressed_size],
                       file_count: zip_result.size
                     })
    elsif content[0..1] == 'PK'
      # This is already ZIP archive - parse directly
      zip_result = extract_zip_from_memory(content)

      Result.success({
                       files: zip_result,
                       total_size: download_result.data[:size],
                       compressed_size: nil,
                       file_count: zip_result.size
                     })
    else
      # Log first bytes for debugging
      log_error "Unknown archive format. First 10 bytes: #{content[0..9].unpack1('H*')}"
      Result.failure('Unknown archive format (not GZIP and not ZIP)')
    end
  rescue StandardError => e
    log_error "Archive processing error: #{e.message}"
    log_error e.backtrace.first(3).join("\n") if e.backtrace
    Result.failure("Archive processing error: #{e.message}")
  end
end