Files
danbooru/app/controllers/uploads_controller.rb
evazion 43c4158d36 uploads: merge tags when a duplicate is uploaded (fix #3130).
Automatically merge tags when uploading a duplicate.

There are two cases:

* You try to upload an image, but it's already on Danbooru. In this case
  you'll be immediately redirected to the original post, before you
  can start tagging the upload.

* You're uploading an image, it wasn't a dupe when you first opened the
  upload page, but you got sniped while tagging it. In this case your tags
  will be merged with the original post, and you will be redirected to the
  original post.

There are a few corner cases:

* If you don't have permission to edit the original post, for example
  because it's banned or has a censored tag, then your tags won't be
  merged and will be silently ignored.

* Only the tags, rating, and parent ID will be merged. The source and
  artist commentary won't be merged. This is so that if an artist uploads
  the exact same file to multiple sites, the new source won't override
  the original source.

* Some tags might be contradictory. For example, the new post might
  be tagged translation_request, but the original post might already be
  translated. It's up to the user to fix these things afterwards.
2022-01-30 03:14:22 -06:00

50 lines
1.7 KiB
Ruby

# frozen_string_literal: true
class UploadsController < ApplicationController
respond_to :html, :xml, :json, :js
skip_before_action :verify_authenticity_token, only: [:create], if: -> { request.xhr? }
def new
@upload = authorize Upload.new(uploader: CurrentUser.user, uploader_ip_addr: CurrentUser.ip_addr, rating: "q", tag_string: "", source: params[:url], referer_url: params[:ref], **permitted_attributes(Upload))
respond_with(@upload)
end
def create
@upload = authorize Upload.new(uploader: CurrentUser.user, uploader_ip_addr: CurrentUser.ip_addr, rating: "q", tag_string: "", **permitted_attributes(Upload))
@upload.save
respond_with(@upload)
end
def batch
authorize Upload
@url = params.dig(:batch, :url) || params[:url]
@source = Sources::Strategies.find(@url, params[:ref]) if @url.present?
respond_with(@source)
end
def image_proxy
authorize Upload
resp = ImageProxy.get_image(params[:url])
send_data resp.body, type: resp.mime_type, disposition: "inline"
end
def index
@uploads = authorize Upload.visible(CurrentUser.user).paginated_search(params, count_pages: true)
@uploads = @uploads.includes(:uploader, :media_assets) if request.format.html?
respond_with(@uploads)
end
def show
@upload = authorize Upload.find(params[:id])
@post = Post.new(uploader: @upload.uploader, uploader_ip_addr: @upload.uploader_ip_addr, source: @upload.source, rating: nil, **permitted_attributes(Post))
if request.format.html? && @upload.is_completed? && @upload.media_assets.first&.post.present?
flash[:notice] = "Duplicate of post ##{@upload.media_assets.first.post.id}"
redirect_to @upload.media_assets.first.post
else
respond_with(@upload)
end
end
end