Last active
February 2, 2025 04:22
-
-
Save mkaschenko/b7ee13d29878e421109c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # lib/carrier.rb | |
| module Carrier | |
| def send_to_browser(file, options = {}) | |
| extension = File.extname(file.path)[1..-1].downcase # '.jpg' => 'jpg' for Mime::Type | |
| options[:type] ||= Mime::Type.lookup_by_extension(extension) | |
| send_file(file.path, options) | |
| end | |
| # TODO: it changes parameter value!!! | |
| # This hack is only for the IE error in a filename encoding with headers | |
| # Using URI.escape | |
| def escape_filename_if_msie(filename) | |
| if request.env['HTTP_USER_AGENT'] =~ /MSIE/ | |
| filename = URI.escape(filename, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]")) | |
| end | |
| filename | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # app/controllers/pages_controller.rb | |
| class PagesController < ApplicationController | |
| def show | |
| authorize :view, document | |
| page_image = preview_page || watermarked_page | |
| send_to_browser(page_image, disposition: 'inline') | |
| end | |
| private | |
| def document | |
| @document ||= Document.find(params[:id]) | |
| end | |
| def preview_page | |
| document_page.image.preview if params[:style] == 'preview' | |
| end | |
| def watermarked_page | |
| Watermarker.new(watermark).mark(document_page.image) | |
| end | |
| def document_page | |
| @document_page ||= | |
| document.current.pages.where(number: params[:page_number]).first! | |
| end | |
| def watermark | |
| ' ' + [ Time.now.to_s(:long), current_user.presents.fullname, document.title ].join(' - ') | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # lib/watermarker.rb | |
| class Watermarker | |
| @@ramdisk = "#{Rails.root}/data/ramdisk/" | |
| attr_accessor :watermark, :border_size, :output_path | |
| def initialize(watermark) | |
| self.watermark = watermark | |
| self.border_size = 1 | |
| self.output_path = generate_output_path('.png') | |
| end | |
| def mark(image_file) | |
| image_path = image_file.path | |
| change_output_values unless png?(image_path) | |
| system "convert -size 900x900 xc:none -fill '#CCCCCC70' \ | |
| -gravity NorthWest -draw \"font-size 34 rotate 45 text 10,10 '#{watermark}'\" miff:- |\ | |
| composite -tile - \"#{image_path}\" miff:- |\ | |
| convert -bordercolor '#808080' -border #{border_size} - \"#{output_path}\" >/dev/null 2>&1" | |
| File.new(output_path) | |
| end | |
| private | |
| def change_output_values(path) | |
| self.border_size = 10 | |
| self.output_path = generate_output_path('.jpg') | |
| end | |
| def generate_output_path(extension) | |
| @@ramdisk + SecureRandom.hex(6) + extension | |
| end | |
| def png?(path) | |
| File.extname(path) == '.png' | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment