* Add HtmlCssToImage for generating images This will be used by social_previews to convert the templates into images. Setup a fallback image primarily for local dev, so that this won't cause errors if authentication is missing. Caching is setup to use contents of the html/css. So it with autobust/generate a new image whenever the markup changes. * Update Article social preview template to not be random + Being random breaks caching (since we generate cachekey by a SHA of html content) + Use Roboto for when generating via HTML/CSS to Image * Update Tag social preview template to not be random + Make rand deterministic so it doesn't break caching * Update user social preview template for html/csstoimage * Update shecoded template to work with both url2png and htmlcsstoimage * Add ability to respond to .png for social previews controller When adding .png to the url, these routes will now generate an image and redirect to it. This leaves the existing pages as they were, so we're backwards compatible with all the existing links out in the world. * Fix merge conflicts * Remove some duplication * Improve spec descriptions/read better * clean up setting template with a ternary
65 lines
1.9 KiB
Ruby
65 lines
1.9 KiB
Ruby
class SocialPreviewsController < ApplicationController
|
|
# No authorization required for entirely public controller
|
|
|
|
PNG_CSS = "body { transform: scale(0.3); } .preview-div-wrapper { overflow: unset; margin: 5vw; }".freeze
|
|
SHE_CODED_TAGS = %w[shecoded theycoded shecodedally].freeze
|
|
|
|
def article
|
|
@article = Article.find(params[:id])
|
|
not_found unless @article.published
|
|
|
|
template = (@article.decorate.cached_tag_list_array & SHE_CODED_TAGS).any? ? "shecoded" : "article"
|
|
|
|
respond_to do |format|
|
|
format.html do
|
|
render template, layout: false
|
|
end
|
|
format.png do
|
|
html = render_to_string(template, formats: :html, layout: false)
|
|
redirect_to HtmlCssToImage.fetch_url(html: html, css: PNG_CSS, google_fonts: "Roboto|Roboto+Condensed"), status: 302
|
|
end
|
|
end
|
|
end
|
|
|
|
def user
|
|
@user = User.find(params[:id]) || not_found
|
|
|
|
respond_to do |format|
|
|
format.html do
|
|
render layout: false
|
|
end
|
|
format.png do
|
|
html = render_to_string(formats: :html, layout: false)
|
|
redirect_to HtmlCssToImage.fetch_url(html: html, css: PNG_CSS, google_fonts: "Roboto"), status: 302
|
|
end
|
|
end
|
|
end
|
|
|
|
def organization
|
|
@user = Organization.find(params[:id]) || not_found
|
|
|
|
respond_to do |format|
|
|
format.html do
|
|
render "user", layout: false
|
|
end
|
|
format.png do
|
|
html = render_to_string("user", formats: :html, layout: false)
|
|
redirect_to HtmlCssToImage.fetch_url(html: html, css: PNG_CSS, google_fonts: "Roboto"), status: 302
|
|
end
|
|
end
|
|
end
|
|
|
|
def tag
|
|
@tag = Tag.find(params[:id]) || not_found
|
|
|
|
respond_to do |format|
|
|
format.html do
|
|
render layout: false
|
|
end
|
|
format.png do
|
|
html = render_to_string(formats: :html, layout: false)
|
|
redirect_to HtmlCssToImage.fetch_url(html: html, css: PNG_CSS, google_fonts: "Roboto"), status: 302
|
|
end
|
|
end
|
|
end
|
|
end
|