ファイル
btrc-hub/backend/app/controllers/static_mirrors_controller.rb
T

55 行
1.8 KiB
Ruby

class StaticMirrorsController < ApplicationController
before_action :development_only
wrap_parameters false
def show
render json: { resource_limit: StaticMirror::RESOURCE_LIMIT,
name_limit: StaticMirror::NAME_LIMIT, max_bytes: StaticMirror::MAX_BYTES,
max_seconds: StaticMirrorJob::MAX_SECONDS, concurrency: 1,
cooldown_seconds: StaticMirrorJob::COOLDOWN,
expiry_seconds: StaticMirrorJob::EXPIRY.to_i, exclusions: StaticMirror::EXCLUSIONS }
end
def create
# No date input is accepted: this endpoint cannot promise historical restoration.
payload = request.request_parameters.except('authenticity_token')
.merge(request.query_parameters)
return head :bad_request unless (payload.keys - ['locale']).empty?
locale = resolve_locale!
session[:static_mirror_owner] ||= SecureRandom.hex(32)
job = StaticMirrorJob.start(owner: session[:static_mirror_owner], locale: locale.code)
render json: { job: }, status: :accepted
rescue StaticMirrorJob::Busy
response.headers['Retry-After'] = StaticMirrorJob::COOLDOWN.to_s
head :too_many_requests
rescue ActiveJob::EnqueueError
render json: { error: 'generation_failed' }, status: :service_unavailable
end
def status
value = StaticMirrorJob.status(params[:id], session[:static_mirror_owner])
return head :not_found unless value
render json: value
rescue StaticMirrorJob::Busy
head :service_unavailable
end
def download
bytes = StaticMirrorJob.download(params[:id], session[:static_mirror_owner])
return head :not_found unless bytes
send_data bytes, filename: 'static-mirror.zip', type: 'application/zip',
disposition: 'attachment'
rescue StaticMirrorJob::Busy
head :service_unavailable
end
private
def development_only
head :not_found unless Rails.env.development?
end
end