mirror of
https://0xacab.org/sutty/sutty
synced 2024-11-22 16:06:23 +00:00
110 lines
2.5 KiB
Ruby
110 lines
2.5 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
# Define un campo de archivo
|
|
class MetadataFile < MetadataTemplate
|
|
# Una ruta vacía a la imagen con una descripción vacía
|
|
def default_value
|
|
{ 'path' => nil, 'description' => nil }
|
|
end
|
|
|
|
def empty?
|
|
value == default_value
|
|
end
|
|
|
|
def validate
|
|
super
|
|
|
|
errors << I18n.t('metadata.image.path_required') if path_missing?
|
|
|
|
errors.compact!
|
|
errors.empty?
|
|
end
|
|
|
|
# Determina si necesitamos la imagen pero no la tenemos
|
|
def path_missing?
|
|
required && !value['path'].blank?
|
|
end
|
|
|
|
# Determina si el archivo ya fue subido
|
|
def uploaded?
|
|
value['path'].is_a?(String)
|
|
end
|
|
|
|
# Determina si la ruta es opcional pero deja pasar si la ruta se
|
|
# especifica
|
|
def path_optional?
|
|
!required && value['path'].blank?
|
|
end
|
|
|
|
# Asociar la imagen subida al sitio y obtener la ruta
|
|
def save
|
|
return true if uploaded?
|
|
return true if path_optional?
|
|
return false unless hardlink.zero?
|
|
|
|
# Modificar el valor actual
|
|
value['path'] = relative_destination_path
|
|
|
|
true
|
|
end
|
|
|
|
def to_param
|
|
{ name => %i[description path] }
|
|
end
|
|
|
|
# Almacena el archivo en el sitio y lo devuelve
|
|
#
|
|
# @return ActiveStorage::Attachment
|
|
def static_file
|
|
return @static_file if @static_file
|
|
|
|
ActiveRecord::Base.connection_pool.with_connection do
|
|
if uploaded?
|
|
blob = ActiveStorage::Blob.find_by(key: key_from_path)
|
|
@static_file ||= site.static_files.find_by(blob_id: blob.id)
|
|
elsif site.static_files.attach(value['path'])
|
|
@static_file ||= site.static_files.last
|
|
end
|
|
end
|
|
end
|
|
|
|
private
|
|
|
|
def path
|
|
@path ||= Pathname.new value['path']
|
|
end
|
|
|
|
def key_from_path
|
|
path.dirname.basename.to_s
|
|
end
|
|
|
|
# Hacemos un link duro para colocar el archivo dentro del repositorio
|
|
# y no duplicar el espacio que ocupan. Esto requiere que ambos
|
|
# directorios estén dentro del mismo punto de montaje.
|
|
def hardlink
|
|
FileUtils.mkdir_p File.dirname(destination_path)
|
|
FileUtils.ln uploaded_path, destination_path
|
|
end
|
|
|
|
def extension
|
|
@extension ||= static_file.filename.to_s.split('.').last
|
|
end
|
|
|
|
# Obtener la ruta al archivo
|
|
# https://stackoverflow.com/a/53908358
|
|
def uploaded_relative_path
|
|
ActiveStorage::Blob.service.path_for(static_file.key)
|
|
end
|
|
|
|
def uploaded_path
|
|
Rails.root.join uploaded_relative_path
|
|
end
|
|
|
|
def relative_destination_path
|
|
File.join('public', static_file.key, static_file.filename.to_s)
|
|
end
|
|
|
|
def destination_path
|
|
File.join(site.path, relative_destination_path)
|
|
end
|
|
end
|