Class: Services::MdsContent

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/services/mds_content.rb

Constant Summary collapse

BASE_EXTRACTION_PATH =
Rails.root.join('tmp').to_path

Instance Method Summary collapse

Instance Method Details

#content_file_nameObject



50
51
52
# File 'app/models/services/mds_content.rb', line 50

def content_file_name
  @content_file_name ||= "composite_file_#{id}.zip"
end

#content_pathObject



25
26
27
# File 'app/models/services/mds_content.rb', line 25

def content_path
  @content_path ||= File.join(BASE_EXTRACTION_PATH, content_file_name)
end

#each_entry(&block) ⇒ Object



9
10
11
# File 'app/models/services/mds_content.rb', line 9

def each_entry(&block)
  each_zipped_file('**/*', &block)
end

#each_zipped_file(glob_pattern) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
# File 'app/models/services/mds_content.rb', line 13

def each_zipped_file(glob_pattern)
  write_content_to_file
  extract_zip_contents
  Dir.glob(File.join(extracted_files_path, glob_pattern), File::FNM_CASEFOLD).sort.each do |zip_file_path|
    yield zip_file_path, zip_file_path[extracted_files_path.length + 1..-1]
  end

ensure
  remove_content_file
  remove_extracted_files
end

#extract_zip_contentsObject



33
34
35
36
# File 'app/models/services/mds_content.rb', line 33

def extract_zip_contents
  FileUtils.mkdir_p(extracted_files_path) unless File.exists?(extracted_files_path)
  RecursiveUnzipper.new(content_path).extract_to(extracted_files_path)
end

#extracted_files_pathObject



29
30
31
# File 'app/models/services/mds_content.rb', line 29

def extracted_files_path
  @extracted_files_path ||= File.join(BASE_EXTRACTION_PATH, "extracted_files_#{id}")
end

#remove_content_fileObject



42
43
44
# File 'app/models/services/mds_content.rb', line 42

def remove_content_file
  File.unlink(content_path) if File.exists?(content_path)
end

#remove_extracted_filesObject



46
47
48
# File 'app/models/services/mds_content.rb', line 46

def remove_extracted_files
  FileUtils.rm_rf(extracted_files_path) if File.exists?(extracted_files_path)
end

#write_content_to_fileObject



38
39
40
# File 'app/models/services/mds_content.rb', line 38

def write_content_to_file
  File.open(content_path, "wb") { |f| f.write(content) }
end