From 2b505cd4e803c0f5f7a48b298b697c3f2ba10423 Mon Sep 17 00:00:00 2001 From: f Date: Mon, 2 Jul 2018 19:07:06 -0300 Subject: [PATCH] subir imagenes --- app/controllers/sites_controller.rb | 19 +++++++++++++++++++ app/models/post.rb | 20 ++++++++++++++++++-- app/models/site.rb | 6 ++++++ app/views/posts/show.haml | 7 ++++++- app/views/posts/template_field/_image.haml | 2 ++ config/routes.rb | 2 ++ 6 files changed, 53 insertions(+), 3 deletions(-) diff --git a/app/controllers/sites_controller.rb b/app/controllers/sites_controller.rb index ab85343..55e5643 100644 --- a/app/controllers/sites_controller.rb +++ b/app/controllers/sites_controller.rb @@ -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! diff --git a/app/models/post.rb b/app/models/post.rb index 02c1e38..bb82e55 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -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 diff --git a/app/models/site.rb b/app/models/site.rb index a3c3d8c..8e16ebc 100644 --- a/app/models/site.rb +++ b/app/models/site.rb @@ -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') diff --git a/app/views/posts/show.haml b/app/views/posts/show.haml index 70d1321..32f3951 100644 --- a/app/views/posts/show.haml +++ b/app/views/posts/show.haml @@ -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 diff --git a/app/views/posts/template_field/_image.haml b/app/views/posts/template_field/_image.haml index 2aa6c64..534adc7 100644 --- a/app/views/posts/template_field/_image.haml +++ b/app/views/posts/template_field/_image.haml @@ -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? diff --git a/config/routes.rb b/config/routes.rb index 1924dd7..126f584 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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