mirror of
https://0xacab.org/sutty/sutty
synced 2024-11-15 02:21:42 +00:00
77 lines
1.7 KiB
Ruby
77 lines
1.7 KiB
Ruby
|
# Gestiona las versiones privadas de los sitios. Solo se puede acceder
|
||
|
# con una cuenta
|
||
|
class PrivateController < ApplicationController
|
||
|
# XXX: Permite ejecutar JS
|
||
|
skip_forgery_protection
|
||
|
|
||
|
include Pundit
|
||
|
rescue_from Pundit::NilPolicyError, with: :page_not_found
|
||
|
|
||
|
# Enviar el archivo si existe, agregar una / al final siempre para no
|
||
|
# romper las direcciones relativas.
|
||
|
def show
|
||
|
authorize site
|
||
|
|
||
|
# Detectar si necesitamos una / al final
|
||
|
if needs_trailing_slash?
|
||
|
redirect_to request.url + '/'
|
||
|
return
|
||
|
end
|
||
|
|
||
|
if deploy_private
|
||
|
send_file path, disposition: 'inline'
|
||
|
else
|
||
|
head :not_found
|
||
|
end
|
||
|
end
|
||
|
|
||
|
private
|
||
|
|
||
|
# Detects if the URL should have a trailing slash
|
||
|
def needs_trailing_slash?
|
||
|
!trailing_slash? && params[:format].blank?
|
||
|
end
|
||
|
|
||
|
def trailing_slash?
|
||
|
request.env['REQUEST_URI'].ends_with?('/')
|
||
|
end
|
||
|
|
||
|
def site
|
||
|
@site ||= find_site
|
||
|
end
|
||
|
|
||
|
def deploy_private
|
||
|
@deploy_private ||= site.deploys.find_by(type: 'DeployPrivate')
|
||
|
end
|
||
|
|
||
|
# Devuelve la ruta completa del archivo
|
||
|
def path
|
||
|
return @path if @path
|
||
|
|
||
|
@path = Pathname.new(File.join(deploy_private.destination, file)).realpath.to_s
|
||
|
|
||
|
raise Errno::ENOENT unless @path.starts_with? deploy_private.destination
|
||
|
|
||
|
@path
|
||
|
rescue Errno::ENOENT
|
||
|
File.join(deploy_private.destination, '404.html')
|
||
|
end
|
||
|
|
||
|
# Devuelve la ruta del archivo, limpieza copiada desde Jekyll
|
||
|
#
|
||
|
# @see Jekyll::URL#sanitize_url
|
||
|
def file
|
||
|
return @file if @file
|
||
|
|
||
|
@file = params[:file] || '/'
|
||
|
@file += '/' if trailing_slash?
|
||
|
@file += if @file.ends_with? '/'
|
||
|
'index.html'
|
||
|
else
|
||
|
'.' + params[:format].to_s
|
||
|
end
|
||
|
|
||
|
@file = @file.gsub('..', '/').gsub('./', '').squeeze('/')
|
||
|
end
|
||
|
end
|