module S3Html

Constants

VERSION

Public Class Methods

delete_dir(prefix) click to toggle source

prefix - is directory path. since s3 aws-sdk gem can't destroy whole folder we should iterate through and delete each item. need return Responce for assurance of if it is deleted or not

# File lib/s3_html.rb, line 32
def delete_dir(prefix)
  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_keys = s3.bucket(S3_BUCKET_NAME).objects(:prefix => prefix).collect(&:key)
  s3_client = get_s3_client
  obj_keys.map do |obj_key|
    s3_client.delete_object({
      bucket: S3_BUCKET_NAME,
      key: obj_key,
    })
  end
end
uploadHtml(file_path) click to toggle source
# File lib/s3_html.rb, line 5
def uploadHtml(file_path)
  extract_point = '/tmp/unzipped/'
  uniq_path = SecureRandom.uuid.to_s
  index_detected = false
  Zip::ZipFile.open(file_path) { |zip_file|
     zip_file.each { |f|
     f_path = File.join(extract_point + uniq_path, f.name)
     FileUtils.mkdir_p(File.dirname(f_path))
     zip_file.extract(f, f_path) unless File.exist?(f_path)
   }
  }
  Dir.foreach(extract_point + uniq_path) do |item|
    next if item == '.' or item == '..'
    index_detected = true if item == "index.html"
  end
  if index_detected
    uploader = S3FolderUpload.new(extract_point, uniq_path, current_account.id)
    uploader.upload!(1)
  else
    @error_message = I18n.t('push_html.invalid_page')
    return false
  end
end

Private Class Methods

get_s3_client() click to toggle source
# File lib/s3_html.rb, line 48
def get_s3_client
    return Aws::S3::Client.new(
       credentials: Aws::Credentials.new(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY),
       region: S3_REGION
     )
end