/
icopy
executable file
·141 lines (124 loc) · 3.31 KB
/
icopy
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/usr/bin/env ruby
# Upload the currently-copied image URL to i.gabebw.com
#
# Options:
#
# -f local_path: Upload a local file FROM here (if not given, uses copied URL)
# -t remote_name: Upload the file TO here, with this name.
#
# USAGE:
#
# Upload the image whose URL I just copied and keep the same name:
#
# icopy
#
# Upload a local image named `from.jpg` and upload as `to.jpg`
#
# icopy -f flip.jpg -t to.jpg
#
# Upload the copied image URL and upload as flip.{jpg,png,gif,etc}
#
# icopy -t flip
require "optparse"
require "uri"
require "tempfile"
from = nil
to = nil
def url_from_clipboard
url = `pbpaste`.strip
unless url.start_with?("http")
STDERR.puts "Error: Looks like you didn't copy a URL"
STDERR.puts "Found: #{url}"
exit 1
end
url
end
def guess_extension!(from, temp_file_path)
from_path = if from =~ /^http/
URI.parse(from).path
else
from
end
extension = File.extname(from_path).sub(/^\./, "")
if extension == ""
file_output = `file "#{temp_file_path}"`.strip
if file_output.include?("JPEG image data")
extension = "jpg"
elsif file_output.include?("PNG image data")
extension = "png"
elsif file_output.include?("GIF image data")
extension = "gif"
else
STDERR.puts "Couldn't guess extension, and none provided"
exit 1
end
end
# Twitter has URLs that end in `.jpg:large` -> `jpg`
# Also, convert `.jpeg` to `.jpg`
extension.sub(/:large$/, "").downcase.sub("jpeg", "jpg")
end
def with_extension!(from, to, path)
if File.directory?(from)
to
elsif to.include?(".")
to
else
to += "." + guess_extension!(from, path)
end
end
if ARGV.size == 1
to = ARGV[0]
else
OptionParser.new do |opts|
opts.banner = <<USAGE
Usage: icopy [-f local-name.gif] [-t remote-name.gif]
Options:
-f local_path: Upload a local file FROM here (if not given, uses copied URL)
-t remote_name: Upload the file TO here, with this name.
USAGE
opts.on("-fFROM", "what to copy from") do |from_value|
from = from_value
end
opts.on("-tTO", "what to copy to") do |to_value|
to = to_value
end
opts.on("-h", "--help", "Prints this help") do
puts opts
exit 0
end
end.parse!
# ARGV is now what's left after OptionParser "eats" the arguments it knows
# about.
# This is mostly to fix the case where I provide `-f` but don't say `-t`
# before the target, like this:
# icopy -f foo.jpg bar.jpg
# when I mean this:
# icopy -f foo.jpg -t bar.jpg
# To fix it, assume set `to` to the extra argument.
if ARGV.length == 1
to = ARGV[0]
elsif ARGV.length > 1
STDERR.puts "Unhandled extra arguments: #{ARGV.inspect}"
exit 1
end
end
if from && !from.start_with?("http")
local_file_name = from
else
from = url_from_clipboard
if from.nil? || from == ""
STDERR.puts "Error: -f parameter is required, or copy a URL"
exit 1
end
local_file_name = Tempfile.new.path
`curl -Lso "#{local_file_name}" "#{from}"`
end
if to.nil?
to = File.basename(from)
elsif to.end_with?("/")
# `-t subdir/` copies it into the subdirectory, base filename unchanged
to = "#{to}#{File.basename(from)}"
end
to = with_extension!(from, to, local_file_name)
STDERR.puts "#{from} -> #{to}"
`upload-image "#{local_file_name}" "#{to}"`