5
0
Fork 0
mirror of https://0xacab.org/sutty/sutty synced 2024-11-16 17:46:21 +00:00

obtener el archivo subido

This commit is contained in:
f 2022-03-04 19:21:03 -03:00
parent 37deb36178
commit 03d9913f58

View file

@ -66,18 +66,19 @@ class MetadataFile < MetadataTemplate
# XXX: La última opción provoca archivos duplicados, pero es lo mejor
# que tenemos hasta que resolvamos https://0xacab.org/sutty/sutty/-/issues/213
#
# @return [ActiveStorage::Attachment]
# @todo encontrar una forma de obtener el attachment sin tener que
# recurrir al último subido.
#
# @return [ActiveStorage::Attachment,nil]
def static_file
return unless path?
@static_file ||=
case value['path']
when ActionDispatch::Http::UploadedFile
site.static_files.last if site.static_files.attach(value['path'])
when String
if (blob = ActiveStorage::Blob.where(key: key_from_path).pluck(:id).first)
site.static_files.find_by(blob_id: blob)
elsif site.static_files.attach(io: path.open, filename: path.basename)
if (blob_id = ActiveStorage::Blob.where(key: key_from_path).pluck(:id).first)
site.static_files.find_by(blob_id: blob_id)
elsif path? && pathname.exist? && site.static_files.attach(io: pathname.open, filename: pathname.basename)
site.static_files.last
end
end
@ -92,8 +93,11 @@ class MetadataFile < MetadataTemplate
@pathname ||= Pathname.new(File.join(site.path, value['path']))
end
# Obtiene la key del attachment a partir de la ruta
#
# @return [String]
def key_from_path
path.dirname.basename.to_s
pathname.dirname.basename.to_s
end
def path?