From 5161a43801929a29caf2343c312fbb879a24beac Mon Sep 17 00:00:00 2001 From: Jeffrey Warren Date: Sat, 17 Jul 2021 13:09:14 -0400 Subject: [PATCH] switch image.rb to google storage via fog library (#9807) * switch image.rb to google storage via fog library * Update app/models/image.rb * Update app/models/image.rb * Update app/models/image.rb * Update image.rb * Add missing env vars * Add missing env vars to other environments * Update paperclip.rb * Update docker-compose-production.yml * Update docker-compose-stable.yml * Update docker-compose-unstable.yml * install fog-google * added local fog config and switch * fixed local storage for dev * fog_host relative * revert fog-google to before double-splat bug * Omit local_root parameter * Revert "Omit local_root parameter" This reverts commit 8406611614e815d9ef13673bfbc5ad3d092700a3. * Add JSON key option to Google Cloud Storage config * fixed display config with style, path * google_project * Update Gemfile * remove XML keys ``` google_storage_access_key_id: ENV["GOOGLE_STORAGE_KEY"] || '' , google_storage_secret_access_key: ENV["GOOGLE_STORAGE_SECRET"] || '' ``` * Add path adjustment to fog storage provider Co-authored-by: Sebastian Silva --- Gemfile | 2 ++ Gemfile.lock | 34 +++++++++++++++++++++++- app/models/image.rb | 2 +- config/initializers/paperclip.rb | 10 +++++++ containers/docker-compose-production.yml | 5 ++++ containers/docker-compose-stable.yml | 5 ++++ containers/docker-compose-unstable.yml | 5 ++++ 7 files changed, 61 insertions(+), 2 deletions(-) diff --git a/Gemfile b/Gemfile index f0e0f335a4..f9756c1c4a 100644 --- a/Gemfile +++ b/Gemfile @@ -12,6 +12,8 @@ gem 'authlogic-oid' gem "composite_primary_keys" gem 'figaro' # To easily test OAuth providers in the development mode gem 'friendly_id' +gem 'fog-google', '1.13.0' # Limited to `v1.13.0` due to https://github.com/fog/fog-google/issues/535 +gem 'fog-local', '0.6.0' gem 'gemoji' gem 'geocoder' gem 'georuby', '2.0' diff --git a/Gemfile.lock b/Gemfile.lock index 5573073fff..9c641aeb44 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -131,6 +131,7 @@ GEM dry-logic (~> 1.0, >= 1.0.2) equalizer (0.0.11) erubi (1.10.0) + excon (0.83.0) execjs (2.7.0) faraday (1.5.0) faraday-em_http (~> 1.0) @@ -155,8 +156,28 @@ GEM rake figaro (1.2.0) thor (>= 0.14.0, < 2) + fog-core (2.1.0) + builder + excon (~> 0.58) + formatador (~> 0.2) + mime-types + fog-google (1.13.0) + fog-core (<= 2.1.0) + fog-json (~> 1.2) + fog-xml (~> 0.1.0) + google-api-client (>= 0.44.2, < 0.51) + google-cloud-env (~> 1.2) + fog-json (1.2.0) + fog-core + multi_json (~> 1.10) + fog-local (0.6.0) + fog-core (>= 1.27, < 3.0) + fog-xml (0.1.3) + fog-core + nokogiri (>= 1.5.11, < 2.0.0) font-awesome-rails (4.7.0.7) railties (>= 3.2, < 7) + formatador (0.3.0) friendly_id (5.4.2) activerecord (>= 4.0.0) gemoji (3.0.1) @@ -164,6 +185,15 @@ GEM georuby (2.0.0) globalid (0.4.2) activesupport (>= 4.2.0) + google-api-client (0.50.0) + addressable (~> 2.5, >= 2.5.1) + googleauth (~> 0.9) + httpclient (>= 2.8.1, < 3.0) + mini_mime (~> 1.0) + representable (~> 3.0) + retriable (>= 2.0, < 4.0) + rexml + signet (~> 0.12) google-apis-core (0.4.0) addressable (~> 2.5, >= 2.5.1) googleauth (>= 0.16.2, < 2.a) @@ -617,6 +647,8 @@ DEPENDENCIES critical-path-css-rails (~> 4.1.1) execjs figaro + fog-google (= 1.13.0) + fog-local (= 0.6.0) font-awesome-rails friendly_id gemoji @@ -714,4 +746,4 @@ RUBY VERSION ruby 2.6.6p146 BUNDLED WITH - 2.2.3 + 2.2.21 diff --git a/app/models/image.rb b/app/models/image.rb index 371aac1dc4..1501daef13 100644 --- a/app/models/image.rb +++ b/app/models/image.rb @@ -4,7 +4,7 @@ class Image < ApplicationRecord belongs_to :user, foreign_key: :uid belongs_to :node, foreign_key: :nid - has_attached_file :photo, styles: { thumb: '200x150>', medium: '500x375>', large: '800x600>' } # , + has_attached_file :photo, styles: { thumb: '200x150>', medium: '500x375>', large: '800x600>' } validates :uid, presence: true validates :photo, presence: true, unless: :remote_url_provided? diff --git a/config/initializers/paperclip.rb b/config/initializers/paperclip.rb index b0eb6d87d7..f005bf8718 100644 --- a/config/initializers/paperclip.rb +++ b/config/initializers/paperclip.rb @@ -8,3 +8,13 @@ csv: ['application/octet-stream', 'text/plain'] } Paperclip::DataUriAdapter.register +Paperclip::Attachment.default_options[:storage] = :fog +Paperclip::Attachment.default_options[:fog_directory] = ENV["GOOGLE_STORAGE_BUCKET_NAME"] || '' +Paperclip::Attachment.default_options[:path] = ":rails_root/public/system/public/system/:class/:attachment/:id_partition/:style/:filename" +Paperclip::Attachment.default_options[:fog_credentials] = { + provider: ENV["FOG_PROVIDER"] || "Local", + local_root: "#{Rails.root}/public", + google_project: 'public-lab' , + google_json_key_location: ENV["GOOGLE_JSON_KEY_FILE"] || '' +} +Paperclip::Attachment.default_options[:fog_host] = "" diff --git a/containers/docker-compose-production.yml b/containers/docker-compose-production.yml index eb1b4ee2e4..95ff2c2849 100644 --- a/containers/docker-compose-production.yml +++ b/containers/docker-compose-production.yml @@ -54,6 +54,11 @@ services: - TWITTER_ACCESS_TOKEN_SECRET=${TWITTER_ACCESS_TOKEN_SECRET} - WEBSITE_HOST_PATTERN=${WEBSITE_HOST_PATTERN} - TWEET_SEARCH=${TWEET_SEARCH} + - GOOGLE_STORAGE_SECRET=${GOOGLE_STORAGE_SECRET} + - GOOGLE_STORAGE_KEY=${GOOGLE_STORAGE_KEY} + - GOOGLE_STORAGE_BUCKET_NAME=${GOOGLE_STORAGE_BUCKET_NAME} + - GOOGLE_JSON_KEY_FILE=${GOOGLE_JSON_KEY_FILE} + - FOG_PROVIDER=${FOG_PROVIDER} volumes: - ..:/app - /etc/passwd:/etc/passwd:ro diff --git a/containers/docker-compose-stable.yml b/containers/docker-compose-stable.yml index a75c7ff76e..c9a2642691 100644 --- a/containers/docker-compose-stable.yml +++ b/containers/docker-compose-stable.yml @@ -53,6 +53,11 @@ services: - TWITTER_ACCESS_TOKEN_SECRET=${TWITTER_ACCESS_TOKEN_SECRET} - WEBSITE_HOST_PATTERN=${WEBSITE_HOST_PATTERN} - TWEET_SEARCH=${TWEET_SEARCH} + - GOOGLE_JSON_KEY_FILE=${GOOGLE_JSON_KEY_FILE} + - GOOGLE_STORAGE_SECRET=${GOOGLE_STORAGE_SECRET} + - GOOGLE_STORAGE_KEY=${GOOGLE_STORAGE_KEY} + - GOOGLE_STORAGE_BUCKET_NAME=${GOOGLE_STORAGE_BUCKET_NAME} + - FOG_PROVIDER=${FOG_PROVIDER} volumes: - ..:/app - /etc/passwd:/etc/passwd:ro diff --git a/containers/docker-compose-unstable.yml b/containers/docker-compose-unstable.yml index a5868b6049..1175db9e27 100644 --- a/containers/docker-compose-unstable.yml +++ b/containers/docker-compose-unstable.yml @@ -53,6 +53,11 @@ services: - TWITTER_ACCESS_TOKEN_SECRET=${TWITTER_ACCESS_TOKEN_SECRET} - WEBSITE_HOST_PATTERN=${WEBSITE_HOST_PATTERN} - TWEET_SEARCH=${TWEET_SEARCH} + - GOOGLE_STORAGE_SECRET=${GOOGLE_STORAGE_SECRET} + - GOOGLE_JSON_KEY_FILE=${GOOGLE_JSON_KEY_FILE} + - GOOGLE_STORAGE_KEY=${GOOGLE_STORAGE_KEY} + - GOOGLE_STORAGE_BUCKET_NAME=${GOOGLE_STORAGE_BUCKET_NAME} + - FOG_PROVIDER=${FOG_PROVIDER} volumes: - ..:/app - /etc/passwd:/etc/passwd:ro