Created
July 22, 2012 13:05
-
-
Save adililhan/3159594 to your computer and use it in GitHub Desktop.
ompldr.org'a dosya yükler ve yüklenen dosyayı bilgisayardan siler.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# | |
# Copyright 2007-2009 David Shakaryan <[email protected]> | |
# Copyright 2007-2009 Brenden Matthews <[email protected]> | |
# Copyright 2010 Murilo Soares Pereira <[email protected]> | |
# | |
# Distributed under the terms of the GNU General Public License v3 | |
# | |
# Special thanks to Christoph for a patch. | |
# Thanks to Murilo for the major refactoring | |
# | |
require 'singleton' | |
require 'getoptlong' | |
require 'tempfile' | |
class XclipBuffer | |
include Singleton | |
attr_reader :content | |
def initialize | |
@content = '' | |
end | |
def append!(string) | |
@content += "#{string}\n" | |
end | |
end | |
class ErrorCounter | |
include Singleton | |
attr_reader :count | |
def initialize | |
@count = 0 | |
end | |
def increment! | |
@count += 1 | |
end | |
end | |
module Shell | |
def curl_installed? | |
!%x{curl --version 2> /dev/null}.empty? | |
end | |
def xclip_installed? | |
!%x{which xclip 2> /dev/null}.empty? | |
end | |
def piped_data_given? | |
!STDIN.tty? | |
end | |
end | |
module Omploader | |
URL = 'http://ompldr.org' | |
UPLOAD_URL = "#{URL}/upload" | |
extend self | |
def file_url(id) | |
"#{URL}/v#{id}" | |
end | |
end | |
module Ompload | |
MAX_FILE_SIZE = 2**32 | |
USAGE = <<-USAGE.gsub(/^ /, '') | |
Usage: ompload [-h|--help] [options] [file(s)] | |
-q, --quiet Only output errors and warnings | |
-u, --url Only output URL when finished | |
-f, --filename Filename to use when posting data | |
from stdin | |
-n, --no-clip Disable copying of URL to clipboard | |
(this feature uses the xclip tool) | |
You can supply a list of files or data via stdin (or both) | |
This script requires a copy of cURL in the path, | |
and will automatically copy the list of URLs to. | |
the X11 clipboard if the `xclip' program is | |
available in your PATH. | |
USAGE | |
class ThrottledError < StandardError; end | |
module Message | |
extend self | |
def curl_failed_posting_file(file_path) | |
"error: curl failed to return a response uploading '#{file_path}'" | |
end | |
def throttled(file_path) | |
"error: got throttled when trying to ompload '#{file_path}'\n" << | |
"Awaiting 60s and attempting to continue..." | |
end | |
def progress(file_path) | |
"Progress for '#{file_path}'" | |
end | |
def omploaded(file_path, id) | |
"Omploaded '#{file_path}' to #{Omploader.file_url(id)}" | |
# system ("ls #{file_path}'") | |
end | |
def invalid_file(file_path) | |
"error: '#{file_path}' does not exist or is not a regular file" | |
end | |
def file_too_big(file_path) | |
"error: '#{file_path}' exceeds #{MAX_FILE_SIZE} bytes " << | |
"(size is #{File.size(file_path)})." | |
end | |
end | |
module UploadsHandler | |
extend self, Shell | |
def handle_file(file_path, options = {}) | |
puts Message.progress(file_path) if !options[:quiet] && !options[:url] | |
response = upload_with_curl({ :file_path => file_path, | |
:silent => options[:quiet] || options[:url] }) | |
handle_response!(response, file_path, options) | |
rescue ThrottledError | |
STDERR.puts Message.throttled(file_path) | |
sleep(60) and retry | |
end | |
def handle_files(file_paths, options = {}) | |
file_paths.each do |file_path| | |
if !File.file?(file_path) | |
STDERR.puts Message.invalid_file(file_path) | |
ErrorCounter.instance.increment! | |
elsif File.size(file_path) > MAX_FILE_SIZE | |
STDERR.puts Message.file_too_big(file_path) | |
ErrorCounter.instance.increment! | |
else | |
handle_file(file_path, options) | |
end | |
end | |
end | |
def handle_data(data, options = {}) | |
file_name = options[:file_name] || 'piped data' | |
puts Message.progress(file_name) if !options[:quiet] && !options[:url] | |
response = upload_with_curl({ :data => data, | |
:file_name => file_name, | |
:silent => options[:quiet] || options[:url] }) | |
handle_response!(response, file_name, options) | |
rescue ThrottledError | |
STDERR.puts Message.throttled(file_name) | |
sleep(60) and retry | |
end | |
private | |
def upload_with_curl(options) | |
response = Tempfile.new('ompload') | |
progress_bar_or_silent = options[:silent] ? '-s' : '-#' | |
if options[:data] | |
IO.popen("curl #{progress_bar_or_silent} -F 'file1=@-;filename=#{options[:file_name]}' #{Omploader::UPLOAD_URL} -o '#{response.path}'", "w+") do |pipe| | |
pipe.puts options[:data] | |
end | |
elsif options[:file_path] | |
%x{curl #{progress_bar_or_silent} -F file1=@#{options[:file_path].inspect} #{Omploader::UPLOAD_URL} -o '#{response.path}'} | |
end | |
IO.read(response.path) | |
end | |
def handle_response!(response, file_name, options) | |
if response =~ /Slow down there, cowboy\./ | |
raise ThrottledError | |
else | |
if response =~ /View file: <a href="v([A-Za-z0-9+\/]+)">/ | |
puts Message.omploaded(file_name, $1) unless options[:quiet] | |
exec ("rm -rf '#{file_name}'") | |
if xclip_installed? && options[:clip] | |
XclipBuffer.instance.append!("#{Omploader.file_url($1)}") | |
end | |
else | |
STDERR.puts Message.curl_failed_posting_file(file_name) | |
ErrorCounter.instance.increment! | |
end | |
end | |
end | |
end | |
module CLI | |
extend self, Shell | |
def run(argv, options = {}) | |
unless curl_installed? | |
abort('error: curl missing or not in path. Cannot continue.') | |
end | |
abort(USAGE) if ARGV.size < 1 && !piped_data_given? || options[:help] | |
UploadsHandler.handle_files(ARGV, options) | |
UploadsHandler.handle_data(STDIN.read(4096), options) if piped_data_given? | |
if xclip_installed? && options[:clip] && !XclipBuffer.instance.content.empty? | |
IO.popen('xclip', 'w+').puts XclipBuffer.instance.content | |
end | |
if !options[:quiet] && !options[:url] | |
if ErrorCounter.instance.count > 0 | |
puts "Finished with #{ErrorCounter.instance.count} errors." | |
else | |
puts 'Success.' | |
end | |
end | |
end | |
end | |
end | |
opts = GetoptLong.new(['--help', '-h', GetoptLong::NO_ARGUMENT], | |
['--filename', '-f', GetoptLong::REQUIRED_ARGUMENT], | |
['--quiet', '-q', GetoptLong::NO_ARGUMENT], | |
['--url', '-u', GetoptLong::NO_ARGUMENT], | |
['--no-clip', '-n', GetoptLong::NO_ARGUMENT]) | |
options = { :clip => true } | |
opts.each do |opt, arg| | |
case opt | |
when '--help' | |
options[:help] = true | |
when '--filename' | |
options[:file_name] = arg | |
when '--quiet' | |
options[:quiet] = true | |
when '--url' | |
options[:url] = true | |
when '--no-clip' | |
options[:clip] = false | |
end | |
end | |
Ompload::CLI.run(ARGV, options) if $0 == __FILE__ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment