blob: b71d5f753f2b66e7a0442caa55cebe6e7ca6ce79 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
|
# frozen_string_literal: true
module Gitlab
module GithubImport
class AttachmentsDownloader
include ::Gitlab::ImportExport::CommandLineUtil
include ::BulkImports::FileDownloads::FilenameFetch
include ::BulkImports::FileDownloads::Validations
DownloadError = Class.new(StandardError)
FILENAME_SIZE_LIMIT = 255 # chars before the extension
DEFAULT_FILE_SIZE_LIMIT = 25.megabytes
TMP_DIR = File.join(Dir.tmpdir, 'github_attachments').freeze
attr_reader :file_url, :filename, :file_size_limit
def initialize(file_url, file_size_limit: DEFAULT_FILE_SIZE_LIMIT)
@file_url = file_url
@file_size_limit = file_size_limit
filename = URI(file_url).path.split('/').last
@filename = ensure_filename_size(filename)
end
def perform
validate_content_length
validate_filepath
file = download
validate_symlink
file
end
def delete
FileUtils.rm_rf File.dirname(filepath)
end
private
def raise_error(message)
raise DownloadError, message
end
def response_headers
@response_headers ||=
Gitlab::HTTP.perform_request(Net::HTTP::Head, file_url, {}).headers
end
def download
file = File.open(filepath, 'wb')
Gitlab::HTTP.perform_request(Net::HTTP::Get, file_url, stream_body: true) { |batch| file.write(batch) }
file
end
def filepath
@filepath ||= begin
dir = File.join(TMP_DIR, SecureRandom.uuid)
mkdir_p dir
File.join(dir, filename)
end
end
end
end
end
|