5
0
Fork 0
mirror of https://0xacab.org/sutty/sutty synced 2024-11-24 05:36:23 +00:00

Merge branch 'jekyll-service' into panel.sutty.nl

This commit is contained in:
f 2022-07-15 19:33:52 -03:00
commit 7c3f88606b

View file

@ -68,18 +68,8 @@ class MetadataFile < MetadataTemplate
when ActionDispatch::Http::UploadedFile when ActionDispatch::Http::UploadedFile
site.static_files.last if site.static_files.attach(value['path']) site.static_files.last if site.static_files.attach(value['path'])
when String when String
if (blob_id = ActiveStorage::Blob.where(key: key_from_path, service_name: site.name).pluck(:id).first) site.static_files.find_by(blob_id: blob_id) || migrate_static_file!
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.tap do |s|
s.blob.update(key: key_from_path)
end end
else
raise ArgumentError, 'No se pudo subir el archivo'
end
end
rescue ArgumentError => e
ExceptionNotifier.notify_exception(e, data: { site: site.name, path: value['path'] })
end end
# Obtiene la ruta absoluta al archivo # Obtiene la ruta absoluta al archivo
@ -95,7 +85,7 @@ class MetadataFile < MetadataTemplate
# #
# @return [String] # @return [String]
def key_from_path def key_from_path
pathname.dirname.basename.to_s @key_from_path ||= pathname.dirname.basename.to_s
end end
def path? def path?
@ -148,4 +138,32 @@ class MetadataFile < MetadataTemplate
def no_file_for_description? def no_file_for_description?
!path? && description? !path? && description?
end end
# Obtiene el id del blob asociado
#
# @return [Integer,nil]
def blob_id
@blob_id ||= ActiveStorage::Blob.where(key: key_from_path, service_name: site.name).pluck(:id).first
end
# Genera el blob para un archivo que ya se encuentra en el
# repositorio y lo agrega a la base de datos.
#
# @return [ActiveStorage::Attachment]
def migrate_static_file!
raise ArgumentError, 'El archivo no existe' unless path? && pathname.exist?
Site.transaction do
blob =
ActiveStorage::Blob.create_after_unfurling!(key: key_from_path,
io: pathname.open,
filename: pathname.basename,
service_name: site.name)
ActiveStorage::Attachment.create!(name: 'static_files', record: site, blob: blob)
end
rescue ArgumentError => e
ExceptionNotifier.notify_exception(e, data: { site: site.name, path: value['path'] })
nil
end
end end