Skip to content

Commit

Permalink
Work in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
marcaltmann committed Jul 2, 2017
1 parent ab56531 commit 78c9ab8
Show file tree
Hide file tree
Showing 6 changed files with 161 additions and 11 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ end
# ---------- Model ----------

gem 'paperclip', '~> 4.2.2'
gem 'fog-google'
gem 'money-rails', '> 0.12.0' # dealing with money in activerecord
gem 'monetize' # parsing money
gem 'enumerize', '>= 0.5.1' # enums as symbols in ar
Expand Down
16 changes: 16 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ GEM
exception_notification (4.1.1)
actionmailer (>= 3.0.4)
activesupport (>= 3.0.4)
excon (0.57.0)
execjs (2.2.2)
eye (0.8.1)
celluloid (~> 0.17.3)
Expand All @@ -257,6 +258,20 @@ GEM
fastercsv (1.5.5)
ffaker (1.25.0)
ffi (1.9.8)
fog-core (1.44.3)
builder
excon (~> 0.49)
formatador (~> 0.2)
fog-google (0.1.0)
fog-core
fog-json
fog-xml
fog-json (1.0.2)
fog-core (~> 1.0)
multi_json (~> 1.10)
fog-xml (0.1.3)
fog-core
nokogiri (>= 1.5.11, < 2.0.0)
font-awesome-rails (4.2.0.0)
railties (>= 3.2, < 5.0)
formatador (0.2.5)
Expand Down Expand Up @@ -731,6 +746,7 @@ DEPENDENCIES
fakeweb (~> 1.3)
fastbill-automatic!
ffaker
fog-google
font-awesome-rails (>= 4.2.0.0)
formtastic (~> 2.3.0.rc3)
friendly_id (>= 4.0.9)
Expand Down
65 changes: 54 additions & 11 deletions app/models/images/user_image.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,16 @@

class UserImage < Image
extend STI
has_attached_file(
:image,

belongs_to :user, foreign_key: 'imageable_id'

after_commit :copy_to_gcs
before_destroy :delete_from_gcs

PAPERCLIP_STORAGE_OPTIONS = {
path: 'public/system/images/:id_partition/:style/:filename',
url: '/system/images/:id_partition/:style/:filename',
default_url: '/assets/missing.png',
styles: {
original: { geometry: '300>x300>', animated: false },
profile: { geometry: '300x300>', format: :jpg, animated: false },
Expand All @@ -15,18 +23,53 @@ class UserImage < Image
profile: '-quality 75 -strip -background white -gravity center -extent 300x300',
thumb: '-quality 75 -strip -background white'
},
default_url: '/assets/missing.png',
url: '/system/images/:id_partition/:style/:filename',
path: 'public/system/images/:id_partition/:style/:filename',
only_process: [:profile, :thumb]
)
}

belongs_to :user, foreign_key: 'imageable_id'
migrate_options = YAML.load(ERB.new(File.read("#{Rails.root}/config/google_migrate.yml")).result).symbolize_keys
PAPERCLIP_STORAGE_OPTIONS.merge!(migrate_options) if migrate_options[:activated_user_images]

has_attached_file :image, PAPERCLIP_STORAGE_OPTIONS

validates_attachment_presence :image, unless: :external_url
validates_attachment_content_type :image,
content_type: %w(
image/jpeg image/png image/gif
)
validates_attachment_content_type :image, content_type: %w(image/jpeg image/png image/gif)
validates_attachment_size :image, in: 1..20.megabytes # the 1 means one byte, not one megabyte

def copy_to_gcs
setup_connection
styles = [:original, :profile, :thumb]
styles.each do |style|
path = image.path(style)
# filename cannot be used directly because extension can be different
filename = path.split("/").last
object_name = "user_images/images/#{self.id}/#{style}/#{filename}"
file = File.open(path)
@connection.put_object(@bucket_name, object_name, file,
'x-goog-acl' => 'public-read')
end
end

def delete_from_gcs
setup_connection
styles = [:original, :profile, :thumb]
styles.each do |style|
begin
path = image.path(style)
# filename cannot be used directly because extension can be different
filename = path.split("/").last
object_name = "user_images/images/#{self.id}"
@connection.delete_object(@bucket_name, object_name)
rescue
end
end
end

private
def setup_connection
access_key = Rails.application.secrets.google_storage_access_key_id
secret_key = Rails.application.secrets.google_storage_secret_access_key
@connection = Fog::Storage::Google.new google_storage_access_key_id: access_key,
google_storage_secret_access_key: secret_key
@bucket_name = Rails.application.secrets.google_bucket_name
end
end
10 changes: 10 additions & 0 deletions config/google_migrate.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
storage: :fog
fog_credentials:
provider: 'Google'
google_storage_access_key_id: <%= Rails.application.secrets.google_storage_access_key_id %>
google_storage_secret_access_key: <%= Rails.application.secrets.google_storage_secret_access_key %>
fog_directory: <%= Rails.application.secrets.google_bucket_name %>
path: ':class/:attachment/:id/:style/:filename'
url: ':gcs_domain_url'

activated_user_images: false
8 changes: 8 additions & 0 deletions config/secrets.yml.example
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ development:
actoinmailer_username:
actionmailer_password:

# ======== GOOGLE CLOUD STORAGE ===
google_storage_access_key_id:
google_storage_secret_access_key:
google_bucket_name:

test:

secret_key_base: c5681369946d41cef75602da03bce3aa5b6031771ff1c12df56b6de774abcda87b5ef5a3bf01bf406f5e79c80a026fc5c43aef0abd6a64fad6f14da4a1de143b
Expand All @@ -51,3 +56,6 @@ test:
actionmailer_address:
actoinmailer_username:
actionmailer_password:
google_storage_access_key_id:
google_storage_secret_access_key:
google_bucket_name:
72 changes: 72 additions & 0 deletions lib/tasks/paperclip_migrate.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
namespace 'paperclip' do

desc "migrate to gcs"
task :migrate => :environment do
# Initialize
access_key = Rails.application.secrets.google_storage_access_key_id
secret_key = Rails.application.secrets.google_storage_secret_access_key
storage = Fog::Storage::Google.new google_storage_access_key_id: access_key,
google_storage_secret_access_key: secret_key

bucket_name = Rails.application.secrets.google_bucket_name
bucket = storage.directories.get(bucket_name)

# s3_options = YAML.load_file(File.join(Rails.root, 'config/aws.yml')).symbolize_keys

# bucket_name = s3_options[Rails.env.to_sym]["bucket"]

# AWS.config(
# :access_key_id => s3_options[Rails.env.to_sym]["access_key_id"],
# :secret_access_key => s3_options[Rails.env.to_sym]["secret_access_key"]
# )

# s3 = AWS::S3.new
# bucket = s3.buckets[bucket_name]



classes = []
classes = ENV['Class'].split(",") if ENV['Class']

classes.each do |class_info|
begin
class_name = class_info.split(":")[0]
attachment_name = class_info.split(":")[1].downcase

class_def = class_name.constantize

puts "Migrating #{class_name}:#{attachment_name}..."
if class_def.all.empty?
puts "#{class_name} is empty"
next
end

styles = class_def.first.send(attachment_name).styles.map{|style| style[0]}

class_def.find_each do |instance|
if not instance.send(attachment_name).exists? or instance.send(attachment_name).url.include? "google"
next
end

styles.each do |style|
attach = instance.send(attachment_name).path(style.to_sym)
filename = attach.split("/").last
path = "#{class_name.underscore.pluralize}/#{attachment_name.pluralize}/#{instance.id}/#{style}/#{filename}"
file = File.open(attach)
puts "Storing #{style} #{filename} in GCS..."
attachment = storage.put_object(bucket_name, path, file,
'x-goog-acl' => 'public-read')
end
end
# rescue AWS::S3::Errors::NoSuchBucket => e
# puts "Creating the non-existing bucket: #{bucket_name}"
# storage.put_bucket(bucket_name)
# retry
rescue Exception => e
puts "Ignoring #{class_name}"
puts e
end
puts ""
end
end
end

0 comments on commit 78c9ab8

Please sign in to comment.