Skip to content

Commit a4b9000

Browse files
committed
use signed uuid in previews
1 parent 229d78c commit a4b9000

File tree

10 files changed

+24
-12
lines changed

10 files changed

+24
-12
lines changed

app/controllers/api/templates_documents_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def create
1616
render json: {
1717
schema:,
1818
documents: documents.as_json(
19-
methods: [:metadata],
19+
methods: %i[metadata signed_uuid],
2020
include: {
2121
preview_images: { methods: %i[url metadata filename] }
2222
}

app/controllers/preview_document_page_controller.rb

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,16 @@ class PreviewDocumentPageController < ActionController::API
66
FORMAT = Templates::ProcessDocument::FORMAT
77

88
def show
9-
attachment = ActiveStorage::Attachment.find_by(uuid: params[:attachment_uuid])
9+
attachment_uuid = ApplicationRecord.signed_id_verifier.verified(params[:signed_uuid])
10+
11+
attachment =
12+
if attachment_uuid
13+
ActiveStorage::Attachment.find_by(uuid: attachment_uuid)
14+
else
15+
ActiveStorage::Attachment.find_by(uuid: params[:signed_uuid])
16+
17+
Rollbar.warning("Load preview from uuid: #{params[:signed_uuid].to_s.first(10)}") if defined?(Rollbar)
18+
end
1019

1120
return head :not_found unless attachment
1221

@@ -21,9 +30,10 @@ def show
2130
find_or_create_document_tempfile_path(attachment)
2231
end
2332

24-
io = Templates::ProcessDocument.generate_pdf_preview_from_file(attachment, file_path, params[:id].to_i)
33+
preview_image =
34+
Templates::ProcessDocument.generate_pdf_preview_from_file(attachment, file_path, params[:id].to_i)
2535

26-
render plain: io.tap(&:rewind).read, content_type: 'image/jpeg'
36+
redirect_to preview_image.url, allow_other_host: true
2737
end
2838

2939
def find_or_create_document_tempfile_path(attachment)

app/controllers/templates_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def edit
3333
@template_data =
3434
@template.as_json.merge(
3535
documents: @template.schema_documents.as_json(
36-
methods: [:metadata],
36+
methods: %i[metadata signed_uuid],
3737
include: { preview_images: { methods: %i[url metadata filename] } }
3838
)
3939
).to_json

app/controllers/templates_preview_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def show
1212
@template_data =
1313
@template.as_json.merge(
1414
documents: @template.schema_documents.as_json(
15-
methods: [:metadata],
15+
methods: %i[metadata signed_uuid],
1616
include: { preview_images: { methods: %i[url metadata filename] } }
1717
)
1818
).to_json

app/javascript/template_builder/document.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ export default {
102102
return this.previewImagesIndex[i] || {
103103
metadata: lazyloadMetadata,
104104
id: Math.random().toString(),
105-
url: this.basePreviewUrl + `/preview/${this.document.uuid}/${i}.jpg`
105+
url: this.basePreviewUrl + `/preview/${this.document.signed_uuid || this.document.uuid}/${i}.jpg`
106106
}
107107
})
108108
},

app/views/submissions/show.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
<% preview_images_index = document.preview_images.loaded? ? document.preview_images.index_by { |e| e.filename.base.to_i } : {} %>
5454
<% lazyload_metadata = document.preview_images.first.metadata %>
5555
<% (document.metadata.dig('pdf', 'number_of_pages') || (document.preview_images.loaded? ? preview_images_index.size : document.preview_images.size)).times do |index| %>
56-
<% page = preview_images_index[index] || page_blob_struct.new(metadata: lazyload_metadata, url: preview_document_page_path(document.uuid, "#{index}.jpg")) %>
56+
<% page = preview_images_index[index] || page_blob_struct.new(metadata: lazyload_metadata, url: preview_document_page_path(document.signed_uuid, "#{index}.jpg")) %>
5757
<div id="<%= "page-#{document.uuid}-#{index}" %>" class="relative">
5858
<img loading="lazy" src="<%= page.url %>" width="<%= page.metadata['width'] %>" class="border rounded mb-4" height="<%= page.metadata['height'] %>">
5959
<div class="top-0 bottom-0 left-0 right-0 absolute">

app/views/submit_form/show.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<% preview_images_index = document.preview_images.loaded? ? document.preview_images.index_by { |e| e.filename.base.to_i } : {} %>
1414
<% lazyload_metadata = document.preview_images.last.metadata %>
1515
<% (document.metadata.dig('pdf', 'number_of_pages') || (document.preview_images.loaded? ? preview_images_index.size : document.preview_images.size)).times do |index| %>
16-
<% page = preview_images_index[index] || page_blob_struct.new(metadata: lazyload_metadata, url: preview_document_page_path(document.uuid, "#{index}.jpg")) %>
16+
<% page = preview_images_index[index] || page_blob_struct.new(metadata: lazyload_metadata, url: preview_document_page_path(document.signed_uuid, "#{index}.jpg")) %>
1717
<div class="relative my-4 shadow-md">
1818
<img loading="lazy" src="<%= page.url %>" width="<%= page.metadata['width'] %>" height="<%= page.metadata['height'] %>">
1919
<div id="page-<%= [document.uuid, index].join('-') %>" class="top-0 bottom-0 left-0 right-0 absolute">

config/initializers/active_storage.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55

66
has_many_attached :preview_images
77

8+
def signed_uuid
9+
@signed_uuid ||= ApplicationRecord.signed_id_verifier.generate(uuid, expires_in: 6.hours)
10+
end
11+
812
def preview_image_url
913
preview_images.joins(:blob).find_by(blob: { filename: '0.jpg' })&.url
1014
end

config/routes.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@
8181
resource :code_modal, only: %i[show], controller: 'templates_code_modal'
8282
resources :submissions_export, only: %i[index new]
8383
end
84-
resources :preview_document_page, only: %i[show], path: '/preview/:attachment_uuid'
84+
resources :preview_document_page, only: %i[show], path: '/preview/:signed_uuid'
8585

8686
resources :start_form, only: %i[show update], path: 'd', param: 'slug' do
8787
get :completed

lib/templates/process_document.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,6 @@ def generate_pdf_preview_from_file(attachment, file_path, page_number)
9898
record: attachment
9999
)
100100
end
101-
102-
io
103101
end
104102
end
105103
end

0 commit comments

Comments
 (0)