Method: Illustration#download_image

Defined in:
lib/illustration.rb

#download_image(url, basename = nil) ⇒ Object

画像のURLからデータを保存して、保存したファイルの絶対パスを返す



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/illustration.rb', line 49

def download_image(url, basename = nil)
  basename = File.basename(basename ? basename : url, ".*")
  if path = search_image(basename)
    return path
  end
  open(url) do |fp|
    content_type = fp.meta["content-type"]
    ext = MIME[content_type] or raise UnknownMIMEType, content_type
    illust_abs_path = create_illust_path(basename) + "." + ext
    open(illust_abs_path, "wb") do |write_fp|
      write_fp.write(fp.read)
    end
    @inspector.info("挿絵「#{File.basename(illust_abs_path)}」を保存しました。")
    return illust_abs_path
  end
rescue UnknownMIMEType => e
  @inspector.error("Illustration#download_image: #{url} は未対応の画像フォーマットです" +
                   "(content-type: #{e.message})")
  nil
rescue StandardError => e
  @inspector.error("Illustration#download_image: #{url} を処理中に例外が発生しました(#{e})")
  nil
end