Merge branch 'master' of github.com:r888888888/danbooru
This commit is contained in:
@@ -41,8 +41,8 @@ module Downloads
|
||||
limit = 4
|
||||
|
||||
while true
|
||||
unless url.is_a?(URI::HTTP)
|
||||
raise Error.new("URL must be HTTP")
|
||||
unless url.is_a?(URI::HTTP) || url.is_a?(URI::HTTPS)
|
||||
raise Error.new("URL must be HTTP or HTTPS")
|
||||
end
|
||||
|
||||
headers = {
|
||||
@@ -50,7 +50,7 @@ module Downloads
|
||||
}
|
||||
@source, headers = before_download(source, headers)
|
||||
|
||||
Net::HTTP.start(url.host, url.port) do |http|
|
||||
Net::HTTP.start(url.host, url.port, :use_ssl => url.is_a?(URI::HTTPS)) do |http|
|
||||
http.read_timeout = 10
|
||||
http.request_get(url.request_uri, headers) do |res|
|
||||
case res
|
||||
|
||||
@@ -247,7 +247,7 @@ class Upload < ActiveRecord::Base
|
||||
module DownloaderMethods
|
||||
# Determines whether the source is downloadable
|
||||
def is_downloadable?
|
||||
source =~ /^http:\/\// && file_path.blank?
|
||||
source =~ /^https?:\/\// && file_path.blank?
|
||||
end
|
||||
|
||||
# Downloads the file to destination_path
|
||||
|
||||
@@ -39,5 +39,24 @@ module Downloads
|
||||
assert_match(/image\/gif/, @download.content_type)
|
||||
end
|
||||
end
|
||||
|
||||
context "A post download with an HTTPS source" do
|
||||
setup do
|
||||
@source = "https://www.google.com/intl/en_ALL/images/logo.gif"
|
||||
@tempfile = Tempfile.new("danbooru-test")
|
||||
@download = Downloads::File.new(@source, @tempfile.path)
|
||||
end
|
||||
|
||||
teardown do
|
||||
@tempfile.close
|
||||
end
|
||||
|
||||
should "stream a file from an HTTPS source" do
|
||||
@download.http_get_streaming do |resp|
|
||||
assert_equal("200", resp.code)
|
||||
assert(resp["Content-Length"].to_i > 0, "File should be larger than 0 bytes")
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -79,6 +79,23 @@ class UploadTest < ActiveSupport::TestCase
|
||||
end
|
||||
end
|
||||
|
||||
context "determining if a file is downloadable" do
|
||||
should "classify HTTP sources as downloadable" do
|
||||
@upload = FactoryGirl.create(:source_upload, source: "http://www.example.com/1.jpg")
|
||||
assert_not_nil(@upload.is_downloadable?)
|
||||
end
|
||||
|
||||
should "classify HTTPS sources as downloadable" do
|
||||
@upload = FactoryGirl.create(:source_upload, source: "https://www.example.com/1.jpg")
|
||||
assert_not_nil(@upload.is_downloadable?)
|
||||
end
|
||||
|
||||
should "classify non-HTTP/HTTPS sources as not downloadable" do
|
||||
@upload = FactoryGirl.create(:source_upload, source: "ftp://www.example.com/1.jpg")
|
||||
assert_nil(@upload.is_downloadable?)
|
||||
end
|
||||
end
|
||||
|
||||
context "file processor" do
|
||||
should "parse and process a cgi file representation" do
|
||||
FileUtils.cp("#{Rails.root}/test/files/test.jpg", "#{Rails.root}/tmp")
|
||||
|
||||
Reference in New Issue
Block a user