From f12fb40e3efb4005f04307187e784c4de5e9cb59 Mon Sep 17 00:00:00 2001
From: r888888888
Date: Fri, 20 Feb 2015 20:04:23 -0800
Subject: [PATCH] implements #2350
---
app/controllers/uploads_controller.rb | 1 +
app/logical/downloads/file.rb | 14 ++++++++++++++
app/logical/sources/site.rb | 2 +-
app/logical/sources/strategies/base.rb | 4 ++++
app/presenters/post_presenter.rb | 6 ++++++
app/views/iqdb_queries/create_by_url.html.erb | 2 +-
app/views/sources/_info.html.erb | 3 ++-
app/views/uploads/_image.html.erb | 5 +++++
app/views/uploads/_post.html.erb | 4 +++-
9 files changed, 37 insertions(+), 4 deletions(-)
diff --git a/app/controllers/uploads_controller.rb b/app/controllers/uploads_controller.rb
index d53d731f4..909b0e6f7 100644
--- a/app/controllers/uploads_controller.rb
+++ b/app/controllers/uploads_controller.rb
@@ -20,6 +20,7 @@ class UploadsController < ApplicationController
begin
@source = Sources::Site.new(params[:url])
+ @remote_size = Downloads::File.new(@normalized_url, ".").size
rescue Exception
end
end
diff --git a/app/logical/downloads/file.rb b/app/logical/downloads/file.rb
index 811c73d11..c0d7da5d9 100644
--- a/app/logical/downloads/file.rb
+++ b/app/logical/downloads/file.rb
@@ -22,6 +22,20 @@ module Downloads
@data[:get_thumbnail] = options[:get_thumbnail]
end
+ def size
+ headers = {
+ "User-Agent" => "#{Danbooru.config.safe_app_name}/#{Danbooru.config.version}"
+ }
+ @source, headers, @data = before_download(@source, headers, @data)
+ url = URI.parse(@source)
+ Net::HTTP.start(url.host, url.port, :use_ssl => url.is_a?(URI::HTTPS)) do |http|
+ http.read_timeout = 3
+ http.request_head(url.request_uri, headers) do |res|
+ return res.content_length
+ end
+ end
+ end
+
def download!
@source, @data = http_get_streaming(@source, @data) do |response|
self.content_type = response["Content-Type"]
diff --git a/app/logical/sources/site.rb b/app/logical/sources/site.rb
index bb7a9f90c..915c682f9 100644
--- a/app/logical/sources/site.rb
+++ b/app/logical/sources/site.rb
@@ -3,7 +3,7 @@
module Sources
class Site
attr_reader :url, :strategy
- delegate :get, :referer_url, :site_name, :artist_name, :profile_url, :image_url, :tags, :artist_record, :unique_id, :page_count, :file_url, :ugoira_frame_data, :to => :strategy
+ delegate :get, :get_size, :referer_url, :site_name, :artist_name, :profile_url, :image_url, :tags, :artist_record, :unique_id, :page_count, :file_url, :ugoira_frame_data, :to => :strategy
def self.strategies
[Strategies::Pixiv, Strategies::NicoSeiga, Strategies::DeviantArt, Strategies::Nijie, Strategies::Twitter]
diff --git a/app/logical/sources/strategies/base.rb b/app/logical/sources/strategies/base.rb
index fe1978ea2..bee4f2269 100644
--- a/app/logical/sources/strategies/base.rb
+++ b/app/logical/sources/strategies/base.rb
@@ -18,6 +18,10 @@ module Sources
raise NotImplementedError
end
+ def get_size
+ @get_size ||= Downloads::File.new(@image_url).size
+ end
+
# Subclasses should return true only if the URL is in its final normalized form.
#
# Sources::Site.new("http://img.pixiv.net/img/evazion").normalized_for_artist_finder?
diff --git a/app/presenters/post_presenter.rb b/app/presenters/post_presenter.rb
index 3f524940a..9ef60e5a4 100644
--- a/app/presenters/post_presenter.rb
+++ b/app/presenters/post_presenter.rb
@@ -37,6 +37,12 @@ class PostPresenter < Presenter
html << %{
}
end
+ if options[:size]
+ html << %{}
+ html << post.file_size.to_formatted_s(:human_size)
+ html << %{
}
+ end
+
html << %{}
html.html_safe
end
diff --git a/app/views/iqdb_queries/create_by_url.html.erb b/app/views/iqdb_queries/create_by_url.html.erb
index 4ae4295dd..8a5aa5ae3 100644
--- a/app/views/iqdb_queries/create_by_url.html.erb
+++ b/app/views/iqdb_queries/create_by_url.html.erb
@@ -1,7 +1,7 @@
<% if @results.any? %>
Similar
<% @results.each do |match| %>
- <%= PostPresenter.preview(Post.find(match.post_id), :tags => "status:any") %>
+ <%= PostPresenter.preview(Post.find(match.post_id), :tags => "status:any", :size => true) %>
<% end %>
<% else %>
Similar
diff --git a/app/views/sources/_info.html.erb b/app/views/sources/_info.html.erb
index b1a0eb693..8ae9928cd 100644
--- a/app/views/sources/_info.html.erb
+++ b/app/views/sources/_info.html.erb
@@ -10,7 +10,8 @@
<%= link_to "Fetch source data", source_path(:format => "json"), :id => "fetch-data-manual" %>
<%= content_tag "span", "Loading source data...", :id => "loading-data", :style => "display: none;" %>
<% end %>
- <%= content_tag "span", "Gallery. Tags may not apply to all images.", :id => "gallery-warning", :style => "display: none;" %>
+
+ <%= content_tag "span", "Gallery. Tags may not apply to all images." %>
- Artist:
diff --git a/app/views/uploads/_image.html.erb b/app/views/uploads/_image.html.erb
index d37bb2c20..a81c4e1ce 100644
--- a/app/views/uploads/_image.html.erb
+++ b/app/views/uploads/_image.html.erb
@@ -1,8 +1,13 @@
<% if params[:url] %>
<%= image_tag(@normalized_url, :title => "Preview", :id => "image") %>
+
<% if params[:ref] %>
Referrer: <%= params[:ref] %>
<% end %>
+ <% if @remote_size %>
+ Size: <%= number_to_human_size(@remote_size) %>
+ <% end %>
+
<% end %>
diff --git a/app/views/uploads/_post.html.erb b/app/views/uploads/_post.html.erb
index f7ba00196..6882edfeb 100644
--- a/app/views/uploads/_post.html.erb
+++ b/app/views/uploads/_post.html.erb
@@ -2,5 +2,7 @@
- post
-->
<% if post %>
- This post was probably already uploaded (<%= link_to "post ##{post.id}", post_path(post), :target => "_blank" %>)
+
+ This post was probably already uploaded (<%= link_to "post ##{post.id}", post_path(post), :target => "_blank" %>)
+
<% end %>