2022-03-04 22:08:09 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module ActiveStorage
|
2022-03-05 23:08:55 +00:00
|
|
|
class Service
|
2022-03-04 22:08:09 +00:00
|
|
|
# Sube los archivos a cada repositorio y los agrega al LFS de su
|
|
|
|
# repositorio git.
|
|
|
|
class JekyllService < Service::DiskService
|
2022-03-05 23:09:09 +00:00
|
|
|
# Genera un servicio para un sitio determinado
|
2022-03-06 19:00:21 +00:00
|
|
|
#
|
|
|
|
# @param :site [Site]
|
|
|
|
# @return [ActiveStorage::Service::JekyllService]
|
2022-03-05 23:09:09 +00:00
|
|
|
def self.build_for_site(site:)
|
|
|
|
new(root: File.join(site.path, 'public'), public: true).tap do |js|
|
|
|
|
js.name = site.name.to_sym
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-15 21:22:06 +00:00
|
|
|
# Solo copiamos el archivo si no existe
|
|
|
|
#
|
|
|
|
# @param :key [String]
|
|
|
|
# @param :io [IO]
|
|
|
|
# @param :checksum [String]
|
|
|
|
def upload(key, io, checksum: nil, **)
|
|
|
|
instrument :upload, key: key, checksum: checksum do
|
2023-03-25 18:22:54 +00:00
|
|
|
unless exist?(key)
|
|
|
|
IO.copy_stream(io, make_path_for(key))
|
2023-03-25 18:29:49 +00:00
|
|
|
LfsObjectService.new(site: site, blob: blob).process
|
2023-03-25 18:22:54 +00:00
|
|
|
end
|
2022-07-15 21:22:06 +00:00
|
|
|
ensure_integrity_of(key, checksum) if checksum
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-03-06 18:28:25 +00:00
|
|
|
# Lo mismo que en DiskService agregando el nombre de archivo en la
|
|
|
|
# firma. Esto permite que luego podamos guardar el archivo donde
|
|
|
|
# corresponde.
|
2022-03-06 19:00:21 +00:00
|
|
|
#
|
|
|
|
# @param :key [String]
|
|
|
|
# @param :expires_in [Integer]
|
|
|
|
# @param :content_type [String]
|
|
|
|
# @param :content_length [Integer]
|
|
|
|
# @param :checksum [String]
|
|
|
|
# @return [String]
|
2022-03-06 18:28:25 +00:00
|
|
|
def url_for_direct_upload(key, expires_in:, content_type:, content_length:, checksum:)
|
|
|
|
instrument :url, key: key do |payload|
|
|
|
|
verified_token_with_expiration = ActiveStorage.verifier.generate(
|
|
|
|
{
|
|
|
|
key: key,
|
|
|
|
content_type: content_type,
|
|
|
|
content_length: content_length,
|
|
|
|
checksum: checksum,
|
|
|
|
service_name: name,
|
|
|
|
filename: filename_for(key)
|
|
|
|
},
|
|
|
|
expires_in: expires_in,
|
|
|
|
purpose: :blob_token
|
|
|
|
)
|
|
|
|
|
|
|
|
generated_url = url_helpers.update_rails_disk_service_url(verified_token_with_expiration, host: current_host)
|
|
|
|
|
|
|
|
payload[:url] = generated_url
|
|
|
|
|
|
|
|
generated_url
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-03-04 22:08:09 +00:00
|
|
|
# Mantener retrocompatibilidad con cómo gestionamos los archivos
|
|
|
|
# subidos hasta ahora.
|
|
|
|
#
|
|
|
|
# @param :key [String]
|
|
|
|
# @return [String]
|
|
|
|
def folder_for(key)
|
|
|
|
key
|
|
|
|
end
|
|
|
|
|
2022-03-06 15:51:06 +00:00
|
|
|
# Obtiene el nombre de archivo para esta key
|
|
|
|
#
|
|
|
|
# @param :key [String]
|
|
|
|
# @return [String]
|
|
|
|
def filename_for(key)
|
2022-07-13 16:34:45 +00:00
|
|
|
ActiveStorage::Blob.where(key: key).limit(1).pluck(:filename).first.tap do |filename|
|
|
|
|
raise ArgumentError, "Filename for key #{key} is blank" if filename.blank?
|
|
|
|
end
|
2022-03-06 15:51:06 +00:00
|
|
|
end
|
|
|
|
|
2022-03-04 22:08:09 +00:00
|
|
|
# Crea una ruta para la llave con un nombre conocido.
|
2022-03-06 19:00:21 +00:00
|
|
|
#
|
|
|
|
# @param :key [String]
|
|
|
|
# @return [String]
|
2022-03-04 22:08:09 +00:00
|
|
|
def path_for(key)
|
2022-03-06 15:51:06 +00:00
|
|
|
File.join root, folder_for(key), filename_for(key)
|
2022-03-04 22:08:09 +00:00
|
|
|
end
|
2023-03-25 18:22:54 +00:00
|
|
|
|
|
|
|
# @return [Site]
|
|
|
|
def site
|
|
|
|
@site ||= Site.find_by_name(File.basename(root))
|
|
|
|
end
|
2022-03-04 22:08:09 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|