Class: Pages::ZipDirectoryService

Inherits:
Object
  • Object
show all
Includes:
BaseServiceUtility, Gitlab::Utils::StrongMemoize
Defined in:
app/services/pages/zip_directory_service.rb

Constant Summary collapse

InvalidEntryError =

used only to track exceptions in Sentry

Class.new(StandardError)
PUBLIC_DIR =
'public'

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from BaseServiceUtility

#deny_visibility_level, #event_service, #log_error, #log_info, #notification_service, #system_hook_service, #todo_service, #visibility_level

Methods included from Gitlab::Allowable

#can?

Constructor Details

#initialize(input_dir, ignore_invalid_entries: false) ⇒ ZipDirectoryService

Returns a new instance of ZipDirectoryService.



15
16
17
18
# File 'app/services/pages/zip_directory_service.rb', line 15

def initialize(input_dir, ignore_invalid_entries: false)
  @input_dir = input_dir
  @ignore_invalid_entries = ignore_invalid_entries
end

Instance Attribute Details

#public_dirObject (readonly)

Returns the value of attribute public_dir.



13
14
15
# File 'app/services/pages/zip_directory_service.rb', line 13

def public_dir
  @public_dir
end

#real_dirObject (readonly)

Returns the value of attribute real_dir.



13
14
15
# File 'app/services/pages/zip_directory_service.rb', line 13

def real_dir
  @real_dir
end

Instance Method Details

#executeObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/services/pages/zip_directory_service.rb', line 20

def execute
  return success unless resolve_public_dir

  output_file = File.join(real_dir, "@migrated.zip") # '@' to avoid any name collision with groups or projects

  FileUtils.rm_f(output_file)

  entries_count = 0
  # Since we're writing not reading here, we can safely silence the cop.
  # It currently cannot discern between opening for reading or writing.
  ::Zip::File.open(output_file, ::Zip::File::CREATE) do |zipfile| # rubocop:disable Performance/Rubyzip
    write_entry(zipfile, PUBLIC_DIR)
    entries_count = zipfile.entries.count
  end

  success(archive_path: output_file, entries_count: entries_count)
rescue StandardError => e
  FileUtils.rm_f(output_file) if output_file
  raise e
end