Commit fdd7582f authored by Albert Yi's avatar Albert Yi

add support for upload preprocessing

parent b561a6d9
unicorn: bundle exec rails server
jobs: bundle exec script/delayed_job run
jobs: bundle exec script/delayed_job run
......@@ -3,31 +3,14 @@ class UploadsController < ApplicationController
respond_to :html, :xml, :json, :js
def new
@upload = Upload.new
@upload_notice_wiki = WikiPage.titled(Danbooru.config.upload_notice_wiki_page).first
if params[:url]
download = Downloads::File.new(params[:url])
@normalized_url, _, _ = download.before_download(params[:url], {})
@post = find_post_by_url(@normalized_url)
begin
@source = Sources::Site.new(params[:url], :referer_url => params[:ref])
@remote_size = download.size
rescue Exception
end
end
@upload, @post, @source, @normalized_url, @remote_size = UploadService::ControllerHelper.prepare(params[:url], params[:ref])
respond_with(@upload)
end
def batch
@url = params.dig(:batch, :url) || params[:url]
@source = nil
if @url
@source = Sources::Site.new(@url, :referer_url => params[:ref])
@source.get
end
@source = UploadService::ControllerHelper.batch(@url, params[:ref])
respond_with(@source)
end
......@@ -57,14 +40,11 @@ class UploadsController < ApplicationController
end
def create
@upload = Upload.create(upload_params)
@service = UploadService.new(upload_params)
@upload = @service.start!
if @upload.errors.empty?
post = @upload.process!
if post.present? && post.valid? && post.warnings.any?
flash[:notice] = post.warnings.full_messages.join(".\n \n")
end
if @service.warnings.any?
flash[:notice] = @service.warnings.join(".\n \n")
end
save_recent_tags
......@@ -73,14 +53,6 @@ class UploadsController < ApplicationController
private
def find_post_by_url(normalized_url)
if normalized_url.nil?
Post.where("SourcePattern(lower(posts.source)) = ?", params[:url]).first
else
Post.where("SourcePattern(lower(posts.source)) IN (?)", [params[:url], @normalized_url]).first
end
end
def save_recent_tags
if @upload
tags = Tag.scan_tags(@upload.tag_string)
......@@ -94,7 +66,7 @@ class UploadsController < ApplicationController
permitted_params = %i[
file source tag_string rating status parent_id artist_commentary_title
artist_commentary_desc include_artist_commentary referer_url
md5_confirmation as_pending
md5_confirmation as_pending
]
params.require(:upload).permit(permitted_params)
......
class PixivUgoiraService
attr_reader :width, :height, :frame_data, :content_type
def save_frame_data(post)
PixivUgoiraFrameData.create(:data => @frame_data, :content_type => @content_type, :post_id => post.id)
end
def calculate_dimensions(source_path)
folder = Zip::File.new(source_path)
tempfile = Tempfile.new("ugoira-dimensions")
begin
folder.first.extract(tempfile.path) {true}
image_size = ImageSpec.new(tempfile.path)
@width = image_size.width
@height = image_size.height
ensure
tempfile.close
tempfile.unlink
end
end
def load(data)
if data[:is_ugoira]
@frame_data = data[:ugoira_frame_data]
@content_type = data[:ugoira_content_type]
end
end
def empty?
@frame_data.nil?
end
end
This diff is collapsed.
This diff is collapsed.
......@@ -12,7 +12,7 @@
<p>This upload has finished processing. <%= link_to "View the post", post_path(@upload.post_id) %>.</p>
<% elsif @upload.is_pending? %>
<p>This upload is waiting to be processed. Please wait a few seconds.</p>
<% elsif @upload.is_processing? %>
<% elsif @upload.is_processing? || @upload.is_preprocessing? || @upload.is_preprocessed? %>
<p>This upload is being processed. Please wait a few seconds.</p>
<% elsif @upload.is_duplicate? %>
<p>This upload is a duplicate: <%= link_to "post ##{@upload.duplicate_post_id}", post_path(@upload.duplicate_post_id) %></p>
......@@ -42,7 +42,7 @@
Upload - <%= Danbooru.config.app_name %>
<% end %>
<% if @upload.is_pending? || @upload.is_processing? %>
<% if @upload.is_pending? || @upload.is_processing? || @upload.is_preprocessing? || @upload.is_preprocessed? %>
<% content_for(:html_header) do %>
<meta http-equiv="refresh" content="2">
<% end %>
......
unless Rails.env.development?
FFMPEG.logger.level = Logger::ERROR
end
class AddMissingFieldsToUploads < ActiveRecord::Migration[5.2]
def change
add_column :uploads, :md5, :string
add_column :uploads, :file_ext, :string
add_column :uploads, :file_size, :integer
add_column :uploads, :image_width, :integer
add_column :uploads, :image_height, :integer
add_column :uploads, :artist_commentary_desc, :text
add_column :uploads, :artist_commentary_title, :text
add_column :uploads, :include_artist_commentary, :boolean
end
end
class AddContextToUploads < ActiveRecord::Migration[5.2]
def change
add_column :uploads, :context, :text
end
end
......@@ -14,6 +14,14 @@ FactoryBot.define do
source "http://www.google.com/intl/en_ALL/images/logo.gif"
end
factory(:ugoira_upload) do
file do
f = Tempfile.new
IO.copy_stream("#{Rails.root}/test/fixtures/ugoira.zip", f.path)
ActionDispatch::Http::UploadedFile.new(tempfile: f, filename: "ugoira.zip")
end
end
factory(:jpg_upload) do
file do
f = Tempfile.new
......
......@@ -38,6 +38,15 @@ class UploadsControllerTest < ActionDispatch::IntegrationTest
assert_response :success
end
context "with a url" do
should "preprocess" do
assert_difference(-> { Upload.count }) do
get_auth new_upload_path, @user, params: {:url => "http://www.google.com/intl/en_ALL/images/logo.gif"}
assert_response :success
end
end
end
context "for a twitter post" do
should "render" do
skip "Twitter keys are not set" unless Danbooru.config.twitter_api_key
......@@ -49,13 +58,15 @@ class UploadsControllerTest < ActionDispatch::IntegrationTest
context "for a post that has already been uploaded" do
setup do
as_user do
@post = create(:post, :source => "aaa")
@post = create(:post, :source => "http://google.com/aaa")
end
end
should "initialize the post" do
get_auth new_upload_path, @user, params: {:url => "http://google.com/aaa"}
assert_response :success
assert_difference(-> { Upload.count }, 0) do
get_auth new_upload_path, @user, params: {:url => "http://google.com/aaa"}
assert_response :success
end
end
end
end
......
This diff is collapsed.
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment