Module: WolfCore::FileUtils

Extended by:
LoggingUtils
Defined in:
lib/wolf_core/utils/file_utils.rb

Overview

rubocop:disable Style/Documentation

Class Method Summary collapse

Methods included from LoggingUtils

log_object

Class Method Details

.bytes_from_url(url) ⇒ Object



70
71
72
73
74
75
# File 'lib/wolf_core/utils/file_utils.rb', line 70

def bytes_from_url(url)
  uri, response = download_file_from_url(url).values_at(:uri, :response)
  bytes = response.body
  filename = filename_from_response(response, uri)
  { bytes: bytes, filename: filename }
end

.delete_files(*args) ⇒ Object



35
36
37
38
39
40
41
42
43
44
# File 'lib/wolf_core/utils/file_utils.rb', line 35

def delete_files(*args)
  pattern = File.join(*args)
  files_to_delete = Dir.glob(pattern)
  files_to_delete.each do |file|
    File.delete(file)
    log_object "File deleted: #{file}"
  end

  log_object "File Deleting Process Finished! (#{files_to_delete.size} files deleted)"
end

.download_file_from_url(url) ⇒ Object



63
64
65
66
67
68
# File 'lib/wolf_core/utils/file_utils.rb', line 63

def download_file_from_url(url)
  uri = URI.parse(url)
  response = Net::HTTP.get_response(uri)
  raise_service_error({ message: "Failed to download file", url: url }) unless response.is_a?(Net::HTTPSuccess)
  { uri: uri, response: response }
end

.encode_file_to_base64_from_url(url) ⇒ Object

rubocop:disable Metrics/MethodLength



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/wolf_core/utils/file_utils.rb', line 46

def encode_file_to_base64_from_url(url) # rubocop:disable Metrics/MethodLength
  uri, response = download_file_from_url(url).values_at(:uri, :response)
  bytes = response.body

  encoded_file = Base64.strict_encode64(bytes)

  filename = filename_from_response(response, uri)
  log_object filename, title: "filename is"
  { filename: filename, encoded_file: encoded_file }
rescue StandardError => e
  raise_service_error({
                        message: "Failed to encode file url",
                        encode_file_error: { message: e.message, backtrace: e.backtrace },
                        url: url
                      })
end

.filename_from_response(response, uri) ⇒ Object



77
78
79
80
81
82
83
84
85
# File 'lib/wolf_core/utils/file_utils.rb', line 77

def filename_from_response(response, uri)
  content_disposition = response["content-disposition"]
  if content_disposition
    match = content_disposition.match(/filename="?([^"]+)"?/)
    match ? match[1] : File.basename(uri.path)
  else
    File.basename(uri.path)
  end
end

.require_relative_folder(*folders) ⇒ Object



9
10
11
12
13
14
# File 'lib/wolf_core/utils/file_utils.rb', line 9

def require_relative_folder(*folders)
  folder_files = File.join(*folders, "**", "*.rb")
  files_to_require = Dir[folder_files].sort
  # log_object files_to_require, title: 'Files to require are'
  safe_require(files_to_require)
end

.safe_require(missing_files) ⇒ Object

rubocop:disable Metrics/MethodLength



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/wolf_core/utils/file_utils.rb', line 16

def safe_require(missing_files) # rubocop:disable Metrics/MethodLength
  error_counter = {}
  while missing_files.any?
    files_to_require = missing_files
    missing_files = []
    files_to_require.each do |file|
      # log_object "Requiring file: #{file}"
      require_relative file
    rescue NameError => e
      error_counter[file] = error_counter[file].to_i + 1
      if error_counter[file] >= 10
        log_object "Error requiring file: #{file}"
        log_object e, title: "Error is"
      end
      missing_files << file if error_counter[file] < 15
    end
  end
end