Replace the old IQDB API client with a new client for the new forked version of IQDB at https://github.com/danbooru/iqdb. Changes: * The /iqdb_queries endpoint now returns `hash` and `signature` fields. The `signature` is the full decoded Haar signature, while the `hash` is a encoded version of the signature. * The /iqdb_queries endpoint no longer returns `width` and `height` fields in the response (these were always 128x128). * We no longer need the IQDBs frontend server, now we talk to the IQDB instance directly. * We no longer send add/remove image commands to IQDB through AWS SQS, now we send them to IQDB directly. They are sent in a delayed job so that if IQDB is down, uploading images is still possible, the add image commands will just get queued up. * Fix a bug where regenerating an image's thumbnails didn't regenerate IQDB, because IQDB silently ignored add image commands when the image already existed in the database.
118 lines
3.0 KiB
Ruby
118 lines
3.0 KiB
Ruby
require 'upload_service/controller_helper'
|
|
require 'upload_service/preprocessor'
|
|
require 'upload_service/replacer'
|
|
require 'upload_service/utils'
|
|
|
|
class UploadService
|
|
attr_reader :params, :post, :upload
|
|
|
|
def initialize(params)
|
|
@params = params
|
|
end
|
|
|
|
def delayed_start(uploader_id)
|
|
CurrentUser.as(uploader_id) do
|
|
start!
|
|
end
|
|
rescue ActiveRecord::RecordNotUnique
|
|
end
|
|
|
|
def start!
|
|
preprocessor = Preprocessor.new(params)
|
|
|
|
if preprocessor.in_progress?
|
|
UploadServiceDelayedStartJob.set(wait: 5.seconds).perform_later(params, CurrentUser.user)
|
|
return preprocessor.predecessor
|
|
end
|
|
|
|
if preprocessor.completed?
|
|
@upload = preprocessor.finish!
|
|
|
|
begin
|
|
create_post_from_upload(@upload)
|
|
rescue Exception => e
|
|
@upload.update(status: "error: #{e.class} - #{e.message}", backtrace: e.backtrace.join("\n"))
|
|
end
|
|
return @upload
|
|
end
|
|
|
|
params[:rating] ||= "q"
|
|
params[:tag_string] ||= "tagme"
|
|
@upload = Upload.create!(params)
|
|
|
|
begin
|
|
if @upload.invalid?
|
|
return @upload
|
|
end
|
|
|
|
@upload.update(status: "processing")
|
|
|
|
@upload.file = Utils.get_file_for_upload(@upload, file: @upload.file&.tempfile)
|
|
Utils.process_file(upload, @upload.file)
|
|
|
|
@upload.save!
|
|
@post = create_post_from_upload(@upload)
|
|
@upload
|
|
rescue Exception => e
|
|
@upload.update(status: "error: #{e.class} - #{e.message}", backtrace: e.backtrace.join("\n"))
|
|
@upload
|
|
end
|
|
end
|
|
|
|
def warnings
|
|
return [] if @post.nil?
|
|
@post.warnings.full_messages
|
|
end
|
|
|
|
def create_post_from_upload(upload)
|
|
@post = convert_to_post(upload)
|
|
@post.save!
|
|
|
|
if upload.context && upload.context["ugoira"]
|
|
PixivUgoiraFrameData.create(
|
|
post_id: @post.id,
|
|
data: upload.context["ugoira"]["frame_data"],
|
|
content_type: upload.context["ugoira"]["content_type"]
|
|
)
|
|
end
|
|
|
|
if upload.has_commentary?
|
|
@post.create_artist_commentary(
|
|
:original_title => upload.artist_commentary_title,
|
|
:original_description => upload.artist_commentary_desc,
|
|
:translated_title => upload.translated_commentary_title,
|
|
:translated_description => upload.translated_commentary_desc
|
|
)
|
|
end
|
|
|
|
@post.update_iqdb
|
|
|
|
upload.update(status: "completed", post_id: @post.id)
|
|
|
|
@post
|
|
end
|
|
|
|
def convert_to_post(upload)
|
|
Post.new.tap do |p|
|
|
p.has_cropped = true
|
|
p.tag_string = upload.tag_string
|
|
p.md5 = upload.md5
|
|
p.file_ext = upload.file_ext
|
|
p.image_width = upload.image_width
|
|
p.image_height = upload.image_height
|
|
p.rating = upload.rating
|
|
if upload.source.present?
|
|
p.source = Sources::Strategies.find(upload.source, upload.referer_url).canonical_url || upload.source
|
|
end
|
|
p.file_size = upload.file_size
|
|
p.uploader_id = upload.uploader_id
|
|
p.uploader_ip_addr = upload.uploader_ip_addr
|
|
p.parent_id = upload.parent_id
|
|
|
|
if !upload.uploader.can_upload_free? || upload.upload_as_pending?
|
|
p.is_pending = true
|
|
end
|
|
end
|
|
end
|
|
end
|