Class: S3Html::S3FolderUpload

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(extract_point, uniq_path, account_id) ⇒ S3FolderUpload

Returns a new instance of S3FolderUpload.



60
61
62
63
64
65
66
67
# File 'lib/s3_html.rb', line 60

def initialize(extract_point, uniq_path, )
  @folder_path       = extract_point << uniq_path
  @files             = Dir.glob("#{folder_path}/**/*")
  @total_files       = files.length
  @extract_point     = extract_point
  @uniq_path         = uniq_path
  @account_id        = 
end

Instance Attribute Details

#filesObject

Returns the value of attribute files.



58
59
60
# File 'lib/s3_html.rb', line 58

def files
  @files
end

#folder_pathObject (readonly)

Returns the value of attribute folder_path.



57
58
59
# File 'lib/s3_html.rb', line 57

def folder_path
  @folder_path
end

#s3_bucketObject (readonly)

Returns the value of attribute s3_bucket.



57
58
59
# File 'lib/s3_html.rb', line 57

def s3_bucket
  @s3_bucket
end

#total_filesObject (readonly)

Returns the value of attribute total_files.



57
58
59
# File 'lib/s3_html.rb', line 57

def total_files
  @total_files
end

Instance Method Details

#upload!(thread_count = 5) ⇒ Object

public: Upload files from the folder to S3

thread_count - How many threads you want to use (defaults to 5)

Examples

=> uploader.upload!(20)
  true
=> uploader.upload!
  true

When finished the process succesfully: Returns key and public_url value When finished the process unsuccesfully: Returns false TODO; need some specific error return



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/s3_html.rb', line 81

def upload!(thread_count = 5)
  result = nil
  file_number = 0
  mutex       = Mutex.new
  threads     = []

  thread_count.times do |i|
    threads[i] = Thread.new {
      until files.empty?
        mutex.synchronize do
          file_number += 1
          Thread.current["file_number"] = file_number
        end
        file = files.pop rescue nil
        next unless file

        path = file
        puts "[#{Thread.current["file_number"]}/#{total_files}] uploading..."
        data = File.open(file)
        if File.directory?(data)
          data.close
          next
        else
          prefix = "htmls/#{@account_id}/" << @uniq_path
          Aws.config.update({
            region: S3_REGION,
            credentials: Aws::Credentials.new(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY)
          })
          s3 = Aws::S3::Resource.new(region:S3_REGION)
          obj = s3.bucket(S3_BUCKET_NAME).object(prefix + path.sub(@extract_point, ""))
          if obj.upload_file data, {acl: 'public-read'}
             result = { :key_url => obj.key, :public_url => obj.public_url }
          end
          data.close
        end

      end
    }
  end
  threads.each { |t| t.join }
  final_result = {}
  if result
    final_result[:key_url] = result[:key_url].split(@uniq_path)[0] + @uniq_path
    final_result[:public_url] = result[:public_url].split(@uniq_path)[0] + @uniq_path + "/index.html"
  else
    final_result = false
  end
  return final_result
end