Files
danbooru/app/controllers/wiki_pages_controller.rb
evazion 0db20e0cab Fix #4591: Wiki pages with filename-like name are broken by default.
Fix wiki pages like this returning 406 errors:

* https://danbooru.donmai.us/wiki_pages/rnd.jpg

Caused by Rails parsing the .jpg part as a file extension and trying to
return a JPEG in response. This happens deep in Rails' MIME negotiation
code, so it's hard to override. The fix is to pass `format: false` in
the route to disable all special handling of file extensions by Rails,
and then handle it ourselves in the controller. Ugly.

This only affected two tags: `rnd.jpg` and `haru.jpg`.
2022-01-19 21:44:40 -06:00

108 lines
2.7 KiB
Ruby

# frozen_string_literal: true
class WikiPagesController < ApplicationController
respond_to :html, :xml, :json, :js
layout "sidebar"
def new
@wiki_page = authorize WikiPage.new(permitted_attributes(WikiPage))
respond_with(@wiki_page)
end
def edit
@wiki_page, _found_by = WikiPage.find_by_id_or_title(params[:id])
authorize @wiki_page
respond_with(@wiki_page)
end
def index
if params[:title].present?
redirect_to wiki_pages_path(search: { title_normalize: params[:title] }, redirect: true)
else
@wiki_pages = authorize WikiPage.paginated_search(params)
respond_with(@wiki_pages)
end
end
def search
authorize WikiPage
render layout: "default"
end
def show
if params[:format].present?
request.format = params[:format]
elsif params[:id].ends_with?(".html", ".json", ".xml")
request.format = params[:id].split(".").last
params[:id].delete_suffix!(".#{request.format.symbol}")
else
request.format = "html"
end
@wiki_page, found_by = WikiPage.find_by_id_or_title(params[:id])
if request.format.html? && @wiki_page.blank? && found_by == :title
@wiki_page = WikiPage.new(title: params[:id])
respond_with @wiki_page, status: 404
elsif request.format.html? && @wiki_page.present? && found_by == :id
redirect_to @wiki_page
elsif @wiki_page.blank?
raise ActiveRecord::RecordNotFound
else
respond_with(@wiki_page)
end
end
def create
@wiki_page = authorize WikiPage.new(permitted_attributes(WikiPage))
@wiki_page.save
respond_with(@wiki_page)
end
def update
@wiki_page, _found_by = WikiPage.find_by_id_or_title(params[:id])
authorize @wiki_page
@wiki_page.update(permitted_attributes(@wiki_page))
flash[:notice] = @wiki_page.warnings.full_messages.join(".\n \n") if @wiki_page.warnings.any?
respond_with(@wiki_page)
end
def destroy
@wiki_page, _found_by = WikiPage.find_by_id_or_title(params[:id])
authorize @wiki_page
@wiki_page.update(is_deleted: true)
respond_with(@wiki_page)
end
def revert
@wiki_page, _found_by = WikiPage.find_by_id_or_title(params[:id])
authorize @wiki_page
@version = @wiki_page.versions.find(params[:version_id])
@wiki_page.revert_to!(@version)
flash[:notice] = "Page was reverted"
respond_with(@wiki_page)
end
def show_or_new
if params[:title].blank?
redirect_to new_wiki_page_path(permitted_attributes(WikiPage))
else
redirect_to wiki_page_path(params[:title])
end
end
private
def item_matches_params(wiki_page)
if params[:search][:title_normalize]
wiki_page.title == WikiPage.normalize_title(params[:search][:title_normalize])
else
true
end
end
end