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.
|
|
|
|
#
|
|
|
|
# @todo: Implementar LFS. No nos gusta mucho la idea porque duplica
|
|
|
|
# el espacio en disco, pero es la única forma que tenemos (hasta que
|
|
|
|
# implementemos IPFS) para poder transferir los archivos junto con el
|
|
|
|
# sitio.
|
|
|
|
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-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-03-06 20:07:37 +00:00
|
|
|
ActiveStorage::Blob.where(key: key).limit(1).pluck(:filename).first
|
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
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|