Skip to content

Commit

Permalink
use signed uuid in previews
Browse files Browse the repository at this point in the history
  • Loading branch information
omohokcoj committed Feb 17, 2024
1 parent 229d78c commit 323f5c0
Show file tree
Hide file tree
Showing 10 changed files with 24 additions and 12 deletions.
2 changes: 1 addition & 1 deletion app/controllers/api/templates_documents_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def create
render json: {
schema:,
documents: documents.as_json(
methods: [:metadata],
methods: %i[metadata signed_uuid],
include: {
preview_images: { methods: %i[url metadata filename] }
}
Expand Down
16 changes: 13 additions & 3 deletions app/controllers/preview_document_page_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -21,9 +30,10 @@ def show
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)
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/templates_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def edit
@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
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/templates_preview_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def show
@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
Expand Down
2 changes: 1 addition & 1 deletion app/javascript/template_builder/document.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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`
}
})
},
Expand Down
2 changes: 1 addition & 1 deletion app/views/submissions/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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">
Expand Down
2 changes: 1 addition & 1 deletion app/views/submit_form/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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">
Expand Down
4 changes: 4 additions & 0 deletions config/initializers/active_storage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@

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
Expand Down
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
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
Expand Down
2 changes: 0 additions & 2 deletions lib/templates/process_document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,6 @@ def generate_pdf_preview_from_file(attachment, file_path, page_number)
record: attachment
)
end

io
end
end
end

0 comments on commit 323f5c0

Please sign in to comment.