-
Notifications
You must be signed in to change notification settings - Fork 1
/
cups-img-backend
executable file
·98 lines (74 loc) · 1.95 KB
/
cups-img-backend
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/env ruby
require 'rbconfig'
STDERR.puts "DEBUG: ruby prefix: #{RbConfig::CONFIG['prefix']}"
require 'rubygems'
require 'tempfile'
require 'fileutils'
require 'json'
require 'etc'
require 'dbus'
module CupsBackend
class Job
attr_reader :job_id, :user, :title, :copies, :options, :source
def initialize(*argv)
@job_id, @user, @title, @copies, @options, @source = *argv
@copies = (@copies || 1).to_i
end
def read
if @source
File.open(@source, 'rb', &:read)
else
STDIN.read
end
end
def tmpInput
File.open("/tmp/rawimg-backend-#{$$}.tmp.#{@job_id}", 'wb') { |fp| fp << self.read }
end
def type
ENV['CONTENT_TYPE'] || 'application/octet-stream'
end
def ext
case type
when 'image/png'
"png"
when 'image/jpeg'
"jpg"
when 'application/pdf'
"pdf"
else
"bin"
end
end
end
end
class Notifier < DBus::Object
dbus_interface "net.livelinktechnology.ImageCapture" do
# Define a signal in the interface:
dbus_signal :GotFile, "type:s, path:s, copies:u"
end
end
if ARGV.size >= 5
job = CupsBackend::Job.new(*ARGV)
# ignore copies
#
STDERR.puts "INFO: Printing page 1"
STDERR.puts "PAGE: 1"
nicename = "job-#{job.job_id}.#{job.ext}"
outfile = "/tmp/#{nicename}"
metafile = "/tmp/#{nicename}.json"
File.open(outfile, 'wb') do |fp|
fp << job.read
end
File.open(metafile, 'w') do |fp|
fp << JSON.pretty_unparse({'ENV' => ENV.to_hash, 'ARGV' => ARGV})
end
File.chmod(0644, "/tmp/#{nicename}", outfile, metafile)
bus = DBus.system_bus
notifier = Notifier.new('/net/livelinktechnology/ImageCapture')
service = bus.request_service("net.livelinktechnology.ImageCapture")
service.export(notifier)
notifier.GotFile(job.type, outfile, job.copies)
exit 0
else
puts %Q[direct #{File.basename $0}:/ "Image Capture" "Img-Cap" "MFG:LiveLink;MDL:Image Capture;DES:LiveLink Image Capture;CLS:PRINTER"]
end