Class: ImageshackUploader

Inherits:
Object
  • Object
show all
Defined in:
lib/captured/uploaders/imageshack_uploader.rb

Overview

Constant Summary collapse

USER_AGENT =
"Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en) AppleWebKit/419 (KHTML, like Gecko) Safari/419.3"
BOUNDARY =
'----------PuSHerInDaBUSH_$'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ ImageshackUploader

Returns a new instance of ImageshackUploader.



11
12
13
14
# File 'lib/captured/uploaders/imageshack_uploader.rb', line 11

def initialize(config = {})
  @config = config
  @shack_id = config['upload']['shackid'] || "captured"
end

Instance Attribute Details

#urlObject (readonly)

Returns the value of attribute url.



7
8
9
# File 'lib/captured/uploaders/imageshack_uploader.rb', line 7

def url
  @url
end

Instance Method Details

#getdirectObject



88
89
90
91
92
# File 'lib/captured/uploaders/imageshack_uploader.rb', line 88

def getdirect
  puts @res.header
  puts @res.body
  @posted_url = @res.header['location']
end

#locate(path) ⇒ Object



57
58
59
# File 'lib/captured/uploaders/imageshack_uploader.rb', line 57

def locate(path)
  path !~ /^http/ ? "local" : "remote"
end

#prepare_multipart(params) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/captured/uploaders/imageshack_uploader.rb', line 36

def prepare_multipart ( params )
  fp = []
  params.each do |k,v|
    if v.respond_to?(:read)
      fp.push(FileParam.new(k,v.path,v.read))
    else fp.push(Param.new(k,v)) 
    end
  end
  query = fp.collect {|p| "--" + BOUNDARY + "\r\n" + p.to_multipart }.join("") + "--" + BOUNDARY + "--"
  return query
end

#prepFile(path_to_file) ⇒ Object



48
49
50
51
52
53
54
55
# File 'lib/captured/uploaders/imageshack_uploader.rb', line 48

def prepFile(path_to_file)
  file = File.new(path_to_file)
  @header['Content-Type'] = "multipart/form-data, boundary=" + BOUNDARY + " "
  @params['url'] = 'paste image url here'
  @params['fileupload'] = file
  $query = prepare_multipart(@params)
  file.close
end

#process_upload(query, headers = {}) ⇒ Object



61
62
63
64
65
# File 'lib/captured/uploaders/imageshack_uploader.rb', line 61

def process_upload( query, headers={} )
  Net::HTTP.start(@hosturi.host) do | http |
    http.post(@hosturi.path, query, headers);
  end
end

#transferObject



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/captured/uploaders/imageshack_uploader.rb', line 76

def transfer
  case locate(@img)
  when "local"
    @hosturi = URI.parse('http://load.imageshack.us/index.php')
    prepFile(@img)
    @res = process_upload($query,@header)
  when "remote"
    @hosturi = URI.parse('http://imageshack.us/transload.php')
    @res = transload(@img)
  end
end

#transload(url) ⇒ Object



67
68
69
70
71
72
73
74
# File 'lib/captured/uploaders/imageshack_uploader.rb', line 67

def transload(url)
  @header['Content-Type'] = 'form-data'
  @params['url'] = url
  @params['fileupload'] = ''
  postreq = Net::HTTP::Post.new(@hosturi.path, @header)
  postreq.set_form_data(@params)
  return Net::HTTP.new(@hosturi.host, @hosturi.port).start { |http| http.request(postreq) }
end

#upload(file_name) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/captured/uploaders/imageshack_uploader.rb', line 16

def upload(file_name)
  unless file_name =~ /jpe?g|png|gif|bmp|tif|tiff|swf$/
    raise(NonImageTypeError, 'Expected image file.')
  end
  @img = file_name
  @posted_url, @hosturi, @res = "","",""
  @header, @params = {}, {}
  @header['Cookie'] = "myimages=#{@shack_id}"
  @header['User-Agent'] = USER_AGENT
  @params['uploadtype'] = 'on'  
  @params['brand'] = ''
  @params['refer'] = ''
  @params['MAX_FILE_SIZE'] = '13145728'
  @params['optimage'] = '0'
  @params['rembar'] = '1'
  transfer
  getdirect
  @url = @posted_url.gsub("content.php?page=done&l=", "")
end