This commit is contained in:
r888888888
2015-02-20 20:04:23 -08:00
parent b93cda01b8
commit f12fb40e3e
9 changed files with 37 additions and 4 deletions

View File

@@ -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

View File

@@ -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"]

View File

@@ -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]

View File

@@ -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?

View File

@@ -37,6 +37,12 @@ class PostPresenter < Presenter
html << %{</p>}
end
if options[:size]
html << %{<p class="desc">}
html << post.file_size.to_formatted_s(:human_size)
html << %{</p>}
end
html << %{</article>}
html.html_safe
end

View File

@@ -1,7 +1,7 @@
<% if @results.any? %>
<h3>Similar</h3>
<% @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 %>
<h3>Similar</h3>

View File

@@ -10,7 +10,8 @@
<p><%= link_to "Fetch source data", source_path(:format => "json"), :id => "fetch-data-manual" %></p>
<p><%= content_tag "span", "Loading source data...", :id => "loading-data", :style => "display: none;" %></p>
<% end %>
<p><%= content_tag "span", "Gallery. Tags may not apply to all images.", :id => "gallery-warning", :style => "display: none;" %></p>
<p id="remote-size" style="display: none;"></p>
<p id="gallery-warning" style="display: none;"><%= content_tag "span", "Gallery. Tags may not apply to all images." %></p>
<ul>
<li><strong>Artist</strong>: <span id="source-artist"></span></li>

View File

@@ -1,8 +1,13 @@
<% if params[:url] %>
<%= image_tag(@normalized_url, :title => "Preview", :id => "image") %>
<% if params[:ref] %>
<p><strong>Referrer: </strong> <%= params[:ref] %></p>
<% end %>
<% if @remote_size %>
<p><strong>Size:</strong> <%= number_to_human_size(@remote_size) %></p>
<% end %>
<p id="scale"></p>
<% end %>

View File

@@ -2,5 +2,7 @@
- post
-->
<% if post %>
<h2 style="margin-bottom: 1em;">This post was probably already uploaded (<%= link_to "post ##{post.id}", post_path(post), :target => "_blank" %>)</h2>
<h2 style="margin-bottom: 1em;">
This post was probably already uploaded (<%= link_to "post ##{post.id}", post_path(post), :target => "_blank" %>)
</h2>
<% end %>