Class: Sphragis::DocumentsController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/sphragis/documents_controller.rb

Instance Method Summary collapse

Instance Method Details

#previewObject

GET /sphragis/documents/:id/preview



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'app/controllers/sphragis/documents_controller.rb', line 6

def preview
  @document_path = params[:path]
  @signature_options = {
    x: params[:x]&.to_i || 400,
    y: params[:y]&.to_i || 50,
    width: params[:width]&.to_i || 150,
    height: params[:height]&.to_i || 50,
    page: params[:page]&.to_i,
    provider: params[:provider]&.to_sym
  }

  @available_providers = ProviderFactory.available_providers
  @selected_provider = @signature_options[:provider] || ProviderFactory.default_provider

  if @document_path && File.exist?(@document_path)
    @signer = PdfSigner.new(@document_path, @signature_options)
    @pdf_info = @signer.pdf_info
  else
    render json: { error: "PDF file not found" }, status: :not_found
  end
end

#signObject

POST /sphragis/documents/:id/sign



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'app/controllers/sphragis/documents_controller.rb', line 40

def sign
  document_path = params[:path]
  signature_options = {
    x: params[:x]&.to_i,
    y: params[:y]&.to_i,
    width: params[:width]&.to_i,
    height: params[:height]&.to_i,
    page: params[:page]&.to_i,
    reason: params[:reason],
    location: params[:location],
    contact_info: params[:contact_info],
    provider: params[:provider]&.to_sym
  }.compact

  signer = PdfSigner.new(document_path, signature_options)
  signed_path = signer.sign

  render json: {
    success: true,
    signed_path: signed_path,
    provider: signer.provider.provider_name,
    message: "Document signed successfully with #{signer.provider.provider_name}"
  }
rescue PdfSigner::SigningError => e
  render json: { error: e.message }, status: :unprocessable_entity
rescue Providers::BaseProvider::ProviderError => e
  render json: { error: "Provider error: #{e.message}" }, status: :service_unavailable
rescue ProviderFactory::ProviderNotConfiguredError => e
  render json: { error: e.message }, status: :precondition_failed
end

#validate_placementObject

GET /sphragis/documents/:id/validate_placement



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'app/controllers/sphragis/documents_controller.rb', line 72

def validate_placement
  document_path = params[:path]
  x = params[:x].to_i
  y = params[:y].to_i
  page = params[:page]&.to_i

  signer = PdfSigner.new(document_path, { x: x, y: y, page: page })

  if signer.validate_placement(page)
    render json: { valid: true }
  else
    render json: { valid: false, error: "Invalid signature placement" }
  end
rescue StandardError => e
  render json: { valid: false, error: e.message }, status: :unprocessable_entity
end

#viewObject

GET /sphragis/documents/:id/view



29
30
31
32
33
34
35
36
37
# File 'app/controllers/sphragis/documents_controller.rb', line 29

def view
  document_path = params[:path]

  if document_path && File.exist?(document_path)
    send_file document_path, type: "application/pdf", disposition: "inline"
  else
    render json: { error: "PDF file not found" }, status: :not_found
  end
end