use signed uuid in previews

pull/217/head
Pete Matsyburka 2 years ago
parent 229d78c78b
commit 323f5c0c15

@ -16,7 +16,7 @@ module Api
render json: {
schema:,
documents: documents.as_json(
methods: [:metadata],
methods: %i[metadata signed_uuid],
include: {
preview_images: { methods: %i[url metadata filename] }
}

@ -6,7 +6,16 @@ class PreviewDocumentPageController < ActionController::API
FORMAT = Templates::ProcessDocument::FORMAT
def show
attachment = ActiveStorage::Attachment.find_by(uuid: params[:attachment_uuid])
attachment_uuid = ApplicationRecord.signed_id_verifier.verified(params[:signed_uuid])
attachment =
if attachment_uuid
ActiveStorage::Attachment.find_by(uuid: attachment_uuid)
else
Rollbar.warning("Load preview from uuid: #{params[:signed_uuid].to_s.first(10)}") if defined?(Rollbar)
ActiveStorage::Attachment.find_by(uuid: params[:signed_uuid])
end
return head :not_found unless attachment
@ -21,9 +30,10 @@ class PreviewDocumentPageController < ActionController::API
find_or_create_document_tempfile_path(attachment)
end
io = Templates::ProcessDocument.generate_pdf_preview_from_file(attachment, file_path, params[:id].to_i)
preview_image =
Templates::ProcessDocument.generate_pdf_preview_from_file(attachment, file_path, params[:id].to_i)
render plain: io.tap(&:rewind).read, content_type: 'image/jpeg'
redirect_to preview_image.url, allow_other_host: true
end
def find_or_create_document_tempfile_path(attachment)

@ -33,7 +33,7 @@ class TemplatesController < ApplicationController
@template_data =
@template.as_json.merge(
documents: @template.schema_documents.as_json(
methods: [:metadata],
methods: %i[metadata signed_uuid],
include: { preview_images: { methods: %i[url metadata filename] } }
)
).to_json

@ -12,7 +12,7 @@ class TemplatesPreviewController < ApplicationController
@template_data =
@template.as_json.merge(
documents: @template.schema_documents.as_json(
methods: [:metadata],
methods: %i[metadata signed_uuid],
include: { preview_images: { methods: %i[url metadata filename] } }
)
).to_json

@ -102,7 +102,7 @@ export default {
return this.previewImagesIndex[i] || {
metadata: lazyloadMetadata,
id: Math.random().toString(),
url: this.basePreviewUrl + `/preview/${this.document.uuid}/${i}.jpg`
url: this.basePreviewUrl + `/preview/${this.document.signed_uuid || this.document.uuid}/${i}.jpg`
}
})
},

@ -53,7 +53,7 @@
<% preview_images_index = document.preview_images.loaded? ? document.preview_images.index_by { |e| e.filename.base.to_i } : {} %>
<% lazyload_metadata = document.preview_images.first.metadata %>
<% (document.metadata.dig('pdf', 'number_of_pages') || (document.preview_images.loaded? ? preview_images_index.size : document.preview_images.size)).times do |index| %>
<% page = preview_images_index[index] || page_blob_struct.new(metadata: lazyload_metadata, url: preview_document_page_path(document.uuid, "#{index}.jpg")) %>
<% page = preview_images_index[index] || page_blob_struct.new(metadata: lazyload_metadata, url: preview_document_page_path(document.signed_uuid, "#{index}.jpg")) %>
<div id="<%= "page-#{document.uuid}-#{index}" %>" class="relative">
<img loading="lazy" src="<%= page.url %>" width="<%= page.metadata['width'] %>" class="border rounded mb-4" height="<%= page.metadata['height'] %>">
<div class="top-0 bottom-0 left-0 right-0 absolute">

@ -13,7 +13,7 @@
<% preview_images_index = document.preview_images.loaded? ? document.preview_images.index_by { |e| e.filename.base.to_i } : {} %>
<% lazyload_metadata = document.preview_images.last.metadata %>
<% (document.metadata.dig('pdf', 'number_of_pages') || (document.preview_images.loaded? ? preview_images_index.size : document.preview_images.size)).times do |index| %>
<% page = preview_images_index[index] || page_blob_struct.new(metadata: lazyload_metadata, url: preview_document_page_path(document.uuid, "#{index}.jpg")) %>
<% page = preview_images_index[index] || page_blob_struct.new(metadata: lazyload_metadata, url: preview_document_page_path(document.signed_uuid, "#{index}.jpg")) %>
<div class="relative my-4 shadow-md">
<img loading="lazy" src="<%= page.url %>" width="<%= page.metadata['width'] %>" height="<%= page.metadata['height'] %>">
<div id="page-<%= [document.uuid, index].join('-') %>" class="top-0 bottom-0 left-0 right-0 absolute">

@ -5,6 +5,10 @@ ActiveSupport.on_load(:active_storage_attachment) do
has_many_attached :preview_images
def signed_uuid
@signed_uuid ||= ApplicationRecord.signed_id_verifier.generate(uuid, expires_in: 6.hours)
end
def preview_image_url
preview_images.joins(:blob).find_by(blob: { filename: '0.jpg' })&.url
end

@ -81,7 +81,7 @@ Rails.application.routes.draw do
resource :code_modal, only: %i[show], controller: 'templates_code_modal'
resources :submissions_export, only: %i[index new]
end
resources :preview_document_page, only: %i[show], path: '/preview/:attachment_uuid'
resources :preview_document_page, only: %i[show], path: '/preview/:signed_uuid'
resources :start_form, only: %i[show update], path: 'd', param: 'slug' do
get :completed

@ -98,8 +98,6 @@ module Templates
record: attachment
)
end
io
end
end
end

Loading…
Cancel
Save