5
0
Fork 0
mirror of https://0xacab.org/sutty/sutty synced 2024-07-03 01:16:08 +00:00
panel/app/controllers/private_controller.rb

89 lines
2 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
# 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
# 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 && File.exist?(path)
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
2020-08-29 23:21:45 +00:00
not_found_path
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
2020-08-29 23:21:45 +00:00
# Devuelve una página 404.html
def not_found_path
return site_not_found_path if File.exist? site_not_found_path
raise PageNotFound
end
def site_not_found_path
@site_not_found_path ||= File.join(deploy_private.destination, '404.html')
end
end