5
0
Fork 0
mirror of https://0xacab.org/sutty/sutty synced 2024-07-01 20:46:07 +00:00

subir imagenes

This commit is contained in:
f 2018-07-02 19:07:06 -03:00
parent a1fe309e6a
commit 2b505cd4e8
No known key found for this signature in database
GPG key ID: F3FDAB97B5F9F7E7
6 changed files with 53 additions and 3 deletions

View file

@ -15,6 +15,25 @@ class SitesController < ApplicationController
redirect_to site_posts_path(site)
end
# Envía un archivo del directorio público de Jekyll
def send_public_file
@site = find_site
file = [params[:basename], params[:format]].join('.')
path = Pathname.new(File.join(@site.path, 'public', params[:type], file))
# TODO Verificar que no nos estén sacando archivos del sistema, como
# /etc/passwd
#
# En sí el matcher de la ruta no admite .. así que estaríamos semi
# cubiertas.
if path.exist?
send_file path, 'x-sendfile': true
else
render status: 404
end
end
def enqueue
@site = find_site
@site.enqueue!

View file

@ -201,6 +201,7 @@ class Post
t = template_fields.find { |t| t.key == k }
if t
# Subir la imagen!
# TODO pasar a su propio método
if t.image?
begin
i = Post::ImageUploader.new(site)
@ -251,9 +252,24 @@ class Post
@post <=> other.post
end
# Obtiene metadatos asegurándose que siempre trabajamos con strings
# Detecta si un valor es un archivo
def url?(name)
path = get_front_matter(name)
return false unless path.is_a? String
# El primer valor es '' porque la URL empieza con /
path.split('/').second == 'public'
end
def image?(name)
return false unless url? name
# TODO no chequear por la extensión
%[gif jpg jpeg png].include? get_front_matter(name).gsub(/.*\./, '')
end
# Obtiene metadatos de forma recursiva
def get_front_matter(name)
@front_matter.dig(name.to_s)
@front_matter.dig(*name)
end
# Como get_front_matter pero con un valor por defecto

View file

@ -274,6 +274,12 @@ class Site
end
alias :reorder_posts! :reorder_collection!
# Obtener una ruta disponible para Sutty
def get_url_for_sutty(path)
# Remover los puntos para que no nos envíen a ../../
File.join('/', 'sites', id, path.gsub('..', ''))
end
# El directorio donde se almacenan los sitios
def self.site_path
File.join(Rails.root, '_sites')

View file

@ -80,4 +80,9 @@
= data.strftime('%F')
- else
-# Texto
= data
- if @post.image? key
%img.img-fluid{src: @site.get_url_for_sutty(data)}
- elsif @post.url? key
%a{href: @site.get_url_for_sutty(data)}= data
- else
= data

View file

@ -1,3 +1,5 @@
- if @post.url? name
%img.img-fluid{src: @site.get_url_for_sutty(@post.get_front_matter(name))}
= file_field_tag field_name_for_post_as_string(name),
class: 'form-control',
required: template.required?

View file

@ -7,6 +7,8 @@ Rails.application.routes.draw do
get 'markdown', to: 'application#markdown'
get '/sites/:site_id/public/:type/:basename', to: 'sites#send_public_file'
resources :sites, only: [ :index, :show ] do
resources :posts
resources :templates