Files

97 lines
2.7 KiB
Ruby
Raw Permalink Normal View History

2023-10-22 01:54:20 +03:00
# frozen_string_literal: true
class PreviewDocumentPageController < ActionController::API
include ActiveStorage::SetCurrent
FORMAT = Templates::ProcessDocument::FORMAT
2026-08-07 21:45:56 +03:00
TMPFILE_PREFIX = 'attachment-'
TMPFILE_TTL = 5.minutes
TMPFILE_MAX_TOTAL_SIZE = Docuseal.multitenant? ? 400.megabytes : 1.gigabyte
2023-10-22 01:54:20 +03:00
def show
2026-02-12 09:22:08 +02:00
result_data =
ApplicationRecord.signed_id_verifier.verified(params[:signed_key], purpose: :attachment)
2024-02-17 22:14:28 +02:00
2026-02-12 09:22:08 +02:00
attachment =
if result_data.is_a?(Array) && result_data.compact_blank.size == 2
attachment_id, attachment_uuid = result_data
ActiveStorage::Attachment.find_by(id: attachment_id, uuid: attachment_uuid)
elsif result_data
ActiveStorage::Attachment.find_by(uuid: result_data)
end
2023-10-22 01:54:20 +03:00
return head :not_found unless attachment
2025-05-03 13:59:15 +03:00
@template = attachment.record
2024-10-09 17:53:50 +03:00
preview_image = attachment.preview_images.joins(:blob)
.find_by(blob: { filename: ["#{params[:id]}.png", "#{params[:id]}.jpg"] })
2023-10-22 01:54:20 +03:00
2026-06-19 16:33:55 +03:00
if preview_image
return redirect_to preview_image.url(time: ActiveStorage::Attachment.service_url_time),
allow_other_host: true
end
2023-10-22 01:54:20 +03:00
2024-02-17 22:14:28 +02:00
preview_image =
2026-08-07 21:45:56 +03:00
open_attachment_io(attachment) do |io|
Templates::ProcessDocument.generate_pdf_preview_from_io(attachment, io, params[:id].to_i)
end
2023-10-22 01:54:20 +03:00
2026-06-19 16:33:55 +03:00
redirect_to preview_image.url(time: ActiveStorage::Attachment.service_url_time), allow_other_host: true
2023-10-22 01:54:20 +03:00
end
2026-08-07 21:45:56 +03:00
def open_attachment_io(attachment, &)
return File.open(ActiveStorage::Blob.service.path_for(attachment.key), 'rb', &) if attachment.service.name == :disk
2023-10-22 01:54:20 +03:00
2026-08-07 21:45:56 +03:00
file_name = "#{TMPFILE_PREFIX}#{Digest::SHA1.hexdigest("#{attachment.id}-#{attachment.uuid}")}"
file_path = File.join(Dir.tmpdir, file_name)
File.open(file_path, File::RDWR | File::CREAT, 0o644) do |file|
file.flock(File::LOCK_EX)
2023-10-22 01:54:20 +03:00
# rubocop:disable Style/ZeroLengthPredicate
2026-08-07 21:45:56 +03:00
if file.size.zero?
cleanup_stale_tempfiles
file.binmode
2023-10-22 01:54:20 +03:00
2026-08-07 21:45:56 +03:00
file.write(attachment.download)
else
FileUtils.touch(file_path)
2023-10-22 01:54:20 +03:00
end
# rubocop:enable Style/ZeroLengthPredicate
2026-08-07 21:45:56 +03:00
file.flock(File::LOCK_UN)
yield file
2023-10-22 01:54:20 +03:00
end
2026-08-07 21:45:56 +03:00
end
def cleanup_stale_tempfiles
entries =
Dir.glob(File.join(Dir.tmpdir, "#{TMPFILE_PREFIX}*")).filter_map do |path|
stat = File.stat(path)
[path, stat.mtime, stat.size]
rescue Errno::ENOENT
nil
end
2023-10-22 01:54:20 +03:00
2026-08-07 21:45:56 +03:00
total_size = entries.sum(&:last)
stale_time = TMPFILE_TTL.ago
entries.sort_by(&:second).each do |path, mtime, size|
break if mtime > stale_time && total_size <= TMPFILE_MAX_TOTAL_SIZE
File.unlink(path)
total_size -= size
rescue Errno::ENOENT
nil
end
2023-10-22 01:54:20 +03:00
end
end