lib/paperweight/download.rb in paperweight-0.1.2 vs lib/paperweight/download.rb in paperweight-0.2.0
- old
+ new
@@ -11,12 +11,10 @@
RuntimeError, # redirection errors (e.g. redirection loop)
URI::InvalidURIError, # invalid URL
Error # our errors
].freeze
- MAX_SIZE = 10 * 1024 * 1024
-
def download(url)
# Finally we download the file. Here we mustn't use simple #open that
# open-uri overrides, because this is vulnerable to shell execution
# attack (if #open method detects a starting pipe (e.g. "| ls"), it will
# execute the following as a shell command).
@@ -50,20 +48,22 @@
OpenURI::Meta.init(tempfile)
end
end
def open_options
+ max_size = Paperweight.config.max_size
+
{}.tap do |options|
# It was shown that in a random sample approximately 20% of websites
# will simply refuse a request which doesn't have a valid User-Agent.
options['User-Agent'] = 'Paperweight'
# It's good to shield ourselves from files that are too big. open-uri
# will call this block as soon as it gets the "Content-Length" header,
# which means that we can bail out before we download the file.
options[:content_length_proc] = lambda { |size|
- if size && size > MAX_SIZE
- raise Error, "file is too big (max is #{MAX_SIZE})"
+ if size && size > max_size
+ raise Error, "file is too big (max is #{max_size})"
end
}
end
end