2018-07-02 20:45:32 +00:00
|
|
|
# Una clase que permite adjuntar imágenes a artículos
|
|
|
|
class Post::ImageUploader < CarrierWave::Uploader::Base
|
2018-10-02 18:49:07 +00:00
|
|
|
include CarrierWave::MiniMagick
|
2018-07-02 20:45:32 +00:00
|
|
|
|
|
|
|
attr_accessor :site
|
|
|
|
|
2018-10-02 18:49:07 +00:00
|
|
|
process :strip
|
|
|
|
process resize_to_limit: [1280, 1280]
|
|
|
|
|
|
|
|
version :thumb do
|
|
|
|
process :strip
|
|
|
|
process resize_to_fit: [200,200]
|
|
|
|
end
|
|
|
|
|
|
|
|
def strip
|
|
|
|
manipulate! do |img|
|
|
|
|
img.strip
|
|
|
|
img
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-07-02 20:45:32 +00:00
|
|
|
# Necesitamos pasar el sitio para poder acceder a los archivos locales
|
2018-08-22 21:37:44 +00:00
|
|
|
def initialize(site, wtf = nil)
|
2018-07-02 20:45:32 +00:00
|
|
|
super
|
|
|
|
|
|
|
|
@site = site
|
|
|
|
end
|
|
|
|
|
|
|
|
# Solo aceptamos imágenes
|
|
|
|
def content_type_whitelist
|
|
|
|
/\Aimage\//
|
|
|
|
end
|
|
|
|
|
|
|
|
# Devuelve la ubicación del directorio dentro de Jekyll, para eso,
|
|
|
|
# tenemos que tener acceso al sitio que estamos editando
|
|
|
|
def store_dir
|
|
|
|
File.join site.path, 'public', 'images'
|
|
|
|
end
|
|
|
|
|
|
|
|
# Almacenar en el 'tmp' local
|
|
|
|
def cache_dir
|
|
|
|
File.join Rails.root, 'tmp', 'images'
|
|
|
|
end
|
|
|
|
|
|
|
|
# XXX los nombres de los archivos siempre son únicos, no chequeamos si
|
|
|
|
# están repetidos.
|
|
|
|
def filename
|
2018-09-28 17:14:43 +00:00
|
|
|
@unique_filename ||= if /\A[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\..*\Z/ =~ file.try(:filename)
|
|
|
|
file.try(:filename)
|
|
|
|
else
|
|
|
|
[SecureRandom.uuid, '.', file.extension].join
|
|
|
|
end
|
2018-07-02 20:45:32 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Obtener la URL dentro del proyecto de Jekyll
|
|
|
|
def url
|
|
|
|
CGI.unescape(super).remove site.path
|
|
|
|
end
|
|
|
|
end
|