Class: ImageVenue::File

Inherits:
Object
  • Object
show all
Defined in:
lib/image_venue/file.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(directory, name = nil, io = nil, url = nil, is_new = true) ⇒ File

Returns a new instance of File.



186
187
188
189
190
191
192
193
194
# File 'lib/image_venue/file.rb', line 186

def initialize(directory, name=nil, io=nil, url=nil, is_new=true)
  self.content_type = 'image/jpeg'
  self.directory = directory
  self.name = name
  self.io = io
  self.url = url
  @is_new = is_new
  return nil
end

Class Attribute Details

.debugObject

Returns the value of attribute debug.



4
5
6
# File 'lib/image_venue/file.rb', line 4

def debug
  @debug
end

Instance Attribute Details

#content_typeObject

Returns the value of attribute content_type.



179
180
181
# File 'lib/image_venue/file.rb', line 179

def content_type
  @content_type
end

#debugObject

Returns the value of attribute debug.



180
181
182
# File 'lib/image_venue/file.rb', line 180

def debug
  @debug
end

#directoryObject

Returns the value of attribute directory.



181
182
183
# File 'lib/image_venue/file.rb', line 181

def directory
  @directory
end

#ioObject

Returns the value of attribute io.



182
183
184
# File 'lib/image_venue/file.rb', line 182

def io
  @io
end

#nameObject

Returns the value of attribute name.



183
184
185
# File 'lib/image_venue/file.rb', line 183

def name
  @name
end

#urlObject

Returns the value of attribute url.



184
185
186
# File 'lib/image_venue/file.rb', line 184

def url
  @url
end

Class Method Details

.debug?Boolean

Returns:

  • (Boolean)


6
7
8
# File 'lib/image_venue/file.rb', line 6

def debug?
  (ImageVenue.debug? or self.debug) ? true : false
end

.delete_file_keyObject



117
118
119
# File 'lib/image_venue/file.rb', line 117

def delete_file_key
  @delete_file_key ||= 'delete_files[]'
end

.delete_params(file_name) ⇒ Object



146
147
148
149
150
151
# File 'lib/image_venue/file.rb', line 146

def delete_params(file_name)
  {
    self.delete_submit_key => self.delete_submit_value,
    self.delete_file_key => file_name
  }
end

.delete_submit_keyObject



121
122
123
# File 'lib/image_venue/file.rb', line 121

def delete_submit_key
  @delete_submit_key ||= 'submit'
end

.delete_submit_valueObject



125
126
127
# File 'lib/image_venue/file.rb', line 125

def delete_submit_value
  @delete_submit_value ||= 'Delete'
end

.delete_uriObject



113
114
115
# File 'lib/image_venue/file.rb', line 113

def delete_uri
  @delete_uri ||= URI.parse(ImageVenue.base_url + '/manage_files.php')
end

.find_params(directory_name) ⇒ Object



171
172
173
174
175
176
# File 'lib/image_venue/file.rb', line 171

def find_params(directory_name)
  {
    self.list_format_key => self.list_format_forum,
    self.list_directory_key => directory_name
  }
end

.list(directory) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/image_venue/file.rb', line 153

def list(directory)
  self.puts_debug "Trying to fetch files from directory: '#{directory.name}' ..."
  request = Net::HTTP::Post.new(self.list_uri.path)
  request.form_data = self.find_params(directory.name)
  request.header['cookie'] = [directory.connection.cookie_as_string]
  response = Net::HTTP.start(self.list_uri.host, self.list_uri.port) do |http|
    http.request(request)
  end
  if response.is_a?(Net::HTTPSuccess)
    files = self.parse_forum_codes(directory, response.body)
    self.puts_debug "Files successfully fetched!"
    return files
  else
    self.puts_debug "Files fetch failed!"
    return false
  end
end

.list_directory_keyObject



47
48
49
# File 'lib/image_venue/file.rb', line 47

def list_directory_key
  @list_directory_key ||= 'dir'
end

.list_format_forumObject



39
40
41
# File 'lib/image_venue/file.rb', line 39

def list_format_forum
  @list_format_forum ||= 'forum'
end

.list_format_keyObject



35
36
37
# File 'lib/image_venue/file.rb', line 35

def list_format_key
  @list_format_key ||= 'poster'
end

.list_format_websiteObject



43
44
45
# File 'lib/image_venue/file.rb', line 43

def list_format_website
  @list_format_website ||= 'webpage'
end

.list_uriObject



31
32
33
# File 'lib/image_venue/file.rb', line 31

def list_uri
  @list_uri ||= URI.parse(ImageVenue.base_url + '/process_get_dir_codes.php')
end

.parse_forum_codes(directory, response_body) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/image_venue/file.rb', line 19

def parse_forum_codes(directory, response_body)
  files = []
  self.puts_debug "Parsing Forum-Codes ..."
  hpricot = Hpricot(response_body)
  urls = hpricot.search('p:last').last.inner_text.scan(/\[URL=(.+?)\]/i).flatten
  urls.each do |url|
    self.puts_debug "Found URL: `#{url}`"
    files << self.new(directory, nil, nil, url, false)
  end
  return files
end

.puts_debug(*args) ⇒ Object



10
11
12
13
14
15
16
17
# File 'lib/image_venue/file.rb', line 10

def puts_debug(*args)
  if self.debug?
    puts(*args)
    return true
  else
    return false
  end
end

.upload_action_keyObject



55
56
57
# File 'lib/image_venue/file.rb', line 55

def upload_action_key
  @upload_action_key ||= 'action'
end

.upload_content_keyObject



71
72
73
# File 'lib/image_venue/file.rb', line 71

def upload_content_key
  @upload_content_key ||= 'imgcontent'
end

.upload_content_notsafeObject Also known as: upload_content_adult



80
81
82
# File 'lib/image_venue/file.rb', line 80

def upload_content_notsafe
  @upload_content_notsafe ||= 'notsafe'
end

.upload_content_safeObject Also known as: upload_content_family



75
76
77
# File 'lib/image_venue/file.rb', line 75

def upload_content_safe
  @upload_content_safe ||= 'safe'
end

.upload_directory_keyObject



67
68
69
# File 'lib/image_venue/file.rb', line 67

def upload_directory_key
  @upload_directory_key ||= 'upload_dir'
end

.upload_file_keyObject



63
64
65
# File 'lib/image_venue/file.rb', line 63

def upload_file_key
  @upload_file_key ||= 'userfile[]'
end

.upload_file_params(file) ⇒ Object



129
130
131
132
133
# File 'lib/image_venue/file.rb', line 129

def upload_file_params(file)
  {
    self.upload_file_key => file
  }
end

.upload_form_params(directory, content_safe = false, interlink = false, resize = nil) ⇒ Object



135
136
137
138
139
140
141
142
143
144
# File 'lib/image_venue/file.rb', line 135

def upload_form_params(directory, content_safe=false, interlink=false, resize=nil)
  return {
    self.upload_action_key => '1',
    self.upload_directory_key => directory.name,
    self.upload_user_key => directory.connection.cookie[directory.connection.class.cookie_user_key],
    self.upload_content_key => (content_safe) ? self.upload_content_safe : self.upload_content_notsafe,
    self.upload_interlink_key => (interlink) ? self.upload_interlink_yes : self.upload_interlink_no,
    self.upload_resize_key => resize || ''
  }
end

.upload_image_typesObject



109
110
111
# File 'lib/image_venue/file.rb', line 109

def upload_image_types
  @upload_image_types ||= ['jpeg', 'jpg']
end


85
86
87
# File 'lib/image_venue/file.rb', line 85

def upload_interlink_key
  @upload_interlink_key ||= 'interlinkimage'
end


93
94
95
# File 'lib/image_venue/file.rb', line 93

def upload_interlink_no
  @upload__interlink_no ||= 'interlinkno'
end


89
90
91
# File 'lib/image_venue/file.rb', line 89

def upload_interlink_yes
  @upload__interlink_yes ||= 'interlinkyes'
end

.upload_maximum_filesObject



101
102
103
# File 'lib/image_venue/file.rb', line 101

def upload_maximum_files
  @upload_maximum_files ||= 10
end

.upload_maximum_sizeObject



105
106
107
# File 'lib/image_venue/file.rb', line 105

def upload_maximum_size
  @upload_maximum_size ||= 3 * 1024 * 1024 # 3 MByte
end

.upload_resize_keyObject



97
98
99
# File 'lib/image_venue/file.rb', line 97

def upload_resize_key
  @upload_resize_key ||= 'img_resize'
end

.upload_uriObject



51
52
53
# File 'lib/image_venue/file.rb', line 51

def upload_uri
  @upload_uri ||= URI.parse(ImageVenue.base_url + '/upload.php')
end

.upload_user_keyObject



59
60
61
# File 'lib/image_venue/file.rb', line 59

def upload_user_key
  @upload_user_key ||= 'user_id'
end

Instance Method Details

#base_nameObject



258
259
260
# File 'lib/image_venue/file.rb', line 258

def base_name
  @base_name ||= Kernel::File.basename(self.name)
end

#clear_cacheObject



324
325
326
327
328
329
330
331
332
333
# File 'lib/image_venue/file.rb', line 324

def clear_cache
  @large_io = nil
  @large_url = nil
  @thumbnail_io = nil
  @thumbnail_url = nil
  @thumbnail_for_board_upper = nil
  @thumbnail_for_board_lower = nil
  @thumbnail_for_website = nil
  self.puts_debug "File's cache cleared."
end

#debug?Boolean

Returns:

  • (Boolean)


196
197
198
# File 'lib/image_venue/file.rb', line 196

def debug?
  (self.class.debug? or self.debug) ? true : false
end

#destroyObject



233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/image_venue/file.rb', line 233

def destroy()
  self.puts_debug "Trying to delete file `#{self.name}` from directory `#{self.directory.name}` ..."
  request = Net::HTTP::Post.new(self.class.delete_uri.path)
  request.form_data = self.class.delete_params(self.name)
  self.directory.connection.view_directory = self.directory.name
  request.header['cookie'] = [self.directory.connection.cookie_as_string]
  response = Net::HTTP.start(self.class.delete_uri.host, self.class.delete_uri.port) do |http|
    http.request(request)
  end
  self.directory.connection.view_directory = nil
  if response.is_a?(Net::HTTPSuccess)
    self.puts_debug "File `#{self.name}` successfully deleted!"
    @is_new = true
    return true
  else
    self.puts_debug "File `#{self.name}` delete failed!"
    return false
  end
end

#is_new?Boolean

Returns:

  • (Boolean)


209
210
211
# File 'lib/image_venue/file.rb', line 209

def is_new?
  @is_new
end

#large_io(reload = false) ⇒ Object



266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/image_venue/file.rb', line 266

def large_io(reload=false)
  if @large_io.nil? or reload
    self.puts_debug "Trying to fetch large image data from `#{self.url}` ..."
    self.large_url(false)
    @large_io = open(@large_url)
    self.puts_debug "Image data successfully fetched!"
  else
    @large_io.rewind
    self.puts_debug "Image data from cache!"
  end
  return @large_io
end

#large_url(reload = false) ⇒ Object



279
280
281
282
283
284
285
286
287
288
289
290
291
292
# File 'lib/image_venue/file.rb', line 279

def large_url(reload=false)
  if reload or @large_url.nil?
    self.puts_debug "Trying to fetch large image URL from `#{self.url}` ..."
    html = open(self.url)
    img = Hpricot(html).search('img[@id="thepic"]:first').first
    uri = URI.parse(self.url)
    uri.path = '/' + img.attributes.find {|key, value| key =~ /^src$/i}.last
    @large_url = uri.to_s
    self.puts_debug "Large image URL successfully fetched!"
  else
    self.puts_debug "Large image URL from cache!"
  end
  return @large_url
end

#puts_debug(*args) ⇒ Object



200
201
202
203
204
205
206
207
# File 'lib/image_venue/file.rb', line 200

def puts_debug(*args)
  if self.debug?
    puts(*args)
    return true
  else
    return false
  end
end

#real_nameObject



262
263
264
# File 'lib/image_venue/file.rb', line 262

def real_name
  @real_name ||= $1+$2 if self.name =~ /^\d+_(.*)_\d+_\d+\w{2}(\.\w{2,3})$/
end

#save(reselect = false, content_safe = false, interlink = false, resize = nil) ⇒ Object



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/image_venue/file.rb', line 213

def save(reselect=false, content_safe=false, interlink=false, resize=nil)
  self.directory.select(reselect)
  self.puts_debug "Trying to upload file `#{self.name}` to directory `#{self.directory.name}` ..."
  request = Net::HTTP::Post.new(self.class.upload_uri.path)
  request.set_multipart_data(self.class.upload_file_params(self), self.class.upload_form_params(self.directory, content_safe, interlink, resize))
  request.header['cookie'] = [self.directory.connection.cookie_as_string]
  response = Net::HTTP.start(self.class.upload_uri.host, self.class.upload_uri.port) do |http|
    http.request(request)
  end
  if response.is_a?(Net::HTTPSuccess)
    self.url = Hpricot(response.body).search('textarea').first.inner_text.strip
    self.puts_debug "File successfully uploaded!"
    @is_new = false
    return true
  else
    self.puts_debug "File upload failed!"
    return false
  end
end

#thumbnail_for_board_lowerObject



316
317
318
# File 'lib/image_venue/file.rb', line 316

def thumbnail_for_board_lower
  @thumbnail_for_board_lower ||= "[url=#{self.url}][img]#{self.thumbnail_url}[/img][/url]"
end

#thumbnail_for_board_upperObject



312
313
314
# File 'lib/image_venue/file.rb', line 312

def thumbnail_for_board_upper
  @thumbnail_for_board_upper ||= "[URL=#{self.url}][IMG]#{self.thumbnail_url}[/IMG][/URL]"
end

#thumbnail_for_websiteObject



320
321
322
# File 'lib/image_venue/file.rb', line 320

def thumbnail_for_website
  @thumbnail_for_website ||= "<a href=\"#{self.url}\" target=_blank><img src=\"#{self.thumbnail_url}\" border=\"0\"></a>"
end

#thumbnail_ioObject



294
295
296
297
298
299
300
301
302
303
# File 'lib/image_venue/file.rb', line 294

def thumbnail_io
  if @thumbnail_io.nil?
    self.puts_debug "Trying to fetch thumbnail data ..."
    @thumbnail_io = open(self.thumbnail_url)
    self.puts_debug "Thumbnail data successfully fetched!"
  else
    self.puts_debug "Thumbnail data from cache!"
  end
  return @thumbnail_io
end

#thumbnail_urlObject



305
306
307
308
309
310
# File 'lib/image_venue/file.rb', line 305

def thumbnail_url
  if @thumbnail_url.nil? and self.url =~ /_(\d+)lo/
    @thumbnail_url = self.url.gsub('img.php?image=', "loc#{$1}/th_")
  end
  return @thumbnail_url
end