diff --git a/Rakefile b/Rakefile index bfa9ec53..e0510665 100644 --- a/Rakefile +++ b/Rakefile @@ -6,6 +6,36 @@ require File.expand_path('../config/application', __FILE__) Rails.application.load_tasks namespace :justask do + desc "Upload to AWS" + task paperclaws: :environment do + if APP_CONFIG["fog"]["credentials"].nil? or APP_CONFIG["fog"]["credentials"]["provider"] != :AWS + throw "Needs fog (AWS) to be defined in justask.yml" + + format = '%t (%c/%C) [%b>%i] %e' + root = "#{Rails.root}/public/system" + files = Dir["#{root}/**/*.*"] + progress = ProgressBar.create title: 'Processing files', format: format, starting_at: 0, total: files.length + + # weird voodoo, something is causing just using "APP_CONFIG["fog"]["credentials"]" as Fog::Storage.new to cause an exception + # TODO: Programmatically copy? + credentials = { + provider: :AWS, + aws_access_key_id: APP_CONFIG["fog"]["credentials"]["aws_access_key_id"], + aws_secret_access_key: APP_CONFIG["fog"]["credentials"]["aws_secret_access_key"], + region: APP_CONFIG["fog"]["credentials"]["region"] + } + + fog = Fog::Storage.new credentials + bucket = fog.directories.get APP_CONFIG["fog"]["directory"] + + files.each do |file| + bucket.files.create key: file[root.length + 1 ... file.length], body: File.open(file), public: true + progress.increment + end + + puts "hopefully uploaded #{files.length} files" + end + desc "Recount everything!" task recount: :environment do format = '%t (%c/%C) [%b>%i] %e' diff --git a/config/initializers/25_paperclip.rb b/config/initializers/25_paperclip.rb index 19c754db..dc6db410 100644 --- a/config/initializers/25_paperclip.rb +++ b/config/initializers/25_paperclip.rb @@ -2,8 +2,13 @@ Paperclip::Attachment.default_options[:storage] = :fog Paperclip::Attachment.default_options[:fog_credentials] = {:provider => "Local", :local_root => "#{Rails.root}/public"} Paperclip::Attachment.default_options[:fog_directory] = "/system" Paperclip::Attachment.default_options[:fog_host] = "/system" + unless APP_CONFIG["fog"].nil? Paperclip::Attachment.default_options[:fog_credentials] = APP_CONFIG["fog"]["credentials"] unless APP_CONFIG["fog"]["credentials"].nil? Paperclip::Attachment.default_options[:fog_directory] = APP_CONFIG["fog"]["directory"] unless APP_CONFIG["fog"]["directory"].nil? Paperclip::Attachment.default_options[:fog_host] = APP_CONFIG["fog"]["host"] unless APP_CONFIG["fog"]["host"].nil? + + if not APP_CONFIG["fog"]["credentials"].nil? and APP_CONFIG["fog"]["host"].nil? + Paperclip::Attachment.default_options[:fog_host] = nil + end end diff --git a/config/justask.yml.example b/config/justask.yml.example index fde5f56f..725b5c16 100644 --- a/config/justask.yml.example +++ b/config/justask.yml.example @@ -49,7 +49,8 @@ fog: provider: 'AWS' aws_access_key_id: 'ACCESS KEY' aws_secret_access_key: 'SECRET KEY' - # bucket name - directory: 'retrospring-user-content' - # URL host - host: "retrospring-user-content.s3.amazonaws.com" + region: 'space-pluto-1' + # bucket name, required + directory: 'retrospring' + # URL host, comment out to use default, GENERALLY you don't want to define this + # host: ''