ompload 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ *.gem
data/CHANGELOG.org ADDED
@@ -0,0 +1,4 @@
1
+ * 1.0.0
2
+ #+BEGIN_SRC
3
+ Initial release.
4
+ #+END_SRC
data/LICENSE ADDED
@@ -0,0 +1,14 @@
1
+ Copyright (C) 2011 Murilo Pereira <murilo@murilopereira.com>
2
+
3
+ This program is free software: you can redistribute it and/or modify
4
+ it under the terms of the GNU General Public License as published by
5
+ the Free Software Foundation, either version 3 of the License, or
6
+ (at your option) any later version.
7
+
8
+ This program is distributed in the hope that it will be useful,
9
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
+ GNU General Public License for more details.
12
+
13
+ You should have received a copy of the GNU General Public License
14
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
data/README.org ADDED
@@ -0,0 +1,13 @@
1
+ * ompload
2
+ Upload files to [[http://ompldr.org/][omploader]].
3
+ ** Author
4
+ [[http://murilopereira.com][Murilo Pereira]]
5
+ ** License
6
+ Distributed under the terms of the GNU General Public License v3
7
+ ** [[http://git.omp.am/?p=omploader.git;a=blob;f=ompload;hb=HEAD][Original project]]
8
+ #+BEGIN_SRC
9
+ Copyright 2007-2009 David Shakaryan <omp@gentoo.org>
10
+ Copyright 2007-2009 Brenden Matthews <brenden@diddyinc.com>
11
+ Distributed under the terms of the GNU General Public License v3
12
+ Special thanks to Christoph for a patch.
13
+ #+END_SRC
data/bin/ompload ADDED
@@ -0,0 +1,33 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # Copyright 2010-2011 Murilo Pereira <murilo@murilopereira.com>
4
+ # Distributed under the terms of the GNU General Public License v3
5
+ #
6
+
7
+ require File.expand_path('../../lib/ompload', __FILE__)
8
+ require 'getoptlong'
9
+
10
+ opts = GetoptLong.new(['--help', '-h', GetoptLong::NO_ARGUMENT],
11
+ ['--filename', '-f', GetoptLong::REQUIRED_ARGUMENT],
12
+ ['--quiet', '-q', GetoptLong::NO_ARGUMENT],
13
+ ['--url', '-u', GetoptLong::NO_ARGUMENT],
14
+ ['--no-clip', '-n', GetoptLong::NO_ARGUMENT])
15
+
16
+ options = { :clip => true }
17
+
18
+ opts.each do |opt, arg|
19
+ case opt
20
+ when '--help'
21
+ options[:help] = true
22
+ when '--filename'
23
+ options[:file_name] = arg
24
+ when '--quiet'
25
+ options[:quiet] = true
26
+ when '--url'
27
+ options[:url] = true
28
+ when '--no-clip'
29
+ options[:clip] = false
30
+ end
31
+ end
32
+
33
+ Ompload::CLI.run(ARGV, options)
data/lib/ompload.rb ADDED
@@ -0,0 +1,209 @@
1
+ require 'singleton'
2
+ require 'tempfile'
3
+
4
+ class XclipBuffer
5
+ include Singleton
6
+
7
+ attr_reader :content
8
+
9
+ def initialize
10
+ @content = ''
11
+ end
12
+
13
+ def append!(string)
14
+ @content += "#{string}\n"
15
+ end
16
+ end
17
+
18
+ class ErrorCounter
19
+ include Singleton
20
+
21
+ attr_reader :count
22
+
23
+ def initialize
24
+ @count = 0
25
+ end
26
+
27
+ def increment!
28
+ @count += 1
29
+ end
30
+ end
31
+
32
+ module Shell
33
+ def curl_installed?
34
+ !%x{curl --version 2> /dev/null}.empty?
35
+ end
36
+
37
+ def xclip_installed?
38
+ !%x{which xclip 2> /dev/null}.empty?
39
+ end
40
+
41
+ def piped_data_given?
42
+ !STDIN.tty?
43
+ end
44
+ end
45
+
46
+ module Omploader
47
+ URL = 'http://ompldr.org'
48
+ UPLOAD_URL = "#{URL}/upload"
49
+
50
+ extend self
51
+
52
+ def file_url(id)
53
+ "#{URL}/v#{id}"
54
+ end
55
+ end
56
+
57
+ module Ompload
58
+ VERSION = '1.0.0'
59
+ MAX_FILE_SIZE = 2**32
60
+
61
+ USAGE = <<-USAGE.gsub(/^ /, '')
62
+ Usage: ompload [-h|--help] [options] [file(s)]
63
+ -q, --quiet Only output errors and warnings
64
+ -u, --url Only output URL when finished
65
+ -f, --filename Filename to use when posting data
66
+ from stdin
67
+ -n, --no-clip Disable copying of URL to clipboard
68
+ (this feature uses the xclip tool)
69
+
70
+ You can supply a list of files or data via stdin (or both)
71
+
72
+ This script requires a copy of cURL in the path,
73
+ and will automatically copy the list of URLs to.
74
+ the X11 clipboard if the `xclip' program is
75
+ available in your PATH.
76
+ USAGE
77
+
78
+ class ThrottledError < StandardError; end
79
+
80
+ module Message
81
+ extend self
82
+
83
+ def curl_failed_posting_file(file_path)
84
+ "error: curl failed to return a response uploading '#{file_path}'"
85
+ end
86
+
87
+ def throttled(file_path)
88
+ "error: got throttled when trying to ompload '#{file_path}'\n" <<
89
+ "Awaiting 60s and attempting to continue..."
90
+ end
91
+
92
+ def progress(file_path)
93
+ "Progress for '#{file_path}'"
94
+ end
95
+
96
+ def omploaded(file_path, id)
97
+ "Omploaded '#{file_path}' to #{Omploader.file_url(id)}"
98
+ end
99
+
100
+ def invalid_file(file_path)
101
+ "error: '#{file_path}' does not exist or is not a regular file"
102
+ end
103
+
104
+ def file_too_big(file_path)
105
+ "error: '#{file_path}' exceeds #{MAX_FILE_SIZE} bytes " <<
106
+ "(size is #{File.size(file_path)})."
107
+ end
108
+ end
109
+
110
+ module UploadsHandler
111
+ extend self, Shell
112
+
113
+ def handle_file(file_path, options = {})
114
+ puts Message.progress(file_path) if !options[:quiet] && !options[:url]
115
+ response = upload_with_curl({ :file_path => file_path,
116
+ :silent => options[:quiet] || options[:url] })
117
+ handle_response!(response, file_path, options)
118
+ rescue ThrottledError
119
+ STDERR.puts Message.throttled(file_path)
120
+ sleep(60) and retry
121
+ end
122
+
123
+ def handle_files(file_paths, options = {})
124
+ file_paths.each do |file_path|
125
+ if !File.file?(file_path)
126
+ STDERR.puts Message.invalid_file(file_path)
127
+ ErrorCounter.instance.increment!
128
+ elsif File.size(file_path) > MAX_FILE_SIZE
129
+ STDERR.puts Message.file_too_big(file_path)
130
+ ErrorCounter.instance.increment!
131
+ else
132
+ handle_file(file_path, options)
133
+ end
134
+ end
135
+ end
136
+
137
+ def handle_data(data, options = {})
138
+ file_name = options[:file_name] || 'piped data'
139
+ puts Message.progress(file_name) if !options[:quiet] && !options[:url]
140
+ response = upload_with_curl({ :data => data,
141
+ :file_name => file_name,
142
+ :silent => options[:quiet] || options[:url] })
143
+ handle_response!(response, file_name, options)
144
+ rescue ThrottledError
145
+ STDERR.puts Message.throttled(file_name)
146
+ sleep(60) and retry
147
+ end
148
+
149
+ private
150
+
151
+ def upload_with_curl(options)
152
+ response = Tempfile.new('ompload')
153
+ progress_bar_or_silent = options[:silent] ? '-s' : '-#'
154
+
155
+ if options[:data]
156
+ IO.popen("curl #{progress_bar_or_silent} -F 'file1=@-;filename=#{options[:file_name]}' #{Omploader::UPLOAD_URL} -o '#{response.path}'", "w+") do |pipe|
157
+ pipe.puts options[:data]
158
+ end
159
+ elsif options[:file_path]
160
+ %x{curl #{progress_bar_or_silent} -F file1=@#{options[:file_path].inspect} #{Omploader::UPLOAD_URL} -o '#{response.path}'}
161
+ end
162
+
163
+ IO.read(response.path)
164
+ end
165
+
166
+ def handle_response!(response, file_name, options)
167
+ if response =~ /Slow down there, cowboy\./
168
+ raise ThrottledError
169
+ else
170
+ if response =~ /View file: <a href="v([A-Za-z0-9+\/]+)">/
171
+ puts Message.omploaded(file_name, $1) unless options[:quiet]
172
+ if xclip_installed? && options[:clip]
173
+ XclipBuffer.instance.append!("#{Omploader.file_url($1)}")
174
+ end
175
+ else
176
+ STDERR.puts Message.curl_failed_posting_file(file_name)
177
+ ErrorCounter.instance.increment!
178
+ end
179
+ end
180
+ end
181
+ end
182
+
183
+ module CLI
184
+ extend self, Shell
185
+
186
+ def run(argv, options = {})
187
+ unless curl_installed?
188
+ abort('error: curl missing or not in path. Cannot continue.')
189
+ end
190
+
191
+ abort(USAGE) if ARGV.size < 1 && !piped_data_given? || options[:help]
192
+
193
+ UploadsHandler.handle_files(ARGV, options)
194
+ UploadsHandler.handle_data(STDIN.read(4096), options) if piped_data_given?
195
+
196
+ if xclip_installed? && options[:clip] && !XclipBuffer.instance.content.empty?
197
+ IO.popen('xclip', 'w+').puts XclipBuffer.instance.content
198
+ end
199
+
200
+ if !options[:quiet] && !options[:url]
201
+ if ErrorCounter.instance.count > 0
202
+ puts "Finished with #{ErrorCounter.instance.count} errors."
203
+ else
204
+ puts 'Success.'
205
+ end
206
+ end
207
+ end
208
+ end
209
+ end
data/ompload.gemspec ADDED
@@ -0,0 +1,17 @@
1
+ require File.expand_path('../lib/ompload', __FILE__)
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = 'ompload'
5
+ s.version = Ompload::VERSION
6
+ s.platform = Gem::Platform::RUBY
7
+ s.author = 'Murilo Pereira'
8
+ s.email = 'murilo@murilopereira.com'
9
+ s.homepage = 'https://github.com/murilasso/ompload'
10
+ s.summary = 'Upload files to omploader.'
11
+
12
+ s.required_rubygems_version = '>= 1.3.6'
13
+
14
+ s.files = `git ls-files`.split("\n")
15
+ s.require_path = '.'
16
+ s.executable = 'ompload'
17
+ end
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ompload
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 1.0.0
6
+ platform: ruby
7
+ authors:
8
+ - Murilo Pereira
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-05-06 00:00:00 -03:00
14
+ default_executable:
15
+ dependencies: []
16
+
17
+ description:
18
+ email: murilo@murilopereira.com
19
+ executables:
20
+ - ompload
21
+ extensions: []
22
+
23
+ extra_rdoc_files: []
24
+
25
+ files:
26
+ - .gitignore
27
+ - CHANGELOG.org
28
+ - LICENSE
29
+ - README.org
30
+ - bin/ompload
31
+ - lib/ompload.rb
32
+ - ompload.gemspec
33
+ has_rdoc: true
34
+ homepage: https://github.com/murilasso/ompload
35
+ licenses: []
36
+
37
+ post_install_message:
38
+ rdoc_options: []
39
+
40
+ require_paths:
41
+ - .
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ none: false
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: "0"
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: 1.3.6
54
+ requirements: []
55
+
56
+ rubyforge_project:
57
+ rubygems_version: 1.6.2
58
+ signing_key:
59
+ specification_version: 3
60
+ summary: Upload files to omploader.
61
+ test_files: []
62
+