This repository has been archived by the owner on Mar 29, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tempfiles-client.rb
executable file
·112 lines (94 loc) · 2.63 KB
/
tempfiles-client.rb
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/env ruby
# frozen_string_literal: true
require 'clipboard'
require 'rest-client'
require 'json'
require 'open-uri'
require 'optparse'
require 'tempfile'
module Settings
@settings = nil
@path = "#{Dir.home}/.config/TempFiles/settings.json"
def self.load
Settings.download unless File.exists? @path
file = JSON.parse(File.read(@path))
@settings = file
end
def self.get(name)
@settings[name]
end
def self.download
url = URI('https://tempfiles.download/linux_client.json')
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
response = http.request(Net::HTTP::Get.new(url))
raise 'Unable to download settings' unless response.code == '200'
begin
json = JSON.parse(response.body)
rescue JSON::ParseException => e
raise 'Downloaded settings contain errors'
end
Dir.mkdir(File.dirname(@path)) unless Dir.exists? File.dirname(@path)
File.open(@path, 'w') do |f|
f.write(JSON.pretty_generate(json))
end
end
end
module TempFiles
def self.upload(path)
file = path.start_with?('file://') ? File.new(open(path), 'rb') : File.new(path, 'rb')
res = RestClient.post(Settings.get('upload url'), file: file)
res = JSON.parse(res)
raise 'Upload error' unless res.key?('url')
res['url']
end
end
def take_screenshot
screenshot_name = "screenshot_#{Time.now.getutc.to_i}.png"
fullpath = File.expand_path("#{Settings.get('local path')}/#{screenshot_name}")
`import #{@options.include?(:windowed) ? '-window root' : ''} -colorspace rgb #{fullpath}`
fullpath
end
def write_data(data)
file = Tempfile.new('tempfiles')
file.write(data)
file.rewind
file.close
file.path
end
def notify(msg, path)
`notify-send TempFiles "#{msg}" -i #{path}`
end
###
# Parse arguments
###
ARGV << '-h' if ARGV.empty?
@options = {}
OptionParser.new do |opts|
opts.on('-s', '--screenshot', 'Take screenshot') do
@options[:screenshot] = true
end
opts.on('-w', '--windowed', 'Capture entire window (requires -s)') do
@options[:windowed] = true
end
opts.on('-u', '--upload', 'Upload file') do
@options[:upload] = true
end
end.parse!
raise OptionParser::MissingArgument if (@options.key?(:windowed) && @options[:screenshot].nil?)
###
# Run program
###
Settings.load
if @options.key? :screenshot
path = take_screenshot
Clipboard.copy "file://#{path}"
end
if @options.key? :upload
clipboard = Clipboard.paste
path = clipboard.start_with?('file://') ? clipboard : write_data(clipboard)
clipboard.slice!('file://')
url = TempFiles.upload(path)
notify('Uploaded!', path) if Settings.get 'notify'
Clipboard.copy url if Settings.get 'copy'
end