Class: Gitlab::GithubImport::AttachmentsDownloader

Inherits:
Object
  • Object
show all
Includes:
BulkImports::FileDownloads::FilenameFetch, BulkImports::FileDownloads::Validations, ImportExport::CommandLineUtil
Defined in:
lib/gitlab/github_import/attachments_downloader.rb

Constant Summary collapse

DownloadError =
Class.new(StandardError)
FILENAME_SIZE_LIMIT =

chars before the extension

255
DEFAULT_FILE_SIZE_LIMIT =
Gitlab::CurrentSettings.max_attachment_size.megabytes
TMP_DIR =
File.join(Dir.tmpdir, 'github_attachments').freeze
SUPPORTED_VIDEO_MEDIA_TYPES =
%w[mov mp4 webm].freeze

Constants included from BulkImports::FileDownloads::FilenameFetch

BulkImports::FileDownloads::FilenameFetch::REMOTE_FILENAME_PATTERN

Constants included from ImportExport::CommandLineUtil

ImportExport::CommandLineUtil::CLEAN_DIR_IGNORE_FILE_NAMES, ImportExport::CommandLineUtil::CommandLineUtilError, ImportExport::CommandLineUtil::DEFAULT_DIR_MODE, ImportExport::CommandLineUtil::FileOversizedError, ImportExport::CommandLineUtil::HardLinkError, ImportExport::CommandLineUtil::UNTAR_MASK

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from BulkImports::FileDownloads::Validations

#response_headers

Methods included from ImportExport::CommandLineUtil

#gunzip, #gzip, #gzip_with_options, #mkdir_p, #tar_cf, #tar_czf, #untar_xf, #untar_zxf

Constructor Details

#initialize(file_url, options: {}, file_size_limit: DEFAULT_FILE_SIZE_LIMIT, web_endpoint: ::Octokit::Default.web_endpoint) ⇒ AttachmentsDownloader

Returns a new instance of AttachmentsDownloader.



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/gitlab/github_import/attachments_downloader.rb', line 19

def initialize(
  file_url, options: {}, file_size_limit: DEFAULT_FILE_SIZE_LIMIT,
  web_endpoint: ::Octokit::Default.web_endpoint)
  @file_url = file_url
  @options = options
  @file_size_limit = file_size_limit
  @web_endpoint = web_endpoint

  filename = URI(file_url).path.split('/').last
  @filename = ensure_filename_size(filename)
end

Instance Attribute Details

#file_size_limitObject (readonly)

Returns the value of attribute file_size_limit.



17
18
19
# File 'lib/gitlab/github_import/attachments_downloader.rb', line 17

def file_size_limit
  @file_size_limit
end

#file_urlObject (readonly)

Returns the value of attribute file_url.



17
18
19
# File 'lib/gitlab/github_import/attachments_downloader.rb', line 17

def file_url
  @file_url
end

#filenameObject (readonly)

Returns the value of attribute filename.



17
18
19
# File 'lib/gitlab/github_import/attachments_downloader.rb', line 17

def filename
  @filename
end

#optionsObject (readonly)

Returns the value of attribute options.



17
18
19
# File 'lib/gitlab/github_import/attachments_downloader.rb', line 17

def options
  @options
end

#web_endpointObject (readonly)

Returns the value of attribute web_endpoint.



17
18
19
# File 'lib/gitlab/github_import/attachments_downloader.rb', line 17

def web_endpoint
  @web_endpoint
end

Instance Method Details

#deleteObject



53
54
55
# File 'lib/gitlab/github_import/attachments_downloader.rb', line 53

def delete
  FileUtils.rm_rf File.dirname(filepath)
end

#performObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/gitlab/github_import/attachments_downloader.rb', line 31

def perform
  validate_filepath

  download_url = get_assets_download_redirection_url

  # skip download for GHE server urls that redirect to a login url
  return file_url if download_url.include?("login?return_to=")

  parsed_file_name = File.basename(URI.parse(download_url).path)

  # if the file has a video filetype extension, we update both the @filename and @filepath with the filetype ext.
  if parsed_file_name.end_with?(*SUPPORTED_VIDEO_MEDIA_TYPES.map { |ext| ".#{ext}" })
    @filename = ensure_filename_size(parsed_file_name)
    add_extension_to_file_path(filename)
  end

  file = download_from(download_url)

  validate_symlink
  file
end