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.
This commit is contained in:
@@ -3,6 +3,8 @@
|
|||||||
class MediaAssetsController < ApplicationController
|
class MediaAssetsController < ApplicationController
|
||||||
respond_to :html, :json, :xml
|
respond_to :html, :json, :xml
|
||||||
|
|
||||||
|
rate_limit :image, rate: 5.0/1.seconds, burst: 50
|
||||||
|
|
||||||
def index
|
def index
|
||||||
@limit = params.fetch(:limit, CurrentUser.user.per_page).to_i.clamp(0, PostSets::Post::MAX_PER_PAGE)
|
@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
|
@preview_size = params[:size].presence || cookies[:post_preview_size].presence || MediaAssetGalleryComponent::DEFAULT_SIZE
|
||||||
@@ -33,4 +35,12 @@ class MediaAssetsController < ApplicationController
|
|||||||
respond_with(@media_asset)
|
respond_with(@media_asset)
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
|||||||
@@ -5,6 +5,10 @@ class MediaAssetPolicy < ApplicationPolicy
|
|||||||
true
|
true
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def image?
|
||||||
|
can_see_image?
|
||||||
|
end
|
||||||
|
|
||||||
def can_see_image?
|
def can_see_image?
|
||||||
record.post.blank? || record.post.visible?(user)
|
record.post.blank? || record.post.visible?(user)
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -146,7 +146,9 @@ Rails.application.routes.draw do
|
|||||||
get :check, to: redirect {|path_params, req| "/iqdb_queries?#{req.query_string}"}
|
get :check, to: redirect {|path_params, req| "/iqdb_queries?#{req.query_string}"}
|
||||||
end
|
end
|
||||||
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 :media_metadata, only: [:index]
|
||||||
|
|
||||||
resources :ai_tags, only: [:index]
|
resources :ai_tags, only: [:index]
|
||||||
|
|||||||
Reference in New Issue
Block a user