add tags to post file urls

This commit is contained in:
r888888888
2016-05-23 11:50:26 -07:00
parent 8bf743810c
commit a90750f5e1
3 changed files with 47 additions and 45 deletions

View File

@@ -99,17 +99,31 @@ class Post < ActiveRecord::Base
end
def file_url
"/data/#{file_path_prefix}#{md5}.#{file_ext}"
if Danbooru.config.enable_seo_post_urls
# the seo tags should be stripped out by nginx
"/data/--#{seo_tags}--#{file_path_prefix}#{md5}.#{file_ext}"
else
"/data/#{file_path_prefix}#{md5}.#{file_ext}"
end
end
def large_file_url
if has_large?
"/data/sample/#{file_path_prefix}#{Danbooru.config.large_image_prefix}#{md5}.#{large_file_ext}"
if Danbooru.config.enable_seo_post_urls
# the seo tags should be stripped out by nginx
"/data/sample/--#{seo_tags}--#{file_path_prefix}#{Danbooru.config.large_image_prefix}#{md5}.#{large_file_ext}"
else
"/data/sample/#{file_path_prefix}#{Danbooru.config.large_image_prefix}#{md5}.#{large_file_ext}"
end
else
file_url
end
end
def seo_tags
@seo_tags ||= humanized_essential_tag_string.gsub(/[^a-z0-9]+/, "-").gsub(/(^-+)|(-+$)/, "")
end
def preview_file_url
if !has_preview?
return "/images/download-preview.png"
@@ -778,26 +792,37 @@ class Post < ActiveRecord::Base
end
end
def essential_tag_string
tag_array.each do |tag|
if tag_categories[tag] == Danbooru.config.tag_category_mapping["copyright"]
return tag
end
end
def humanized_essential_tag_string
@humanized_essential_tag_string ||= begin
string = []
tag_array.each do |tag|
if tag_categories[tag] == Danbooru.config.tag_category_mapping["character"]
return tag
if character_tags.any?
chartags = character_tags.slice(0, 5)
if character_tags.length > 5
chartags << "others"
end
chartags = chartags.map do |tag|
tag.match(/^(.+?)(?:_\(.+\))?$/)[1]
end
string << chartags.to_sentence
end
end
tag_array.each do |tag|
if tag_categories[tag] == Danbooru.config.tag_category_mapping["artist"]
return tag
if copyright_tags.any?
copytags = copyright_tags.slice(0, 5)
if copyright_tags.length > 5
copytags << "others"
end
copytags = copytags.to_sentence
string << (character_tags.any? ? "(#{copytags})" : copytags)
end
end
return tag_array.first
if artist_tags_excluding_hidden.any?
string << "drawn by"
string << artist_tags_excluding_hidden.to_sentence
end
string.empty? ? "##{id}" : string.join(" ").tr("_", " ")
end
end
def tag_string_copyright

View File

@@ -104,34 +104,7 @@ class PostPresenter < Presenter
end
def humanized_essential_tag_string
string = []
if @post.character_tags.any?
chartags = @post.character_tags.slice(0, 5)
if @post.character_tags.length > 5
chartags << "others"
end
chartags = chartags.map do |tag|
tag.match(/^(.+?)(?:_\(.+\))?$/)[1]
end
string << chartags.to_sentence
end
if @post.copyright_tags.any?
copytags = @post.copyright_tags.slice(0, 5)
if @post.copyright_tags.length > 5
copytags << "others"
end
copytags = copytags.to_sentence
string << (@post.character_tags.any? ? "(#{copytags})" : copytags)
end
if @post.artist_tags_excluding_hidden.any?
string << "drawn by"
string << @post.artist_tags_excluding_hidden.to_sentence
end
string.empty? ? "##{@post.id}" : string.join(" ").tr("_", " ")
@post.humanized_essential_tag_string
end
def categorized_tag_string

View File

@@ -396,6 +396,10 @@ module Danbooru
def addthis_key
end
def enable_seo_post_urls
false
end
# enable some (donmai-specific) optimizations for post counts
def estimate_post_counts
true