Files
danbooru/app/controllers/uploads_controller.rb
2015-07-04 16:31:34 -04:00

121 lines
3.3 KiB
Ruby

class UploadsController < ApplicationController
before_filter :member_only
after_filter :save_recent_tags, :only => [:create]
respond_to :html, :xml, :json, :js
rescue_from Upload::Error, :with => :rescue_exception
def new
@upload = Upload.new
if params[:url]
@normalized_url = params[:url]
headers = default_headers()
data = {}
Downloads::RewriteStrategies::Base.strategies.each do |strategy|
@normalized_url, headers, data = strategy.new(@normalized_url).rewrite(@normalized_url, headers, data)
end
@post = find_post_by_url(@normalized_url)
extract_artist_commentary(@upload, data)
begin
@source = Sources::Site.new(params[:url], :referer_url => params[:ref])
@remote_size = Downloads::File.new(@normalized_url, ".").size
rescue Exception
end
end
respond_with(@upload)
end
def batch
@source = Sources::Site.new(params[:url], :referer_url => params[:ref])
@source.get
@urls = @source.image_urls
end
def image_proxy
if params[:url].blank? || params[:ref].blank?
raise "Must specify url and referer"
end
url = URI.parse(params[:url])
headers = {
"Referer" => params[:ref],
"User-Agent" => "#{Danbooru.config.safe_app_name}/#{Danbooru.config.version}"
}
Net::HTTP.start(url.host, url.port) do |http|
resp = http.request_get(url.request_uri, headers)
if resp.is_a?(Net::HTTPSuccess)
send_data resp.body, type: resp.content_type, disposition: "inline"
else
raise "HTTP error code: #{resp.code} #{resp.message}"
end
end
end
def index
@search = Upload.search(params[:search])
@uploads = @search.order("id desc").paginate(params[:page], :limit => params[:limit])
respond_with(@uploads) do |format|
format.xml do
render :xml => @uploads.to_xml(:root => "uploads")
end
end
end
def show
@upload = Upload.find(params[:id])
respond_with(@upload) do |format|
format.html do
if @upload.is_completed? && @upload.post_id
redirect_to(post_path(@upload.post_id))
end
end
end
end
def create
@upload = Upload.create(params[:upload].merge(:server => Socket.gethostname))
@upload.process! if @upload.errors.empty?
respond_with(@upload)
end
def update
@upload = Upload.find(params[:id])
@upload.process!
respond_with(@upload)
end
protected
def extract_artist_commentary(upload, data)
if data[:artist_commentary_desc]
upload.artist_commentary_title = data[:artist_commentary_title]
upload.artist_commentary_desc = data[:artist_commentary_desc]
end
end
def find_post_by_url(normalized_url)
if normalized_url.nil?
Post.where(source: params[:url]).first
else
Post.where(source: [params[:url], @normalized_url]).first
end
end
def default_headers
{
"User-Agent" => "#{Danbooru.config.safe_app_name}/#{Danbooru.config.version}"
}
end
def save_recent_tags
if @upload
tags = Tag.scan_tags(@upload.tag_string)
tags = (TagAlias.to_aliased(tags) + Tag.scan_tags(cookies[:recent_tags])).uniq.slice(0, 30)
cookies[:recent_tags] = tags.join(" ")
cookies[:recent_tags_with_categories] = Tag.categories_for(tags).to_a.flatten.join(" ")
end
end
end