mirror of
https://0xacab.org/sutty/sutty
synced 2025-03-14 17:58:16 +00:00
Merge branch 'issue-15093' into 'rails'
fix: incluir archivos renombrados See merge request sutty/sutty!242
This commit is contained in:
commit
0d21451601
4 changed files with 91 additions and 11 deletions
|
@ -1,6 +1,7 @@
|
||||||
AllCops:
|
AllCops:
|
||||||
TargetRubyVersion: '2.7'
|
TargetRubyVersion: "3.1"
|
||||||
NewCops: enable
|
NewCops: "enable"
|
||||||
|
|
||||||
Style/AsciiComments:
|
Style/AsciiComments:
|
||||||
Enabled: false
|
Enabled: false
|
||||||
|
Style/MultilineBlockChain:
|
||||||
|
Enabled: false
|
||||||
|
|
|
@ -15,6 +15,22 @@ module ActiveStorage
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Obtiene Blob#key desde la ubicación, la inversa de path_for
|
||||||
|
#
|
||||||
|
# @param :path [String,Pathname]
|
||||||
|
# @return [String]
|
||||||
|
def key_from_path(path)
|
||||||
|
Pathname.new(path).dirname.basename.to_s
|
||||||
|
end
|
||||||
|
|
||||||
|
# Elimina el archivo
|
||||||
|
#
|
||||||
|
# @param :key [String]
|
||||||
|
# @return [nil]
|
||||||
|
def delete(key)
|
||||||
|
FileUtils.rm_f(path_for(key))
|
||||||
|
end
|
||||||
|
|
||||||
# Solo copiamos el archivo si no existe
|
# Solo copiamos el archivo si no existe
|
||||||
#
|
#
|
||||||
# @param :key [String]
|
# @param :key [String]
|
||||||
|
@ -87,7 +103,15 @@ module ActiveStorage
|
||||||
# @param :key [String]
|
# @param :key [String]
|
||||||
# @return [String]
|
# @return [String]
|
||||||
def path_for(key)
|
def path_for(key)
|
||||||
File.join root, folder_for(key), filename_for(key)
|
File.join absolute_path_for(key), filename_for(key)
|
||||||
|
end
|
||||||
|
|
||||||
|
# La ruta absoluta del directorio donde se almacena el archivo
|
||||||
|
#
|
||||||
|
# @param :key [String]
|
||||||
|
# @return [String]
|
||||||
|
def absolute_path_for(key)
|
||||||
|
File.join root, folder_for(key)
|
||||||
end
|
end
|
||||||
|
|
||||||
# @return [Site]
|
# @return [Site]
|
||||||
|
|
|
@ -88,7 +88,7 @@ class MetadataFile < MetadataTemplate
|
||||||
#
|
#
|
||||||
# @return [String]
|
# @return [String]
|
||||||
def key_from_path
|
def key_from_path
|
||||||
@key_from_path ||= pathname.dirname.basename.to_s
|
@key_from_path ||= site.blob_service.key_from_path(path: pathname)
|
||||||
end
|
end
|
||||||
|
|
||||||
def path?
|
def path?
|
||||||
|
|
|
@ -10,10 +10,13 @@ class Site
|
||||||
included do
|
included do
|
||||||
has_many :indexed_posts, dependent: :destroy
|
has_many :indexed_posts, dependent: :destroy
|
||||||
|
|
||||||
MODIFIED_STATUSES = %i[added modified].freeze
|
MODIFIED_STATUSES = %i[added modified renamed].freeze
|
||||||
DELETED_STATUSES = %i[deleted].freeze
|
|
||||||
LOCALE_FROM_PATH = /\A_/.freeze
|
LOCALE_FROM_PATH = /\A_/.freeze
|
||||||
|
|
||||||
|
def blob_service
|
||||||
|
@blob_service ||= ActiveStorage::Service::JekyllService.build_for_site(site: self)
|
||||||
|
end
|
||||||
|
|
||||||
def index_posts!
|
def index_posts!
|
||||||
Site.transaction do
|
Site.transaction do
|
||||||
docs.each(&:index!)
|
docs.each(&:index!)
|
||||||
|
@ -67,7 +70,7 @@ class Site
|
||||||
|
|
||||||
# Obtiene todos los archivos a reindexar
|
# Obtiene todos los archivos a reindexar
|
||||||
#
|
#
|
||||||
# @return [Array<Rugged::Delta>]
|
# @return [Array<Rugged::Diff::Delta>]
|
||||||
def indexable_posts
|
def indexable_posts
|
||||||
@indexable_posts ||=
|
@indexable_posts ||=
|
||||||
diff_with_head.each_delta.select do |delta|
|
diff_with_head.each_delta.select do |delta|
|
||||||
|
@ -77,12 +80,25 @@ class Site
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Encuentra todos los archivos estáticos a reindexar, si fueron
|
||||||
|
# borrados necesitamos saber la ubicación anterior, si son nuevos,
|
||||||
|
# la nueva.
|
||||||
|
#
|
||||||
|
# @return [Array<Rugged::Diff::Delta>]
|
||||||
|
def indexable_static_files
|
||||||
|
@indexable_static_files ||=
|
||||||
|
diff_with_head.each_delta.select do |delta|
|
||||||
|
(
|
||||||
|
delta.status == :deleted &&
|
||||||
|
delta.old_file[:path].start_with?('public/')
|
||||||
|
) || delta.new_file[:path].start_with?('public/')
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
# Elimina los artículos eliminados o que cambiaron de ubicación
|
# Elimina los artículos eliminados o que cambiaron de ubicación
|
||||||
# del índice
|
# del índice
|
||||||
def remove_deleted_posts!
|
def remove_deleted_posts!
|
||||||
indexable_posts.select do |delta|
|
indexable_posts.select(&:deleted?).each do |delta|
|
||||||
DELETED_STATUSES.include? delta.status
|
|
||||||
end.each do |delta|
|
|
||||||
locale, path = locale_and_path_from(delta.old_file[:path])
|
locale, path = locale_and_path_from(delta.old_file[:path])
|
||||||
|
|
||||||
indexed_posts.destroy_by(locale: locale, path: path).tap do |destroyed_posts|
|
indexed_posts.destroy_by(locale: locale, path: path).tap do |destroyed_posts|
|
||||||
|
@ -93,6 +109,45 @@ class Site
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Elimina de ActiveStorage los archivos borrados, renombrados o
|
||||||
|
# modificados, para poder recalcular su contenido y nueva
|
||||||
|
# ubicación.
|
||||||
|
#
|
||||||
|
# Los renombrados o modificados se vuelven a crear en
|
||||||
|
# reindex_modified_static_files!
|
||||||
|
def remove_deleted_or_renamed_static_files!
|
||||||
|
indexable_static_files.select do |delta|
|
||||||
|
delta.deleted? || delta.modified? || delta.renamed?
|
||||||
|
end.each do |delta|
|
||||||
|
key = blob_service.key_from_path(delta.old_file[:path])
|
||||||
|
|
||||||
|
static_files.blobs.find_by(service_name: name, key: key).tap do |blob|
|
||||||
|
next unless blob
|
||||||
|
|
||||||
|
transaction do
|
||||||
|
static_files.where(blob_id: blob.id).destroy_all
|
||||||
|
blob.destroy
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# Reindexa los archivos estáticos modificados
|
||||||
|
def reindex_modified_static_files!
|
||||||
|
indexable_static_files.select do |delta|
|
||||||
|
MODIFIED_STATUSES.include?(delta.status) || delta.renamed?
|
||||||
|
end.each do |delta|
|
||||||
|
transaction do
|
||||||
|
path = blob_service.absolute_path_for(delta.new_file[:path].sub(%r{\Apublic/}, ''))
|
||||||
|
key = blob_service.key_from_path(path)
|
||||||
|
pathname = Pathname.new(path)
|
||||||
|
blob = ActiveStorage::Blob.create_after_unfurling!(key: key, io: pathname.open, filename: pathname.basename, service_name: name)
|
||||||
|
|
||||||
|
ActiveStorage::Attachment.create!(name: 'static_files', record: self, blob: blob)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
# Reindexa artículos que cambiaron de ubicación, se agregaron
|
# Reindexa artículos que cambiaron de ubicación, se agregaron
|
||||||
# o fueron modificados
|
# o fueron modificados
|
||||||
def reindex_modified_posts!
|
def reindex_modified_posts!
|
||||||
|
|
Loading…
Reference in a new issue