Class: CampfireExport::Upload

Inherits:
Object
  • Object
show all
Includes:
IO
Defined in:
lib/campfire_export.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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)
  @message = message
  @room = message.room
  @date = message.date
  @deleted = false
end

Instance Attribute Details

#byte_sizeObject

Returns the value of attribute byte_size.



395
396
397
# File 'lib/campfire_export.rb', line 395

def byte_size
  @byte_size
end

#content_typeObject

Returns the value of attribute content_type.



395
396
397
# File 'lib/campfire_export.rb', line 395

def content_type
  @content_type
end

#dateObject

Returns the value of attribute date.



395
396
397
# File 'lib/campfire_export.rb', line 395

def date
  @date
end

#filenameObject

Returns the value of attribute filename.



395
396
397
# File 'lib/campfire_export.rb', line 395

def filename
  @filename
end

#idObject

Returns the value of attribute id.



395
396
397
# File 'lib/campfire_export.rb', line 395

def id
  @id
end

#messageObject

Returns the value of attribute message.



395
396
397
# File 'lib/campfire_export.rb', line 395

def message
  @message
end

#roomObject

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

Returns:

  • (Boolean)


404
405
406
# File 'lib/campfire_export.rb', line 404

def deleted?
  @deleted
end

#exportObject



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
# File 'lib/campfire_export.rb', line 421

def export
  begin
    log(:info, "    #{message.body} ... ")

    # Get the upload object corresponding to this message.
    upload_path = "/room/#{room.id}/messages/#{message.id}/upload.xml"
    upload = Nokogiri::XML get(upload_path).body
    
    # Get the upload itself and export it.
    @id = upload.css('id').text
    @byte_size = upload.css('byte-size').text.to_i
    @content_type = upload.css('content-type').text
    @filename = upload.css('name').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



450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
# File 'lib/campfire_export.rb', line 450

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

Returns:

  • (Boolean)


408
409
410
# File 'lib/campfire_export.rb', line 408

def is_image?
  content_type.start_with?("image/")
end

#thumb_dirObject

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_dirObject



412
413
414
# File 'lib/campfire_export.rb', line 412

def upload_dir
  "uploads/#{id}"
end