2019-11-14 17:27:24 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class Site
|
|
|
|
# Obtiene todos los archivos relacionados en artículos del sitio y los
|
2021-02-23 22:00:38 +00:00
|
|
|
# sube a Sutty.
|
2019-11-14 17:27:24 +00:00
|
|
|
class StaticFileMigration
|
|
|
|
# Tipos de metadatos que contienen archivos
|
2021-02-23 22:00:38 +00:00
|
|
|
STATIC_TYPES = %i[file image].freeze
|
2019-11-14 17:27:24 +00:00
|
|
|
|
|
|
|
attr_reader :site
|
|
|
|
|
|
|
|
def initialize(site:)
|
|
|
|
@site = site
|
|
|
|
end
|
|
|
|
|
|
|
|
def migrate!
|
2021-02-23 22:00:38 +00:00
|
|
|
modified = site.docs.map do |doc|
|
|
|
|
next unless STATIC_TYPES.map do |field|
|
|
|
|
next unless doc.attribute? field
|
|
|
|
next unless doc[field].path?
|
|
|
|
next unless doc[field].static_file
|
2019-11-14 17:27:24 +00:00
|
|
|
|
2021-02-23 22:00:38 +00:00
|
|
|
true
|
|
|
|
end.any?
|
2019-11-14 17:27:24 +00:00
|
|
|
|
2021-02-23 22:00:38 +00:00
|
|
|
log.write "#{doc.path.relative};no se pudo guardar\n" unless doc.save(validate: false)
|
2019-11-14 17:27:24 +00:00
|
|
|
|
2021-02-23 22:00:38 +00:00
|
|
|
doc.path.absolute
|
|
|
|
end.compact
|
2019-11-14 17:27:24 +00:00
|
|
|
|
|
|
|
log.close
|
|
|
|
|
2020-11-11 18:31:15 +00:00
|
|
|
return if modified.empty?
|
|
|
|
|
2019-11-14 17:27:24 +00:00
|
|
|
# TODO: Hacer la migración desde el servicio de creación de sitios?
|
|
|
|
site.repository.commit(file: modified,
|
|
|
|
message: I18n.t('sites.static_file_migration'),
|
|
|
|
usuarie: author)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def author
|
2020-06-16 22:10:54 +00:00
|
|
|
@author ||= GitAuthor.new email: "sutty@#{Site.domain}",
|
|
|
|
name: 'Sutty'
|
2019-11-14 17:27:24 +00:00
|
|
|
end
|
|
|
|
|
2021-02-23 22:00:38 +00:00
|
|
|
def log
|
|
|
|
@log ||= File.open(File.join(site.path, 'migration.csv'), 'w')
|
2019-11-14 17:27:24 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|