From ad998a812742178d1decc111fc8ed4694a45534d Mon Sep 17 00:00:00 2001 From: evazion Date: Mon, 31 Oct 2022 22:50:37 -0500 Subject: [PATCH] media assets: add image redirect routes. Add URLs that redirect from a media asset to the image: * https://danbooru.donmai.us/media_assets/6961761/original -> https://cdn.donmai.us/original/2c/4f/2c4ff193b0fc7106e59b2f6696a68a02.jpg * https://danbooru.donmai.us/media_assets/6961761/sample -> https://cdn.donmai.us/sample/2c/4f/sample-2c4ff193b0fc7106e59b2f6696a68a02.jpg * https://danbooru.donmai.us/media_assets/6961761/720x720 -> https://cdn.donmai.us/180x180/2c/4f/2c4ff193b0fc7106e59b2f6696a68a02.webp * https://danbooru.donmai.us/media_assets/6961761/360x360 -> https://cdn.donmai.us/180x180/2c/4f/2c4ff193b0fc7106e59b2f6696a68a02.jpg * https://danbooru.donmai.us/media_assets/6961761/180x180 -> https://cdn.donmai.us/180x180/2c/4f/2c4ff193b0fc7106e59b2f6696a68a02.jpg This is useful if you have a media asset ID and want to get to the image without looking up the image URL in the API. This endpoint is rate-limited to 5 requests per second. It's not meant to be used for things like thumbnail galleries or bulk scraping images. --- app/controllers/media_assets_controller.rb | 10 ++++++++++ app/policies/media_asset_policy.rb | 4 ++++ config/routes.rb | 4 +++- 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/app/controllers/media_assets_controller.rb b/app/controllers/media_assets_controller.rb index f7d503ac2..2f668e457 100644 --- a/app/controllers/media_assets_controller.rb +++ b/app/controllers/media_assets_controller.rb @@ -3,6 +3,8 @@ class MediaAssetsController < ApplicationController respond_to :html, :json, :xml + rate_limit :image, rate: 5.0/1.seconds, burst: 50 + def index @limit = params.fetch(:limit, CurrentUser.user.per_page).to_i.clamp(0, PostSets::Post::MAX_PER_PAGE) @preview_size = params[:size].presence || cookies[:post_preview_size].presence || MediaAssetGalleryComponent::DEFAULT_SIZE @@ -33,4 +35,12 @@ class MediaAssetsController < ApplicationController respond_with(@media_asset) end end + + def image + media_asset = authorize MediaAsset.find(params[:media_asset_id]) + variant = media_asset.variant(params[:variant]) + raise ActiveRecord::RecordNotFound if variant.nil? + + redirect_to variant.file_url + end end diff --git a/app/policies/media_asset_policy.rb b/app/policies/media_asset_policy.rb index 05e57eefd..6b704ea37 100644 --- a/app/policies/media_asset_policy.rb +++ b/app/policies/media_asset_policy.rb @@ -5,6 +5,10 @@ class MediaAssetPolicy < ApplicationPolicy true end + def image? + can_see_image? + end + def can_see_image? record.post.blank? || record.post.visible?(user) end diff --git a/config/routes.rb b/config/routes.rb index 23a618ff9..8ffb5d8e3 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -146,7 +146,9 @@ Rails.application.routes.draw do get :check, to: redirect {|path_params, req| "/iqdb_queries?#{req.query_string}"} end end - resources :media_assets, only: [:index, :show] + resources :media_assets, only: [:index, :show] do + get "/:variant", to: "media_assets#image", as: :image + end resources :media_metadata, only: [:index] resources :ai_tags, only: [:index]