Class: Dratools::DownloadService

Inherits:
Object
  • Object
show all
Defined in:
lib/dratools/download_service.rb

Overview

解決済みダウンロードの取得、probe、md5 検証、既存ファイル判定をまとめて扱う。

Defined Under Namespace

Classes: DownloadResult

Constant Summary collapse

DEFAULT_OUTPUT_DIRECTORY =
'.'
DEFAULT_PROTOCOL =
DownloadCandidate::HTTPS_PROTOCOL
DEFAULT_PROBE_TIMEOUT_SECONDS =
5
DEFAULT_SIZE_TIMEOUT_SECONDS =
10
DEFAULT_REDIRECT_LIMIT =
5
HTTP_LOCATION_HEADER =
'location'
HTTPS_SCHEME =
'https'
USER_AGENT_HEADER =
'User-Agent'
FASTQ_HREF_PATTERN =
/href=(?<quote>["'])(?<href>[^"']*\.fastq[^"']*)\k<quote>/i

Instance Method Summary collapse

Constructor Details

#initialize(runner: ExternalCommandRunner.new, checksum_verifier: ChecksumVerifier.new) ⇒ DownloadService

Returns a new instance of DownloadService.



38
39
40
41
# File 'lib/dratools/download_service.rb', line 38

def initialize(runner: ExternalCommandRunner.new, checksum_verifier: ChecksumVerifier.new)
  @runner = runner
  @checksum_verifier = checksum_verifier
end

Instance Method Details

#content_lengths(download, protocol: DEFAULT_PROTOCOL, timeout: DEFAULT_SIZE_TIMEOUT_SECONDS) ⇒ Object



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

def content_lengths(download, protocol: DEFAULT_PROTOCOL, timeout: DEFAULT_SIZE_TIMEOUT_SECONDS)
  download_url = download.url_for_protocol(protocol)
  return [nil] unless http_url?(download_url)

  if download.directory_url_for_protocol?(protocol)
    file_urls = directory_file_urls(download_url, timeout: timeout)
    return [nil] if file_urls.empty?

    return file_urls.map { |file_url| safe_head_content_length(file_url, timeout: timeout) }
  end

  [safe_head_content_length(download_url, timeout: timeout)]
rescue Error
  [nil]
end

#probe_download(download, protocol: DEFAULT_PROTOCOL, timeout: DEFAULT_PROBE_TIMEOUT_SECONDS) ⇒ Object



43
44
45
# File 'lib/dratools/download_service.rb', line 43

def probe_download(download, protocol: DEFAULT_PROTOCOL, timeout: DEFAULT_PROBE_TIMEOUT_SECONDS)
  @runner.probe_url(download.url_for_protocol(protocol), timeout: timeout)
end

#save_download(download, outdir: DEFAULT_OUTPUT_DIRECTORY, protocol: DEFAULT_PROTOCOL, verify: true, force: false, skip_existing: false) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/dratools/download_service.rb', line 63

def save_download(
  download,
  outdir: DEFAULT_OUTPUT_DIRECTORY,
  protocol: DEFAULT_PROTOCOL,
  verify: true,
  force: false,
  skip_existing: false
)
  FileUtils.mkdir_p(outdir)
  download_url = download.url_for_protocol(protocol)
  if download.directory_url_for_protocol?(protocol)
    raise InvalidRecordError, "download URL points to a directory: #{download_url}"
  end

  output_path = File.join(outdir, download.filename_for_protocol(protocol))
  FileUtils.rm_f(output_path) if force && File.file?(output_path)
  if should_skip_existing?(
    output_path,
    download,
    download_url: download_url,
    skip_existing: skip_existing
  )
    return DownloadResult.new(path: output_path, skipped: true)
  end

  @runner.download_url(download_url, output_path)
  if verify && checksum_available?(download)
    @checksum_verifier.verify_md5!(output_path, download.md5)
  end
  DownloadResult.new(path: output_path, skipped: false)
end