Class: Floccus::PutHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/floccus/put_helper.rb

Constant Summary collapse

KB =
1024

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ PutHelper

Setup



8
9
10
11
12
13
# File 'lib/floccus/put_helper.rb', line 8

def initialize(*args)
  @filename = args[0]
  @hashed_filename = Floccus::Filename.generate(@filename)
  @cloud = Floccus::Cloud.new
  @s3 = @cloud.s3
end

Instance Method Details

#runObject

Run the full command



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/floccus/put_helper.rb', line 17

def run
  file = open(@filename)

  # Instantiate a Progress Bar
  total_blocks = (file.size / KB).to_i  + 2
  progressbar = ProgressBar.create total: total_blocks

  # Create the object
  object = @s3.buckets[@cloud.bucket].objects[@hashed_filename]

  # Write the file
  object.write(content_length: file.size, acl: :public_read) do |buffer, bytes|
    buffer.write(file.read(bytes))
    
    progressbar.increment
  end

  file.close

  public_url = if @cloud.use_domain?
                 obj = object.public_url
                 obj.scheme = "http"
                 obj.host = @cloud.domain
                 obj.path = obj.path.gsub("/#{@cloud.bucket}", "")
                 obj
               else
                 object.public_url
               end

  # Echo the results
  system "echo #{public_url} | pbcopy"
  puts "---> #{public_url}"
end