Class: CampfireExport::Upload
- Inherits:
-
Object
- Object
- CampfireExport::Upload
- Includes:
- IO
- Defined in:
- lib/campfire_export.rb
Instance Attribute Summary collapse
-
#byte_size ⇒ Object
Returns the value of attribute byte_size.
-
#content_type ⇒ Object
Returns the value of attribute content_type.
-
#date ⇒ Object
Returns the value of attribute date.
-
#filename ⇒ Object
Returns the value of attribute filename.
-
#full_url ⇒ Object
Returns the value of attribute full_url.
-
#id ⇒ Object
Returns the value of attribute id.
-
#message ⇒ Object
Returns the value of attribute message.
-
#room ⇒ Object
Returns the value of attribute room.
Instance Method Summary collapse
- #deleted? ⇒ Boolean
- #export ⇒ Object
- #export_content(content_dir, path_component = nil, verify = true) ⇒ Object
-
#initialize(message) ⇒ Upload
constructor
A new instance of Upload.
- #is_image? ⇒ Boolean
-
#thumb_dir ⇒ Object
Image thumbnails are used to inline image uploads in HTML transcripts.
- #upload_dir ⇒ Object
Methods included from IO
#api_url, #export_dir, #export_file, #get, #log, #verify_export, #zero_pad
Constructor Details
#initialize(message) ⇒ Upload
Returns a new instance of Upload.
397 398 399 400 401 402 |
# File 'lib/campfire_export.rb', line 397 def initialize() @message = @room = .room @date = .date @deleted = false end |
Instance Attribute Details
#byte_size ⇒ Object
Returns the value of attribute byte_size.
395 396 397 |
# File 'lib/campfire_export.rb', line 395 def byte_size @byte_size end |
#content_type ⇒ Object
Returns the value of attribute content_type.
395 396 397 |
# File 'lib/campfire_export.rb', line 395 def content_type @content_type end |
#date ⇒ Object
Returns the value of attribute date.
395 396 397 |
# File 'lib/campfire_export.rb', line 395 def date @date end |
#filename ⇒ Object
Returns the value of attribute filename.
395 396 397 |
# File 'lib/campfire_export.rb', line 395 def filename @filename end |
#full_url ⇒ Object
Returns the value of attribute full_url.
395 396 397 |
# File 'lib/campfire_export.rb', line 395 def full_url @full_url end |
#id ⇒ Object
Returns the value of attribute id.
395 396 397 |
# File 'lib/campfire_export.rb', line 395 def id @id end |
#message ⇒ Object
Returns the value of attribute message.
395 396 397 |
# File 'lib/campfire_export.rb', line 395 def @message end |
#room ⇒ Object
Returns the value of attribute room.
395 396 397 |
# File 'lib/campfire_export.rb', line 395 def room @room end |
Instance Method Details
#deleted? ⇒ Boolean
404 405 406 |
# File 'lib/campfire_export.rb', line 404 def deleted? @deleted end |
#export ⇒ Object
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 |
# File 'lib/campfire_export.rb', line 421 def export begin log(:info, " #{.body} ... ") # Get the upload object corresponding to this message. upload_path = "/room/#{room.id}/messages/#{.id}/upload.xml" upload = Nokogiri::XML get(upload_path).body # Get the upload itself and export it. @id = upload.xpath('/upload/id').text @byte_size = upload.xpath('/upload/byte-size').text.to_i @content_type = upload.xpath('/upload/content-type').text @filename = upload.xpath('/upload/name').text @full_url = upload.xpath('/upload/full-url').text export_content(upload_dir) export_content(thumb_dir, path_component="thumb/#{id}", verify=false) if is_image? log(:info, "ok\n") rescue CampfireExport::Exception => e if e.code == 404 # If the upload 404s, that should mean it was subsequently deleted. @deleted = true log(:info, "deleted\n") else raise e end end end |
#export_content(content_dir, path_component = nil, verify = true) ⇒ Object
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 |
# File 'lib/campfire_export.rb', line 451 def export_content(content_dir, path_component=nil, verify=true) # If the export directory name is different than the URL path component, # the caller can define the path_component separately. path_component ||= content_dir # Write uploads to a subdirectory, using the upload ID as a directory # name to avoid overwriting multiple uploads of the same file within # the same day (for instance, if 'Picture 1.png' is uploaded twice # in a day, this will preserve both copies). This path pattern also # matches the tail of the upload path in the HTML transcript, making # it easier to make downloads functional from the HTML transcripts. content_path = "/room/#{room.id}/#{path_component}/#{CGI.escape(filename)}" content = get(content_path).body FileUtils.mkdir_p(File.join(export_dir, content_dir)) export_file(content, "#{content_dir}/#{filename}", 'wb') verify_export("#{content_dir}/#{filename}", byte_size) if verify end |
#is_image? ⇒ Boolean
408 409 410 |
# File 'lib/campfire_export.rb', line 408 def is_image? content_type.start_with?("image/") end |
#thumb_dir ⇒ Object
Image thumbnails are used to inline image uploads in HTML transcripts.
417 418 419 |
# File 'lib/campfire_export.rb', line 417 def thumb_dir "thumbs/#{id}" end |
#upload_dir ⇒ Object
412 413 414 |
# File 'lib/campfire_export.rb', line 412 def upload_dir "uploads/#{id}" end |