Files
danbooru/app/controllers/posts_controller.rb
evazion abdab7a0a8 uploads: rework upload process.
Rework the upload process so that files are saved to Danbooru first
before the user starts tagging the upload.

The main user-visible change is that you have to select the file first
before you can start tagging it. Saving the file first lets us fix a
number of problems:

* We can check for dupes before the user tags the upload.
* We can perform dupe checks and show preview images for users not using the bookmarklet.
* We can show preview images without having to proxy images through Danbooru.
* We can show previews of videos and ugoira files.
* We can reliably show the filesize and resolution of the image.
* We can let the user save files to upload later.
* We can get rid of a lot of spaghetti code related to preprocessing
  uploads. This was the cause of most weird "md5 confirmation doesn't
  match md5" errors.

(Not all of these are implemented yet.)

Internally, uploading is now a two-step process: first we create an upload
object, then we create a post from the upload. This is how it works:

* The user goes to /uploads/new and chooses a file or pastes an URL into
  the file upload component.
* The file upload component calls `POST /uploads` to create an upload.
* `POST /uploads` immediately returns a new upload object in the `pending` state.
* Danbooru starts processing the upload in a background job (downloading,
  resizing, and transferring the image to the image servers).
* The file upload component polls `/uploads/$id.json`, checking the
  upload `status` until it returns `completed` or `error`.
* When the upload status is `completed`, the user is redirected to /uploads/$id.
* On the /uploads/$id page, the user can tag the upload and submit it.
* The upload form calls `POST /posts` to create a new post from the upload.
* The user is redirected to the new post.

This is the data model:

* An upload represents a set of files uploaded to Danbooru by a user.
  Uploaded files don't have to belong to a post. An upload has an
  uploader, a status (pending, processing, completed, or error), a
  source (unless uploading from a file), and a list of media assets
  (image or video files).

* There is a has-and-belongs-to-many relationship between uploads and
  media assets. An upload can have many media assets, and a media asset
  can belong to multiple uploads. Uploads are joined to media assets
  through a upload_media_assets table.

  An upload could potentially have multiple media assets if it's a Pixiv
  or Twitter gallery. This is not yet implemented (at the moment all
  uploads have one media asset).

  A media asset can belong to multiple uploads if multiple people try
  to upload the same file, or if the same user tries to upload the same
  file more than once.

New features:

* On the upload page, you can press Ctrl+V to paste an URL and immediately upload it.
* You can save files for upload later. Your saved files are at /uploads.

Fixes:

* Improved error messages when uploading invalid files, bad URLs, and
  when forgetting the rating.
2022-01-28 04:13:22 -06:00

177 lines
5.7 KiB
Ruby

# frozen_string_literal: true
class PostsController < ApplicationController
respond_to :html, :xml, :json, :js
layout "sidebar"
before_action :log_search_query, only: :index
after_action :log_search_count, only: :index, if: -> { request.format.html? && response.successful? }
rate_limit :index, rate: 1.0/2.seconds, burst: 50, if: -> { request.format.atom? }, key: "posts:index.atom"
def index
if params[:md5].present?
@post = authorize Post.find_by!(md5: params[:md5])
respond_with(@post) do |format|
format.html { redirect_to(@post) }
end
elsif params[:random].to_s.truthy?
query = "#{post_set.normalized_query.to_s} random:#{post_set.per_page}".strip
redirect_to posts_path(tags: query, page: params[:page], limit: params[:limit], format: request.format.symbol)
else
@preview_size = params[:size].presence || cookies[:post_preview_size].presence || PostGalleryComponent::DEFAULT_SIZE
@posts = authorize post_set.posts, policy_class: PostPolicy
respond_with(@posts) do |format|
format.atom
end
end
end
def show
@post = authorize Post.find(params[:id])
if request.format.html?
include_deleted = @post.is_deleted? || (@post.parent_id.present? && @post.parent.is_deleted?) || CurrentUser.user.show_deleted_children?
@sibling_posts = @post.parent.present? ? @post.parent.children : Post.none
@sibling_posts = @sibling_posts.undeleted unless include_deleted
@sibling_posts = @sibling_posts.includes(:media_asset)
@child_posts = @post.children
@child_posts = @child_posts.undeleted unless include_deleted
@sibling_posts = @sibling_posts.includes(:media_asset)
end
respond_with(@post) do |format|
format.html.tooltip { render layout: false }
end
end
def show_seq
authorize Post
context = PostSearchContext.new(params)
if context.post_id
redirect_to(post_path(context.post_id, q: params[:q]))
else
redirect_to(post_path(params[:id], q: params[:q]))
end
end
def update
@post = authorize Post.find(params[:id])
@post.update(permitted_attributes(@post))
@show_votes = (params[:show_votes].presence || cookies[:post_preview_show_votes].presence || "false").truthy?
@preview_size = params[:size].presence || cookies[:post_preview_size].presence || PostGalleryComponent::DEFAULT_SIZE
respond_with_post_after_update(@post)
end
def create
@post = authorize Post.new_from_upload(permitted_attributes(Post))
@post.save
if @post.errors.any?
@upload = UploadMediaAsset.find(params[:post][:upload_media_asset_id]).upload
flash[:notice] = @post.errors.full_messages.join("; ")
respond_with(@post, render: { template: "uploads/show" })
else
respond_with(@post)
end
end
def destroy
@post = authorize Post.find(params[:id])
if params[:commit] == "Delete"
move_favorites = params.dig(:post, :move_favorites).to_s.truthy?
@post.delete!(params.dig(:post, :reason), move_favorites: move_favorites, user: CurrentUser.user)
flash[:notice] = "Post deleted"
end
respond_with_post_after_update(@post)
end
def revert
@post = authorize Post.find(params[:id])
@version = @post.versions.find(params[:version_id])
@post.revert_to!(@version)
respond_with(@post) do |format|
format.js
end
end
def copy_notes
@post = Post.find(params[:id])
@other_post = authorize Post.find(params[:other_post_id].to_i)
@post.copy_notes_to(@other_post)
if @post.errors.any?
@error_message = @post.errors.full_messages.join("; ")
render :json => {:success => false, :reason => @error_message}.to_json, :status => 400
else
head 204
end
end
def random
@post = Post.user_tag_match(params[:tags]).random(1).take
raise ActiveRecord::RecordNotFound if @post.nil?
authorize @post
respond_with(@post) do |format|
format.html { redirect_to post_path(@post, :tags => params[:tags]) }
end
end
def mark_as_translated
@post = authorize Post.find(params[:id])
@post.mark_as_translated(params[:post])
respond_with_post_after_update(@post)
end
private
def post_set
@post_set ||= begin
tag_query = params[:tags] || params.dig(:post, :tags)
show_votes = (params[:show_votes].presence || cookies[:post_preview_show_votes].presence || "false").truthy?
PostSets::Post.new(tag_query, params[:page], params[:limit], format: request.format.symbol, show_votes: show_votes)
end
end
def log_search_query
DanbooruLogger.add_attributes("search", {
query: post_set.normalized_query.to_s,
page: post_set.current_page,
limit: post_set.per_page,
term_count: post_set.normalized_query.terms.count,
tag_count: post_set.normalized_query.tags.count,
metatag_count: post_set.normalized_query.metatags.count,
})
end
def log_search_count
DanbooruLogger.add_attributes("search", { count: post_set.post_count, })
end
def respond_with_post_after_update(post)
respond_with(post) do |format|
format.html do
if post.warnings.any?
flash[:notice] = post.warnings.full_messages.join(".\n \n")
end
if post.errors.any?
@error_message = post.errors.full_messages.join("; ")
render :template => "static/error", :status => 500
else
response_params = {:q => params[:tags_query], :pool_id => params[:pool_id], :favgroup_id => params[:favgroup_id]}
response_params.reject! {|_key, value| value.blank?}
redirect_to post_path(post, response_params)
end
end
format.json do
render :json => post.to_json
end
end
end
end