summaryrefslogtreecommitdiff
path: root/app/controllers/oembeds_controller.rb
blob: 4b7e54f2ddcd81cd06c55563db032dc9536d1691 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# frozen_string_literal: true
require 'booru/config'

class OembedsController < ApplicationController
  skip_authorization_check

  def show
    uri = URI(URI.decode_www_form_component(params[:url].tr(' ', '+'))) rescue nil
    return head(:bad_request) unless uri

    # /img/view/2014/6/12/651690__safe_solo_pinkie+pie_solo+female_artist+needed_sneezing_allergies_pollen.png
    # /img/2014/6/11/650526/large.png
    id = uri.path.match(/\/img\/.*\/(\d+)(\.|[\/_][_\w])/)[1] rescue nil
    # /17842
    # /images/1245
    id ||= uri.path.match(/\/(\d+)/)[1] rescue nil

    @post = Post.find(id)
    name = I18n.t('booru.name')
    site = I18n.t('booru.site')

    @data = {
      version:             '1.0',
      type:                'photo',
      title:               "##{@post.id} - #{@post.tag_list} - #{name}",
      author_url:          @post.source_url.presence,
      author_name:         ((@post.tags.detect { |t| t.name.include?('artist:') }.name.split(':')[1]) rescue 'Unknown Artist'),
      provider_name:       name,
      provider_url:        "#{Booru::CONFIG.settings[:public_url_root]}/#{@post.id}",
      cache_age:           7200,
      "#{site}_id" =>       @post.id,
      "#{site}_score" =>    @post.score,
      "#{site}_comments" => @post.comments_count,
      "#{site}_tags" =>     @post.tags.pluck(:name)
    }

    if params[:maxwidth] && params[:maxheight]
      width = params[:maxwidth].to_i
      height = params[:maxheight].to_i
      @data[:thumbnail_url] = @post.media.url([width, height])
    else
      @data[:thumbnail_url] = @post.media.pretty_url
    end

    respond_to do |format|
      format.html { redirect_to post_path(@post) }
      format.json { render json: @data }
      format.xml  {}
    end
  end
end